Android - 获取Null指针的错误

时间:2015-10-20 14:36:28

标签: android

我现在试着寻找答案,我开始感到沮丧。起初,我试图使用findViewById并且我的应用程序一直崩溃,所以在我搜索了一下之后,我发现我在使用setContentView之前尝试使用它们。所以,我解决了这个问题,但是现在它不会让我在我拥有的TextView上使用setText,它给了我同样的错误:

10-20 10:14:19.460: E/AndroidRuntime(1503): FATAL EXCEPTION: main
10-20 10:14:19.460: E/AndroidRuntime(1503): Process:com.goldengear.incremental, PID: 1503
10-20 10:14:19.460: E/AndroidRuntime(1503): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.goldengear.incremental/com.goldengear.incremental.PlayActivity}: java.lang.NullPointerException
10-20 10:14:19.460: E/AndroidRuntime(1503):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-20 10:14:19.460: E/AndroidRuntime(1503):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-20 10:14:19.460: E/AndroidRuntime(1503):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-20 10:14:19.460: E/AndroidRuntime(1503):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-20 10:14:19.460: E/AndroidRuntime(1503):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-20 10:14:19.460: E/AndroidRuntime(1503):     at android.os.Looper.loop(Looper.java:136)
10-20 10:14:19.460: E/AndroidRuntime(1503):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-20 10:14:19.460: E/AndroidRuntime(1503):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 10:14:19.460: E/AndroidRuntime(1503):     at java.lang.reflect.Method.invoke(Method.java:515)
10-20 10:14:19.460: E/AndroidRuntime(1503):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-20 10:14:19.460: E/AndroidRuntime(1503):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-20 10:14:19.460: E/AndroidRuntime(1503):     at dalvik.system.NativeStart.main(Native Method)
10-20 10:14:19.460: E/AndroidRuntime(1503): Caused by: java.lang.NullPointerException
10-20 10:14:19.460: E/AndroidRuntime(1503):     at com.goldengear.incremental.PlayActivity.onCreate(PlayActivity.java:31)
10-20 10:14:19.460: E/AndroidRuntime(1503):     at android.app.Activity.performCreate(Activity.java:5231)
10-20 10:14:19.460: E/AndroidRuntime(1503):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-20 10:14:19.460: E/AndroidRuntime(1503):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-20 10:14:19.460: E/AndroidRuntime(1503):     ... 11 more

这是我的活动:

package com.goldengear.incremental;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class PlayActivity extends ActionBarActivity {

int clicks = 0;
int clickpower = 1;
TextView clickView; 
Button clickbtn; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.menuContainer, new MenuFragment()).commit();
    }

    clickView = (TextView)findViewById(R.id.clickView);
    clickbtn = (Button)findViewById(R.id.clickButton);

    clickView.setText("Hello!");



}
}

以下是调用它的活动(我有一个onClick for play):

package com.goldengear.incremental;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity  {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void play(View v) {

    Intent intent = new Intent(getApplicationContext(), PlayActivity.class);
    startActivity(intent);
}

}

这是Manifest(因为我看到一些提到它可能是问题的东西):

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.goldengear.incremental"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light" >
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".PlayActivity" >
    </activity>
</application>

任何帮助,将不胜感激!谢谢!

0 个答案:

没有答案