• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps startActivity don't open.

carelv7

Newbie
Jul 4, 2017
18
1
Hi, having a little problem, would appreciate some help
The startActivity just doesn't want to open the new intent. I have a toast message and if I comment the startActivity line out, it shows the toast just on the first list item, so it gets the listitem, but it just doesn't want to open the activity. Just crashes the app.
Here is my code:

Code:
package com.example.android.sab;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import static android.media.CamcorderProfile.get;

public class brands extends AppCompatActivity {

    [USER=1021285]@override[/USER]
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.brand_list);
        setTitle("Order By Brand");

        final ArrayList<Brand> brands = new ArrayList<Brand>();

        brands.add(new Brand("Black Label",R.drawable.black_label,R.color.black_label));
        brands.add(new Brand("Castle Lager", R.drawable.castle_lager,R.color.castle_lager));
        brands.add(new Brand("Castle Lite",R.drawable.castle_lite,R.color.castle_lite));
        brands.add(new Brand("Castle Milk Stout",R.drawable.milk_stout,R.color.castle_milk_stout));
        brands.add(new Brand("Hansa Pilsner",R.drawable.hansa,R.color.hansa));
        brands.add(new Brand("Flying Fish",R.drawable.flying_fish,R.color.flying_fish));
        brands.add(new Brand("Lion Lager",R.drawable.lion_lager,R.color.lion_lager));
        brands.add(new Brand("Redd's",R.drawable.redds,R.color.redds));
        brands.add(new Brand("Brutal Fruit",R.drawable.brutal_fruit,R.color.brutal_fruit));

        BrandAdapter adapter = new BrandAdapter(this, brands, R.color.order_by_brand);
        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            [USER=1021285]@override[/USER]
            public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
                int itemPosition = position;
               switch(itemPosition) {
                case 0 :Intent blackLabelIntent = new Intent(brands.this, black_label.class);
                startActivity(blackLabelIntent);
                    Context context = getApplicationContext();
                    CharSequence text = "Hello toast!";
                    int duration = Toast.LENGTH_SHORT;

                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                break;
            }}}
        );
        }}
 
Last edited by a moderator:
You guys are right, there was something wrong with the black_label class. When I replaced the black_label class with another clas (size class) the code works perfectly fine. It opens. Even if I comment out the line to go to the black_label class the toast works.

So this was the BlackLabel class:

package com.example.android.sab;
/**
* Created by Carel on 2017/07/13.
*/
public class BlackLabel {
}

It was just an empty class.
I then copies the size class over to the BlackLabel class and made the changes.
Now it looks like this:

package com.example.android.sab;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class BlackLabel extends AppCompatActivity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.black_label);
setTitle("Black label");
}
}
And I added it to the AndoidManifext.xml file and it worked. Previously when I had just the non activity class I couldn't add it in the manifest. No I could.

So I just thought I let you guys know. Thank you very much for replying. Apologies I took so long to answer, but I had to attend to my day job....lol
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones