无法实例化活动组件信息:nullpointerexception

时间:2015-07-07 01:40:46

标签: android nullpointerexception

我有一个活动,这是我的应用程序启动器活动。它由于此错误而无法运行:Unable to instantiate activity component info是由nullpointerexception:

这是我的活动Java文件:

public class MainMenuActivity extends Activity {
private TextView view1;
private TextView view2;

private File directory;
private File f1;
private File f2;

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

    this.directory = new File(this.getFilesDir(), null);
    this.f1 = new File(this.directory, "f1");
    this.f2 = new File(this.directory, "f2");

    checkExist();

    view1 = (TextView) findViewById(R.id.text1);
    view1.setCompoundDrawablesWithIntrinsicBounds(getResources()
            .getDrawable(R.drawable.image_1), null, null, null);

    view2 = (TextView) findViewById(R.id.text2);
    view2.setCompoundDrawablesWithIntrinsicBounds(getResources()
            .getDrawable(R.drawable.ic_launcher), null, null, null);

    view1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainMenuActivity.this, "Walther The Best Clock",
                    Toast.LENGTH_LONG).show();

        }
    });

    view2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    });
}

private void checkExist() {
    if (!this.directory.exists()) {
        this.directory.mkdir();
    }
    if (!f1.exists()) {
        f1.mkdir();
    }
    if (!f2.exists()) {
        f2.mkdir();
    }
    if (!f3.exists()) {
        f3.mkdir();
    }
    if (!f4.exists()) {
        f4.mkdir();
    }
}
}

和活动xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/ic_launcher"
        android:text="This is First"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:background="#DDD"
        android:layout_weight="1"
        android:gravity="center_vertical"/>

    <TextView
        android:id="@+id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/ic_launcher"
        android:text="This is second"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_weight="1"
        android:gravity="center_vertical"/>


</LinearLayout>

这是错误日志:

07-06 21:23:02.409: E/AndroidRuntime(17901): FATAL EXCEPTION: main
07-06 21:23:02.409: E/AndroidRuntime(17901): Process: com.example.griding, PID: 17901
07-06 21:23:02.409: E/AndroidRuntime(17901): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.griding/com.example.griding.MainMenuActivity}: java.lang.NullPointerException: name == null
07-06 21:23:02.409: E/AndroidRuntime(17901):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at android.os.Looper.loop(Looper.java:136)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at android.app.ActivityThread.main(ActivityThread.java:5017)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at java.lang.reflect.Method.invokeNative(Native Method)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at java.lang.reflect.Method.invoke(Method.java:515)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at dalvik.system.NativeStart.main(Native Method)
07-06 21:23:02.409: E/AndroidRuntime(17901): Caused by: java.lang.NullPointerException: name == null
07-06 21:23:02.409: E/AndroidRuntime(17901):    at java.io.File.<init>(File.java:150)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at java.io.File.<init>(File.java:124)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at com.example.griding.MainMenuActivity.onCreate(MainMenuActivity.java:32)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at android.app.Activity.performCreate(Activity.java:5231)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-06 21:23:02.409: E/AndroidRuntime(17901):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-06 21:23:02.409: E/AndroidRuntime(17901):    ... 11 more
07-06 21:23:05.139: I/Process(17901): Sending signal. PID: 17901 SIG: 9

2 个答案:

答案 0 :(得分:1)

您正尝试在此处创建一个带有空名称的文件:this.directory = new File(this.getFilesDir(), null);

将其更改为this.directory = this.getFilesDir();

来自文档: http://docs.oracle.com/javase/7/docs/api/java/io/File.html#File%28java.io.File,%20java.lang.String%29

  

公共文件(文件父级,       String child)

     

.....

     

抛出:       NullPointerException - 如果child为null

答案 1 :(得分:1)

空指针异常由“this.directory = new File(this.getFilesDir(),null);”引起。您不能将null作为子路径名字符串传递。