Android 添加setOnclickListener后,将发生nullPointerException错误
我刚开始安卓编程。我是靠书本学习的,只是照本宣科。 但是错误发生了。我认为错误发生在serOnClickListener方法中。因为添加此方法后,不要运行应用程序(在设备上)。我搜索nullpointerException,但没有得到答案。。请帮帮我 我想知道碎片的布局。书籍仅用activity_xml解释布局代码,但我的 eclipse有两个xml文件fragment.xml,activity.xmlAndroid 添加setOnclickListener后,将发生nullPointerException错误,android,xml,android-fragments,Android,Xml,Android Fragments,我刚开始安卓编程。我是靠书本学习的,只是照本宣科。 但是错误发生了。我认为错误发生在serOnClickListener方法中。因为添加此方法后,不要运行应用程序(在设备上)。我搜索nullpointerException,但没有得到答案。。请帮帮我 我想知道碎片的布局。书籍仅用activity_xml解释布局代码,但我的 eclipse有两个xml文件fragment.xml,activity.xml <FrameLayout xmlns:android="http://schemas.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bignerdranch.android.geoquiz.QuizActivity"
tools:ignore="MergeRootFrame" />
代码 ---问题方法
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"
/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"
/>
</LinearLayout>
</LinearLayout>
按钮属于片段的视图层次结构。您应该将它们的初始化移动到Fragment.onViewCreated
setContentView(R.layout.activity\u)中代码>R.layout.activity\u测验!=R.layout.Fragments Fragment.onViewCreated的位置可能存在重复?请像对onCreateView所做的那样覆盖它
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"
/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"
/>
</LinearLayout>
</LinearLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bignerdranch.android.geoquiz.QuizActivity"
tools:ignore="MergeRootFrame" />
03-16 21:35:44.177: E/Trace(19811): error opening trace file: No such file or directory (2)
03-16 21:35:44.177: D/ActivityThread(19811): setTargetHeapUtilization:0.25
03-16 21:35:44.177: D/ActivityThread(19811): setTargetHeapIdealFree:8388608
03-16 21:35:44.187: D/ActivityThread(19811): setTargetHeapConcurrentStart:2097152
03-16 21:35:44.197: V/ActivityThread(19811): Class path: /data/app/com.bignerdranch.android.geoquiz-1.apk, JNI path: /data/data/com.bignerdranch.android.geoquiz/lib
03-16 21:35:44.277: I/ActionBarImpl(19811): mActivity.getLocalClassName : QuizActivityid : 0
03-16 21:35:44.287: I/ActionBarImpl(19811): mContext.getPackageName : com.bignerdranch.android.geoquizid : 0
03-16 21:35:44.287: W/dalvikvm(19811): threadid=1: thread exiting with uncaught exception (group=0x41252438)
03-16 21:35:44.307: E/AndroidRuntime(19811): FATAL EXCEPTION: main
03-16 21:35:44.307: E/AndroidRuntime(19811): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.QuizActivity}: java.lang.NullPointerException
03-16 21:35:44.307: E/AndroidRuntime(19811): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
03-16 21:35:44.307: E/AndroidRuntime(19811): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
03-16 21:35:44.307: E/AndroidRuntime(19811): at android.app.ActivityThread.access$600(ActivityThread.java:138)
03-16 21:35:44.307: E/AndroidRuntime(19811): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
03-16 21:35:44.307: E/AndroidRuntime(19811): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 21:35:44.307: E/AndroidRuntime(19811): at android.os.Looper.loop(Looper.java:137)
03-16 21:35:44.307: E/AndroidRuntime(19811): at android.app.ActivityThread.main(ActivityThread.java:4929)
03-16 21:35:44.307: E/AndroidRuntime(19811): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 21:35:44.307: E/AndroidRuntime(19811): at java.lang.reflect.Method.invoke(Method.java:511)
03-16 21:35:44.307: E/AndroidRuntime(19811): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
03-16 21:35:44.307: E/AndroidRuntime(19811): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-16 21:35:44.307: E/AndroidRuntime(19811): at dalvik.system.NativeStart.main(Native Method)
03-16 21:35:44.307: E/AndroidRuntime(19811): Caused by: java.lang.NullPointerException
03-16 21:35:44.307: E/AndroidRuntime(19811): at com.bignerdranch.android.geoquiz.QuizActivity.onCreate(QuizActivity.java:28)
03-16 21:35:44.307: E/AndroidRuntime(19811): at android.app.Activity.performCreate(Activity.java:5238)
03-16 21:35:44.307: E/AndroidRuntime(19811): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
03-16 21:35:44.307: E/AndroidRuntime(19811): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
03-16 21:35:44.307: E/AndroidRuntime(19811): ... 11 more