Android App gettin NullPointerException布局中缺少视图
编辑:答案在我问题下面的评论中 我不确定为什么我的应用程序会强制关闭。最近我删除了一个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文件。听起来很简单 然而,在看了我的日志后,我注意到它说:Android App gettin NullPointerException布局中缺少视图,android,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)。我更新了清单以包含这
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.xml'通过按下按钮导致'mainselect.xml')。但是,我从未更改main.xml或main.java。有人对我应该尝试什么有什么建议吗?或者有人发现了问题
代码:
Main.java------
main.xml----
检查Main.java文件第22行的空引用。检查Main.java文件第22行的空引用。此NPE的第一个可能原因是Pompe de velo指出的,当时布局中没有此类视图。第二个是构建错误。不知何故,eclipse插件(或android构建工具本身)有时会生成错误的R
文件,这些文件可能指向NPE,甚至会导致视图/布局/字符串等混乱。这是第二种类型,因为我们可以在
main.xml
中看到continueButton
视图。这个NPE的第一个可能原因是Pompe de velo指出的,当时布局中没有这样的视图。第二个是构建错误。不知何故,eclipse插件(或android构建工具本身)有时会生成错误的R
文件,这些文件可能指向NPE,甚至会导致视图/布局/字符串等混乱。这是第二种类型,因为我们可以在
main.xml
中看到continueButton
视图。问题出在main.java的第22行,那里的代码是什么?看起来像是构建错误。尝试清理/构建项目以重新生成gen
文件夹。Genius!清洗它成功了!如果你把它作为一个答案,我会给它一个绿色的小复选框:)。编辑:或者我会自己回答,但我会给你信用。非常感谢。EDIT2:显然我有8个小时不能自己回答。我稍后再讨论。问题在Main.java的第22行,那里的代码是什么?看起来像是构建错误。尝试清理/构建项目以重新生成gen
文件夹。Genius!清洗它成功了!如果你把它作为一个答案,我会给它一个绿色的小复选框:)。编辑:或者我会自己回答,但我会给你信用。非常感谢。EDIT2:显然我有8个小时不能自己回答。第21行和第22行是:viewv1=findviewbyd(R.id.continueButton)
和v1.setOnClickListener(此)代码>,它在我进行更改之前工作。我从未编辑过continueButton。因此v1为空。查看当前布局中是否有continueButton id。视图v1
从您的findViewById()
函数获取空引用。因此,不能对其调用setOnClickListener()
。检查布局xml文件中是否有idcontinueButton
,哪个xml文件应该是传递到setContentView()
函数中的文件。那么,在我更改代码的其他部分之前,为什么这样做有效continueButton
,单击后,加载MainSelect.xml
。是的,main.xml中仍然有一个continueButton id。第21行和第22行是:View v1=findViewById(R.id.continueButton)
和v1.setOnClickListener(此)代码>,它在我进行更改之前工作。我从未编辑过continueButton。因此v1为空。查看当前布局中是否有continueButton id。视图v1
从您的findViewById()
函数获取空引用。因此,不能对其调用setOnClickListener()
。检查布局xml文件中是否有idcontinueButton
,哪个xml文件应该是传递到setContentView()
函数中的文件。那么,在我更改代码的其他部分之前,为什么这样做有效continueButton
,单击后,将加载MainSelect.xml
。是的,main.xml中仍然有一个continueButton id。
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;
}
}
}
<?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>