每件事情似乎都是正确的,但无法找出错误的原因.. 我跟着这个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
答案 0 :(得分:1)
您正在使用ListActivity,因此请勿使用
ListView archiveaudio_listview = (ListView) findViewById(R.id.archiveaudio_list_listview);
像这样使用
ListView archiveaudio_listview = getListView();
而不是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();
}