1. Are you ready for the Galaxy S20? Here is everything we know so far!

startActivity don't open.

Discussion in 'Android Development' started by carelv7, Jul 13, 2017.

  1. carelv7

    carelv7 Newbie
    Thread Starter

    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 (Text):
    1.  
    2. package com.example.android.sab;
    3.  
    4. import android.content.Context;
    5. import android.content.Intent;
    6. import android.support.v7.app.AppCompatActivity;
    7. import android.os.Bundle;
    8. import android.view.View;
    9. import android.widget.AdapterView;
    10. import android.widget.ListView;
    11. import android.widget.Toast;
    12.  
    13. import java.util.ArrayList;
    14.  
    15. import static android.media.CamcorderProfile.get;
    16.  
    17. public class brands extends AppCompatActivity {
    18.  
    19.     [USER=1021285]@override[/USER]
    20.     protected void onCreate(Bundle savedInstanceState) {
    21.         super.onCreate(savedInstanceState);
    22.         setContentView(R.layout.brand_list);
    23.         setTitle("Order By Brand");
    24.  
    25.         final ArrayList<Brand> brands = new ArrayList<Brand>();
    26.  
    27.         brands.add(new Brand("Black Label",R.drawable.black_label,R.color.black_label));
    28.         brands.add(new Brand("Castle Lager", R.drawable.castle_lager,R.color.castle_lager));
    29.         brands.add(new Brand("Castle Lite",R.drawable.castle_lite,R.color.castle_lite));
    30.         brands.add(new Brand("Castle Milk Stout",R.drawable.milk_stout,R.color.castle_milk_stout));
    31.         brands.add(new Brand("Hansa Pilsner",R.drawable.hansa,R.color.hansa));
    32.         brands.add(new Brand("Flying Fish",R.drawable.flying_fish,R.color.flying_fish));
    33.         brands.add(new Brand("Lion Lager",R.drawable.lion_lager,R.color.lion_lager));
    34.         brands.add(new Brand("Redd's",R.drawable.redds,R.color.redds));
    35.         brands.add(new Brand("Brutal Fruit",R.drawable.brutal_fruit,R.color.brutal_fruit));
    36.  
    37.         BrandAdapter adapter = new BrandAdapter(this, brands, R.color.order_by_brand);
    38.         ListView listView = (ListView) findViewById(R.id.list);
    39.         listView.setAdapter(adapter);
    40.  
    41.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    42.             [USER=1021285]@override[/USER]
    43.             public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
    44.                 int itemPosition = position;
    45.                switch(itemPosition) {
    46.                 case 0 :Intent blackLabelIntent = new Intent(brands.this, black_label.class);
    47.                 startActivity(blackLabelIntent);
    48.                     Context context = getApplicationContext();
    49.                     CharSequence text = "Hello toast!";
    50.                     int duration = Toast.LENGTH_SHORT;
    51.  
    52.                     Toast toast = Toast.makeText(context, text, duration);
    53.                     toast.show();
    54.                 break;
    55.             }}}
    56.         );
    57.         }}
    58.  


     


    #1 carelv7, Jul 13, 2017
    Last edited by a moderator: Jul 13, 2017
  2. Best Answer:
    Post #4 by Deleted User, Jul 14, 2017 (1 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. Deleted User

    Deleted User Guest

    Please show the stack trace from the Logcat view.
     
    steve likes this.
  4. steve

    steve Developer
    Administrator

    Try to rename your activity class to BlackLabel rather than black_label.
     
  5. Deleted User

    Deleted User Guest

    Agreed Steve. Also need to see the code for that activity, because it sounds like the error is happening there. The stack trace would tell us for sure.
     
  6. carelv7

    carelv7 Newbie
    Thread Starter

    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
     
  7. wseng92

    wseng92 Android Enthusiast

    We are happy that you solved it. Please wrap your code with code tags to make this site clean ;)
     
Loading...
Similar Threads - startActivity don't open
  1. The_Chief
    Replies:
    4
    Views:
    725
  2. App Update
    Replies:
    0
    Views:
    311
  3. hesgeneric
    Replies:
    6
    Views:
    633
  4. dorlow
    Replies:
    3
    Views:
    720
  5. redrosealways
    Replies:
    8
    Views:
    640
  6. JimmyPixel3a
    Replies:
    21
    Views:
    1,281
  7. dorlow
    Replies:
    9
    Views:
    580
  8. corpID
    Replies:
    0
    Views:
    508
  9. Paultx
    Replies:
    2
    Views:
    376
  10. Daniel Fernandes
    Replies:
    2
    Views:
    4,632

Share This Page

Loading...