Reputation: 3756
So I've been following the wrox book on begining android I just can't seem to firgue out why my program is crashing when it switches between actives. The crash occurs when I click on the center d_pad outside of The EditBox and Button.
The error logcat describe is as follows.
11-11 11:03:50.700: W/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-11 11:03:50.949: E/AndroidRuntime(334): FATAL EXCEPTION: main
11-11 11:03:50.949: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activites/com.example.activites.Activity2}: java.lang.NullPointerException
11-11 11:03:50.949: E/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-11 11:03:50.949: E/AndroidRuntime(334): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-11 11:03:50.949: E/AndroidRuntime(334): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-11 11:03:50.949: E/AndroidRuntime(334): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-11 11:03:50.949: E/AndroidRuntime(334): at android.os.Handler.dispatchMessage(Handler.java:99)
11-11 11:03:50.949: E/AndroidRuntime(334): at android.os.Looper.loop(Looper.java:123)
11-11 11:03:50.949: E/AndroidRuntime(334): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-11 11:03:50.949: E/AndroidRuntime(334): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 11:03:50.949: E/AndroidRuntime(334): at java.lang.reflect.Method.invoke(Method.java:507)
11-11 11:03:50.949: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-11 11:03:50.949: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-11 11:03:50.949: E/AndroidRuntime(334): at dalvik.system.NativeStart.main(Native Method)
11-11 11:03:50.949: E/AndroidRuntime(334): Caused by: java.lang.NullPointerException
11-11 11:03:50.949: E/AndroidRuntime(334): at com.example.activites.Activity2.onCreate(Activity2.java:17)
11-11 11:03:50.949: E/AndroidRuntime(334): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-11 11:03:50.949: E/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-11 11:03:50.949: E/AndroidRuntime(334): ... 11 more
Here is the xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please enter you name" />
<EditText
android:id="@+id/txt_username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn_OK"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
Here are the two classes that are interacting with one another. First the main
package com.example.activites;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;
public class MainActivity extends Activity {
String tag = "Events";
int request_Code = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Log.d(tag, "In the onCreate() event");
}
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.activity_main, menu);
// return true;
// }
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode== KeyEvent.KEYCODE_DPAD_CENTER)
{
startActivityForResult(new Intent("com.example.ACTIVITY2"),request_Code);
}
return false;
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == request_Code){
if(resultCode==RESULT_OK){
Toast.makeText(this, data.getData().toString(), Toast.LENGTH_SHORT).show();
}
}
}
}
Second class
package com.example.activites;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Activity2 extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Button btn = (Button) findViewById(R.id.btn_OK);
btn.setOnClickListener (new View.OnClickListener(){
public void onClick (View view){
Intent data = new Intent();
EditText txt_username = (EditText) findViewById(R.id.txt_username);
data.setData(Uri.parse(txt_username.getText().toString()));
setResult(RESULT_OK, data);
finish();
}
});
}
}
Here is the manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activites"
android:versionCode="1"
android:debuggable="true"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity2"
android:label="Activity 2" >
<intent-filter>
<action android:name="com.example.ACTIVITY2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Again thanks in advance for your help/ I'm just surprised how different debugging these android applications are from .net development.
Upvotes: 0
Views: 683
Reputation: 86948
I would guess that your activity2
layout doesn't have a Button with the id btn_OK
, so btn
is null
:
Button btn = (Button) findViewById(R.id.btn_OK);
btn.setOnClickListener( ... ); // Crash here with an NPE
findViewById()
can only locate Views that are in the current layout passed to setContentView()
, otherwise it returns null
.
This is the important LogCat information:
Caused by: java.lang.NullPointerException
at com.example.activites.Activity2.onCreate(Activity2.java:17)
Notice the last bit Activity2.java:17
, that is the file and line number where the error occured. Which I guess is: btn.setOnClickListener( ... );
for the reasons above.
Upvotes: 3