Click here to Skip to main content
15,173,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir, I have created an android application with navigation drawer. When the user clicks the application should go for the new activity.For that I have coded this

Java
if (id == R.id.maths) {
            Intent intent = new Intent(Main.this, Mathematics.class);
            startActivity(intent);
}

and it shifts to the new activity successfully. I want to add a list view in my new activity for that I have coded like this, for the blank activity inorder to show the list view

Java
public class Mathematics extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_mathematics);
        String[] classes = {"A1","A2","A3","A4","A5","A6","A7","A8"};
        ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.content_mathematics, classes);

        ListView listView = (ListView) findViewById(R.id.mathList);
        listView.setAdapter(adapter);
    }
}


I have also added list view in the xml file,
Java
<ListView
     android:id="@+id/mathList"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" >
 </ListView>


I have build-ed the application with zero warnings and zero errors but when I run the application the app crashes

Kindly help me with this sir

Thank you for your time sir

What I have tried:

I tried running my application using an emulator and the emulator shows black screen for hours. I referred tutorials point and modified the code but still the same case. I couldn't figure out since the application compiled with zero errors.
Posted
Updated 2-Jul-16 8:29am
Comments
Richard MacCutchan 1-Jul-16 5:19am
   
You need to use your debugger and the emulator logs to find out where it crashed, and why. We cannot guess what is missing or incorrect.
Richard MacCutchan 1-Jul-16 5:21am
   
It may be that your adapter variable is being cleaned up when it goes out of scope. Make it a class variable and see if that fixes it.
[no name] 2-Jul-16 13:30pm
   
Thank you sir I made it to work!

1 solution

Finally made it to work.

Java
String[] topics = new String[]{"A1","A2","A3","A4","A5","A6","A7","A8"};
        ListAdapter adapt = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,topics);
        ListView view = (ListView)findViewById(R.id.maths);
        view.setAdapter(adapt);
</string>


Modifications:

Declared String in alternate way(I know both are same)

As suggested by Richard MacCutchan I have Referred current tutorial from Android documentation and modified this line to this,
ArrayAdapter adapter = new ArrayAdapter<string>(this, R.layout.content_mathematics, classes);</string>


To

Java
ListAdapter adapt = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,topics);</string>


I don't need images in this list view so I've set the layout to its basic (simple_list_item_1)
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900