我成功编译了一个Android应用程序并创建了它的apk。我还用我的密钥库签署了.apk文件。但是在USB调试模式下运行时,由于某种原因,它会给我一个NullPointerException。
以下是例外:
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)
&#13;
我的AndroidManifest.xml文件:
<?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>
&#13;
我的com.adi.play.PlayActivity
.java文件:
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);
}
});
}
}
&#13;
请帮忙。这件事让我烦恼。
答案 0 :(得分:4)
这一行引起了它
public final String RESULT = getResources().getString(R.string.result);
getResources()
方法在调用期间需要context
,运行时没有可用的上下文。
<强>解决方案:强>
在onCreate上初始化RESULT
:
public class PlayActivity extends Activity {
private String result; //this line
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);
result = getResources().getString(R.string.result); //this line
答案 1 :(得分:3)
这是你的问题,我不知道你的IDE如何不抱怨:
public final String RESULT = getResources().getString(R.string.result);
尝试在onCreate();
中为RESULT
分配值
Here您可以在Android中找到有关资源使用的官方文档以及如何访问资源。
答案 2 :(得分:1)