单击Listener导致无法启动活动nullpointer异常

时间:2013-11-04 21:01:41

标签: android

我在一个更大的项目中整天都在努力解决这个问题。我写了一个测试应用程序,以排除它是由我的数据对象等引起的。但我得到了相同的结果。这个应用程序中的所有内容或多或少都是android studio提供的样板文件和开发人员文档中的示例。 min SDK是4.0,我用4.3编译它。

    package com.testapp.testapp;

        import android.app.Activity;
        import android.app.ActionBar;
        import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.os.Build;
    import android.widget.Button;
    import android.widget.TextView;

    public class MainActivity extends Activity {
    TextView MyTextview;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            if (savedInstanceState == null) {
                getFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment())
                        .commit();
            }
            Button MyButton = (Button)findViewById(R.id.mybutton);
            MyTextview = (TextView)findViewById(R.id.tv);
            final View.OnClickListener Click = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch(v.getId()) {
                        case R.id.mybutton:
                            MyTextview.setText("Test");
                        break;
                    }
                }
            };
            MyButton.setOnClickListener(Click);
        }
        /**
         * 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_main, container, false);
                return rootView;
            }
        }

    }

fragment_main.xml     

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"/>
    <Button
        android:layout_height="75dp"
        android:layout_width="fill_parent"
        android:id="@+id/mybutton"
     />

</RelativeLayout>

堆栈跟踪

11-04 15:47:08.396  21228-21228/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testapp.testapp/com.testapp.testapp.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2336)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
            at android.app.ActivityThread.access$600(ActivityThread.java:150)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1266)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5256)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.testapp.testapp.MainActivity.onCreate(MainActivity.java:39)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2290)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
            at android.app.ActivityThread.access$600(ActivityThread.java:150)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1266)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5256)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

你在错误的地方寻找正确的东西。您要查找的内容(findViewById)属于片段视图层次结构,而不属于活动。覆盖片段内的onActivityCreated,然后移动findViewById onCreate中的Activity

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_main, container, false);
            Button MyButton = (Button)rootView.findViewById(R.id.mybutton);
             MyTextview = (TextView)rootView.findViewById(R.id.tv);
            final View.OnClickListener Click = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch(v.getId()) {
                    case R.id.mybutton:
                        MyTextview.setText("Test");
                    break;
                }
            }
        };
            MyButton.setOnClickListener(Click);
            return rootView;
        }
    }

从您的活动onCreate中删除代码段并再次运行