如何修复“无法实例化活动”

时间:2019-07-27 05:35:42

标签: java android nullpointerexception

我正在android studio(java)中制作一个简单的剪刀石头布游戏。我遇到了一个问题-当我打开应用程序时,它立即崩溃。

我使用方法'resf'为逻辑创建了一个单独的类,该方法获取信息(石头,纸或剪刀)并返回结果(胜利,失败,平局)。 这是逻辑类:

package com.example.rockpaperscissors;
import android.app.Activity;

import java.util.Random;

public class RPSLogic  {


    public static int resf(int obj){

        Random rnd = new Random(System.currentTimeMillis());
        int resobj = rnd.nextInt(3);
        int res;
        if(obj==resobj){
            res = 0;
        }else {
           if(obj-resobj==1){
               res = 1;
           } else {
               if(obj-resobj==2){
                   res = 1;
               }else {
                   res = 2;
               }
           }
        }
        return res;
    }
}

这是MainActivity类:

    package com.example.rockpaperscissors;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

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

        paper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(RPSLogic.resf(2));
            }
        });
        scissors.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(RPSLogic.resf(1));
            }
        });
        rock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(RPSLogic.resf(0));

            }
        });
    }
    TextView result = (TextView) findViewById(R.id.result);
    Button paper = (Button) findViewById(R.id.paper);
    Button scissors = (Button) findViewById(R.id.scissors);
    Button rock = (Button) findViewById(R.id.rock);
    Button info = (Button) findViewById(R.id.info);

}

这是android清单的一部分:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>

还值得注意的是,布局文件(按钮的ID等)中的所有内容都是正确的

logcat错误:

2019-07-27 10:07:28.200 11344-11344/com.example.rockpaperscissors W/ckpaperscissor: JIT profile information will not be recorded: profile file does not exits.
2019-07-27 10:07:28.288 11344-11344/com.example.rockpaperscissors I/Perf: Connecting to perf service.
2019-07-27 10:07:28.312 11344-11344/com.example.rockpaperscissors I/InstantRun: starting instant run server: is main process
2019-07-27 10:07:28.469 11344-11344/com.example.rockpaperscissors D/AndroidRuntime: Shutting down VM
2019-07-27 10:07:28.474 11344-11344/com.example.rockpaperscissors E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.rockpaperscissors, PID: 11344
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.rockpaperscissors/com.example.rockpaperscissors.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2876)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6810)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.support.v7.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
        at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
        at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
        at com.example.rockpaperscissors.MainActivity.<init>(MainActivity.java:39)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
        at android.support.v4.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1216)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2864)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:201) 
        at android.app.ActivityThread.main(ActivityThread.java:6810) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 
2019-07-27 10:07:28.507 11344-11344/com.example.rockpaperscissors I/Process: Sending signal. PID: 11344 SIG: 9

1 个答案:

答案 0 :(得分:0)

  

在使用它之前启动视图(创建视图对象)(setText操作)。避免 NullPointerException

更正的代码:

testing