android - ListView 错误无法启动 Activity ComponentInfo

标签 android listview

一切似乎都是对的,但找不到错误的原因.. 我关注了这个blog . 定义行的布局并在适配器中使用它

包 com.joshclemm.android.tabs;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ArchivedAudio_List extends ListActivity {
    static final String[] audiovalue = new String[] { "C by balguruswamy",
            "C++ by kanitkar", "java By Dr.kanitkar", ".net by Dr. joshi",
            "Sociology by Dr.Sarnaik", "Physoclgy by Dr.Batra" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(ArchivedAudio_List.this,
                R.layout.listview_with_img, R.id.label, audiovalue));
        ListView archiveaudio_listview = (ListView) findViewById(R.id.archiveaudio_list_listview);

        archiveaudio_listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(),
                        ((TextView) arg1).getText() + "clicked",
                        Toast.LENGTH_LONG).show();
                startActivity(new Intent(getApplicationContext(),
                        ArchiveAudio.class));

            }
        });

    }
}

listview_with_img.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/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20px" >
    </TextView>

   <ImageView
        android:id="@+id/icon"
        android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/play" >
    </ImageView>
</LinearLayout>

这是错误日志

07-24 06:35:07.809: E/AndroidRuntime(564): FATAL EXCEPTION: main
07-24 06:35:07.809: E/AndroidRuntime(564): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joshclemm.android.tabs/com.joshclemm.android.tabs.CustomTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joshclemm.android.tabs/com.joshclemm.android.tabs.ArchivedAudio_List}: java.lang.NullPointerException
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.os.Looper.loop(Looper.java:137)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.ActivityThread.main(ActivityThread.java:4340)
07-24 06:35:07.809: E/AndroidRuntime(564):  at java.lang.reflect.Method.invokeNative(Native Method)
07-24 06:35:07.809: E/AndroidRuntime(564):  at java.lang.reflect.Method.invoke(Method.java:511)
07-24 06:35:07.809: E/AndroidRuntime(564):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-24 06:35:07.809: E/AndroidRuntime(564):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-24 06:35:07.809: E/AndroidRuntime(564):  at dalvik.system.NativeStart.main(Native Method)
07-24 06:35:07.809: E/AndroidRuntime(564): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joshclemm.android.tabs/com.joshclemm.android.tabs.ArchivedAudio_List}: java.lang.NullPointerException
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:1796)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.widget.TabHost.setCurrentTab(TabHost.java:346)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.widget.TabHost.addTab(TabHost.java:236)
07-24 06:35:07.809: E/AndroidRuntime(564):  at com.joshclemm.android.tabs.CustomTabActivity.onCreate(CustomTabActivity.java:42)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.Activity.performCreate(Activity.java:4465)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
07-24 06:35:07.809: E/AndroidRuntime(564):  ... 11 more
07-24 06:35:07.809: E/AndroidRuntime(564): Caused by: java.lang.NullPointerException
07-24 06:35:07.809: E/AndroidRuntime(564):  at com.joshclemm.android.tabs.ArchivedAudio_List.onCreate(ArchivedAudio_List.java:28)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.Activity.performCreate(Activity.java:4465)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-24 06:35:07.809: E/AndroidRuntime(564):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
07-24 06:35:07.809: E/AndroidRuntime(564):  ... 21 more

最佳答案

你正在使用 ListActivity 所以不要使用

ListView archiveaudio_listview = (ListView) findViewById(R.id.archiveaudio_list_listview);

这样使用

ListView archiveaudio_listview = getListView();

检查 thisthis

您可以使用它代替 onitemclick。

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    //get selected items
    String selectedValue = (String) getListAdapter().getItem(position);
    Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

}

关于android - ListView 错误无法启动 Activity ComponentInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11625520/

相关文章:

Javax : ListView: load controllers by selecting item in ListView

android - TeamCity:使用Gradle进行构建以检索<项目名称>/build/outputs/apk/*。apk的路径,而不管哪个项目?

android - 如何知道一个 Activity 是否已经开始

android - 如何在一个 ListView 上绑定(bind)多个 Scroll 监听器?

api - 休息 API : fields of objects in a list of objects in response JSON

listview - FutureBuilder 只加载一次 API 数据

java - Android Studio java.lang.IllegalStateException

android - Android DatePicker日期格式可以修改吗?

iphone - 创建音乐家使用的哪个移动平台支持应用程序-录制和回放?

android - 具有动态加载图像的 ListView - 如何等待 ListView 加载所有图像,然后显示 ListView