App gettin NullPointerException missing view in Layout
EDIT: The solution is in the comment below my question.
I'm unsure why my app is force closing. Recently I had deleted an xml file (named 'directory.xml') and a java class file (named 'Directory.java'), and added in 4 xml and java files (directory1.xml, directory2.xml, directory3.xml, directory4.xml, Directory1.java, Directory2.java, Directory3.java, Directory4.java). I updated the manifest to include these changes. There are buttons I have on a layout called 'mainselect.xml'. Four buttons are the buttons that have on-click listeners set up on them to call the java files (Directory1-4.java), which simply sets the content layout to the associated .xml file. It all sounds so simple.
However, after looking at my LogCat, I noticed it said this:
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
..which seems to be saying it can't start the Main activity ('main.xml' leads to 'mainselect.xml' by pressing a button, by the way). However, I never changed main.xml or Main.java. Does anyone have any suggestions as to what I should try? Or perhaps someone sees a problem here?
Code:
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>
Check for a null reference in your Main.java file, on the line 22.
The first possible reason for this NPE is that pointed by Pompe de velo, when there's no such view in layout. The second one is building error. Somehow eclipse plugin (or android building tool itself) sometimes generate wrong R
file that can point to NPE or even views/layouts/strings etc messing.
This one is kinda of 2nd type coz we can see continueButton
view in main.xml
.
精彩评论