android - 生成的 ListView 应用程序的 Intent 停止工作

标签 android android-intent android-listview

我正在尝试运行一个 android 应用程序,其中我有一个 MainActivity,它由一个生成的 ListView 和一个 Intent 组成 TextView 项目当用户单击 ListView 中的第一项时,他将转到名为 PortalActivity 的第二个 Activity 。到目前为止,这是我尝试做的。当我尝试在 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:(主要 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>

而 PortalActivity 只是一个具有另一个已定义布局的 Activity 。我确定问题出在 Intent 上,因为在评论它时,应用程序运行并显示 ListView ,门户 Activity 的布局也工作正常。

这是 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 的新手,所以感谢您的帮助:)

最佳答案

这个电话

     ListView yourListView = (ListView) findViewById(R.id.listview_tv);

返回 null 因为布局中没有资源 ID R.id.listview_tvListView。然后是下面一行

     yourListView.setOnItemClickListener(new OnItemClickListener() 

得到一个 NullPointerException 因为 yourListView 是空的。

由于您正在扩展 ListActivity,要获取 ListView 小部件,您需要像这样调用 getListView():

ListView yourListView = getListView();

关于android - 生成的 ListView 应用程序的 Intent 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19819222/

相关文章:

android - InAppBilling问题

android - 如何使用图像作为按钮

android - 从 Android 应用程序打开 Facebook 页面?

java - 无法使用共享首选项设置按钮的可见性

java - 调整行大小时用 View 填充 ListView 行高

android - 我如何在 Android API 级别 10 上实现带有 ListView 的 SlidingDrawer?

javascript - Phonegap 导航模块未使用 JQM 加载

java - 如何向类中的函数添加回调

android - 在 Android 上使用 Chrome 打开 android-app 方案

android - 在 ListView 中修改文本大小时更新项目高度