I successfully compiled an android app and created it's apk. I also signed the .apk file with my keystore. But it gives me a NullPointerException for some reason when running in USB debugging mode.
Here is the exception:
06-16 15:23:11.639 30531-30531/com.adi.play E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.adi.play/com.adi.play.PlayActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
at com.adi.play.PlayActivity.<init>(PlayActivity.java:12)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
My AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.adi.play"
android:versionCode="1"
android:versionName="1.0">
<application android:label="@string/app_name"
android:icon="@drawable/me">
<activity android:name="PlayActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
My com.adi.play.PlayActivity
.java file:
package com.adi.play;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
public class PlayActivity extends Activity {
public final String RESULT = getResources().getString(R.string.result);
private int total = 0;
private TextView displayResult;
private Button incrementButton;
private Button decrementButton;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_play);
displayResult = (TextView)findViewById(R.id.display_result);
incrementButton = (Button)findViewById(R.id.increment_button);
decrementButton = (Button)findViewById(R.id.decrement_button);
displayResult.setText(RESULT+total);
incrementButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
total++;
displayResult.setText(RESULT+total);
}
});
decrementButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
total--;
displayResult.setText(RESULT+total);
}
});
}
}
Please help. This thing is annoying me.
This is your problem, I dont know how your IDE does not complain about the same:
Try to assign value to
RESULT
in onCreate();Here you can find the official docs for resource usage in Android and how to access resources.