Reputation: 2592
I am trying to run an android application where I have a MainActivity
which consists of a generated ListView
of TextView
items and an Intent so that when the user clicks on the first item in the list view, he goes to a second activity named PortalActivity
. This is what I have tried to do so far. When I try to run it in eclipse android emulator, it shows this popup that 'Unfortunately, myApp stopped working'.
public class MainActivity extends ListActivity
{
final String[] menuItems = {"Portal", "Settings", "Help", "About"};
@Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.listview_item, R.id.listview_tv, menuItems));
ListView yourListView = (ListView) findViewById(R.id.listview_tv);
yourListView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0)
{
Intent myIntent = new Intent(MainActivity.this,PortalActivity.class);
startActivity(myIntent);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
listview_item.xml: (layout for Main Activity)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/listview_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="TextView"
/>
</LinearLayout>
and PortalActivity is just an activity with another defined layout. I'm sure the problem is around the intent since when commenting it, the app runs and shows the list view and the layout for portal activity is also working fine.
This is the StackTrace in Log Cat
11-06 13:05:35.167: E/AndroidRuntime(1590): FATAL EXCEPTION: main
11-06 13:05:35.167: E/AndroidRuntime(1590): Process: com.example.androidexample, PID: 1590
11-06 13:05:35.167: E/AndroidRuntime(1590): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidexample/com.example.androidexample.MainActivity}: java.lang.NullPointerException
11-06 13:05:35.167: E/AndroidRuntime(1590): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-06 13:05:35.167: E/AndroidRuntime(1590): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-06 13:05:35.167: E/AndroidRuntime(1590): at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-06 13:05:35.167: E/AndroidRuntime(1590): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-06 13:05:35.167: E/AndroidRuntime(1590): at android.os.Handler.dispatchMessage(Handler.java:102)
11-06 13:05:35.167: E/AndroidRuntime(1590): at android.os.Looper.loop(Looper.java:137)
11-06 13:05:35.167: E/AndroidRuntime(1590): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-06 13:05:35.167: E/AndroidRuntime(1590): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 13:05:35.167: E/AndroidRuntime(1590): at java.lang.reflect.Method.invoke(Method.java:515)
11-06 13:05:35.167: E/AndroidRuntime(1590): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-06 13:05:35.167: E/AndroidRuntime(1590): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-06 13:05:35.167: E/AndroidRuntime(1590): at dalvik.system.NativeStart.main(Native Method)
11-06 13:05:35.167: E/AndroidRuntime(1590): Caused by: java.lang.NullPointerException
11-06 13:05:35.167: E/AndroidRuntime(1590): at com.example.androidexample.MainActivity.onCreate(MainActivity.java:25)
11-06 13:05:35.167: E/AndroidRuntime(1590): at android.app.Activity.performCreate(Activity.java:5243)
11-06 13:05:35.167: E/AndroidRuntime(1590): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-06 13:05:35.167: E/AndroidRuntime(1590): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
11-06 13:05:35.167: E/AndroidRuntime(1590): ... 11 more
Does someone have any idea what is wrong please? I am very new to Android so thanks for any help :)
Upvotes: 1
Views: 526
Reputation: 95646
This call
ListView yourListView = (ListView) findViewById(R.id.listview_tv);
returns null
because there is no ListView
in the layout with the resource ID R.id.listview_tv
. Then the following line
yourListView.setOnItemClickListener(new OnItemClickListener()
gets a NullPointerException
because yourListView
is null.
Since you are extending ListActivity
, to get the ListView
widget you need to call getListView()
like this:
ListView yourListView = getListView();
Upvotes: 1