-6

我成功编译了一个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)

我的 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>

我的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);
			}
		});
	}
}

请帮忙。这件事让我很烦。

4

3 回答 3

4

这条线导致它

public final String RESULT = getResources().getString(R.string.result);

getResources()方法需要context在调用期间可用,运行时没有可用的上下文。

解决方案:

RESULT改为在您的 onCreate 上初始化:

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
于 2015-06-16T10:08:25.323 回答
3

这是你的问题,我不知道你的 IDE 是如何抱怨的:

public final String RESULT = getResources().getString(R.string.result);

尝试RESULT在 onCreate();中赋值

在这里您可以找到有关 Android 资源使用以及如何访问资源的官方文档。

于 2015-06-16T10:08:21.527 回答
1

getResources()需要在执行后创建的上下文。onCreate()

试试这个代码。

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 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);

        RESULT = getResources().getString(R.string.result);

        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);
            }
        });
    }
}
于 2015-06-16T10:29:10.590 回答