我正在尝试运行一个 android 应用程序,其中我有一个MainActivity
由生成ListView
的TextView
项目和一个 Intent 组成的应用程序,这样当用户单击列表视图中的第一个项目时,他会转到第二个名为PortalActivity
. 这是我迄今为止尝试做的。当我尝试在 eclipse android 模拟器中运行它时,它会显示这个弹出窗口“不幸的是,myApp 停止工作”。
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:(主要活动的布局)
<?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>
而 PortalActivity 只是一个具有另一个定义布局的活动。我确定问题出在意图上,因为在评论它时,应用程序运行并显示列表视图,门户活动的布局也可以正常工作。
这是 Log Cat 中的 StackTrace
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
请问有人知道出了什么问题吗?我对 Android 很陌生,所以感谢您的帮助:)