添加setOnclickListener后,会发生nullPointerException错误

时间:2014-03-16 12:36:49

标签: android xml android-fragments

我刚刚启动android编程。我正在读书,只是复制书籍代码。 但是会发生错误。我认为错误发生在serOnClickListener方法。因为添加此方法后,请不要运行app(在设备上)。我搜索nullpointerException,但我没有得到答案..请帮助我。

我想知道片段布局。书籍解释只有activity_xml的布局代码,但我的 eclipse有两个xml文件fragment.xml,activity.xml


public class QuizActivity extends ActionBarActivity {

private Button mTrueButton;
private Button mFalseButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);

    mTrueButton =(Button)findViewById(R.id.true_button);
    mFalseButton = (Button)findViewById(R.id.false_button);

---问题方法

    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

        }
    });

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}   // end of onCreate


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.quiz, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_quiz, container, false);
        return rootView;
    }
}

}

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>

activity.xml

<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" />

logcat的

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

1 个答案:

答案 0 :(得分:1)

Button属于片段的视图层次结构。您应该在Fragment.onViewCreated

中移动初始化