这里我的 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="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/layout2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:focusable="false" />
<TextView
android:id="@+id/tvCityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="15dp"
android:textColor="@drawable/text_hover"
android:paddingLeft="30dp"
android:paddingTop="9dp"
android:textStyle="normal"
android:background="@drawable/dest_hover"
/>
</LinearLayout>
</LinearLayout>
这是我处理 listview 的 java 代码:
ListView lst;
Button chk;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.destination);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
lst=(ListView)findViewById(R.id.listViewCities);
lst.setFocusable(false);
chk=(Button)findViewById(R.id.check);
chk.setFocusableInTouchMode(false);
chk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("sukjhdskfskdgf", "ggogogogogogogogoggo");
}
});
}
Log cat 中显示的错误:
08-30 05:43:30.440: E/AndroidRuntime(1442): FATAL EXCEPTION: main
08-30 05:43:30.440: E/AndroidRuntime(1442): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emaven.triphills/com.emaven.triphills.Tab}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emaven.triphills/com.emaven.triphills.Destination}: java.lang.NullPointerException
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.os.Looper.loop(Looper.java:137)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-30 05:43:30.440: E/AndroidRuntime(1442): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 05:43:30.440: E/AndroidRuntime(1442): at java.lang.reflect.Method.invoke(Method.java:511)
08-30 05:43:30.440: E/AndroidRuntime(1442): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-30 05:43:30.440: E/AndroidRuntime(1442): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-30 05:43:30.440: E/AndroidRuntime(1442): at dalvik.system.NativeStart.main(Native Method)
08-30 05:43:30.440: E/AndroidRuntime(1442): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emaven.triphills/com.emaven.triphills.Destination}: java.lang.NullPointerException
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2023)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.widget.TabHost.setCurrentTab(TabHost.java:413)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.widget.TabHost.addTab(TabHost.java:240)
08-30 05:43:30.440: E/AndroidRuntime(1442): at com.emaven.triphills.Tab.onCreate(Tab.java:28)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.Activity.performCreate(Activity.java:5104)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-30 05:43:30.440: E/AndroidRuntime(1442): ... 11 more
08-30 05:43:30.440: E/AndroidRuntime(1442): Caused by: java.lang.NullPointerException
08-30 05:43:30.440: E/AndroidRuntime(1442): at com.emaven.triphills.Destination.onCreate(Destination.java:51)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.Activity.performCreate(Activity.java:5104)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-30 05:43:30.440: E/AndroidRuntime(1442): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-30 05:43:30.440: E/AndroidRuntime(1442): ... 21 more
请帮我解决这个问题。感谢您