2011-08-12 20 views
0

编辑:解决方案是在我的问题下面的评论。应用程序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文件。这一切听起来很简单。

然而,看着我的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.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> 
+1

问题是在第22行Main.java,它的代码是什么? –

+2

看起来像构建错误。尝试清理/生成项目以重新生成'gen'文件夹。 – ernazm

+0

天才!清理它的工作!如果你将其作为回答发布,我会给它一个绿色的小复选框:)。编辑:或者我会自己回答,但我会给你信贷。谢谢!编辑2:显然我无法自己回答8个小时。我会在那之后得到它。 – Mxyk

回答

1

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

3

您在您的Main.java空引用文件,在线22上。

+0

第21行和第22行是:'View v1 = findViewById(R.id.continueButton);''''和'v1.setOnClickListener(this);',它在我进行更改之前就可以工作。我从来没有编辑过continueButton。 – Mxyk

+0

因此v1是空的。看看在当前布局中是否有continueButton ID。 –

+0

视图'v1'从您的'findViewById()'函数中获得空引用。因此,你不能在其上调用'setOnClickListener()'。检查你的布局xml文件中是否有'continueButton',这个xml文件应该是你传递给'setContentView()'函数的文件。 – Wroclai