应用程序gettin NullPointerException布局中缺少视图

时间:2011-08-12 14:38:09

标签: android

编辑:解决方案在我的问题下方的评论中。

我不确定为什么我的应用程序会强行关闭。最近我删除了一个xml文件(名为'directory.xml')和一个java类文件(名为'Directory.java'),并添加了4个xml和java文件(directory1.xml,directory2.xml,directory3.xml, directory4.xml,Directory1.java,Directory2.java,Directory3.java,Directory4.java)。我更新了清单以包含这些更改。我在布局上有一些名为'mainselect.xml'的按钮。四个按钮是在其上设置点击式监听器以调用java文件(Directory1-4.java)的按钮,它只是将内容布局设置为关联的.xml文件。这一切听起来都很简单。

然而,在看了我的LogCat之后,我注意到它说:

08-12 10:26:50.973: ERROR/AndroidRuntime(1205): FATAL EXCEPTION: main
08-12 10:26:50.973: ERROR/AndroidRuntime(1205): java.lang.RuntimeException: Unable to start activity ComponentInfo{around.lowell/around.lowell.Main}: java.lang.NullPointerException
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at android.os.Looper.loop(Looper.java:123)
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at java.lang.reflect.Method.invokeNative(Native Method)
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at java.lang.reflect.Method.invoke(Method.java:521)
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at dalvik.system.NativeStart.main(Native Method)
08-12 10:26:50.973: ERROR/AndroidRuntime(1205): Caused by: java.lang.NullPointerException
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at around.lowell.Main.onCreate(Main.java:22)
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
 08-12 10:26:50.973: ERROR/AndroidRuntime(1205):     ... 11 more

..似乎在说它无法启动Main活动(顺便说一句,'main.xml'通过按下按钮导致'mainselect.xml')。但是,我从未更改main.xml或Main.java。有没有人对我应该尝试什么有任何建议?或许有人在这里看到问题?

代码:

Main.java ------

package around.lowell;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.content.Intent;

public class Main extends Activity implements OnClickListener {
// Used for color: 1 = color, 0 = not
public static int x = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Set up click listeners for all buttons
    View v1 = findViewById(R.id.continueButton);
    v1.setOnClickListener(this);

    View v2 = findViewById(R.id.colorCheck);
    v2.setOnClickListener(this);
}

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.continueButton:
        Intent i1 = new Intent(this, MainSelect.class);
        startActivity(i1);
        break;
    case R.id.colorCheck:
        CheckBox check = (CheckBox) findViewById(R.id.colorCheck);
        if(check.isChecked()) {
            // Main
            x = 1;
            LinearLayout l1a = (LinearLayout) findViewById(R.id.mainLayout);
            l1a.setBackgroundResource(R.drawable.background);
            View b1a = findViewById(R.id.continueButton);
            b1a.setBackgroundResource(R.drawable.buttoncolor);
            View b2a = findViewById(R.id.colorCheck);
            b2a.setBackgroundResource(R.drawable.buttoncolor); 
        } else {
            // Main
            x = 0;
            LinearLayout l1a = (LinearLayout) findViewById(R.id.mainLayout);
            l1a.setBackgroundColor(R.color.blackground);
            View b1a = findViewById(R.id.continueButton);
            b1a.setBackgroundResource(R.drawable.colorless);
            View b2a = findViewById(R.id.colorCheck);
            b2a.setBackgroundResource(R.drawable.colorless); 
        }
        break;
    }
}
}

main.xml ----

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    >
   <ScrollView
     android:layout_height="fill_parent"
     android:layout_width="fill_parent" >
<LinearLayout
     android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/welcomeText1"
        android:text="Welcome to the..."
        android:textColor="#FFFFFF"
        android:textSize="24sp"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginTop="70dip"
        android:gravity="center" 
        >
    </TextView>
    <TextView
        android:id="@+id/welcomeText2"
        android:text="Around Lowell App"
        android:textColor="#FFFFFF"
        android:textSize="44sp"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginTop="30dip"
        android:gravity="center"
        >
    </TextView>
    <TextView
        android:id="@+id/author"
        android:text="By: Mike Stowell"
        android:textColor="#FFFFFF"
        android:textSize="12sp"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginTop="20dip"
        android:gravity="center"
    >
    </TextView>
    <Button
        android:id="@+id/continueButton"
        android:text="Continue"
        android:textColor="#FFFFFF"
        android:background="@drawable/buttoncolor"
        android:layout_height="40sp"
        android:layout_width="fill_parent"
        android:layout_marginLeft="30dip"
        android:layout_marginRight="30dip"
        android:layout_marginTop="30dip"
        android:layout_marginBottom="10dip"
    >
    </Button>
    <CheckBox
        android:id="@+id/colorCheck"
        android:text="          Color "
        android:checked="true"
        android:background="@drawable/buttoncolor"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="center" 
    >
    </CheckBox>
</LinearLayout>
  </ScrollView>
</LinearLayout>

2 个答案:

答案 0 :(得分:3)

在第22行的Main.java文件中检查空引用。

答案 1 :(得分:1)

这个NPE的第一个可能原因是Pompe de velo指出的,当布局中没有这样的视图时。第二个是构建错误。不知何故eclipse插件(或android构建工具本身)有时会生成错误的R文件,可能指向NPE甚至视图/布局/字符串等混乱。
这个是第二类型,我们可以在continueButton中看到main.xml视图。