gpt4 book ai didi

java - 我正在尝试启动应用程序,但它崩溃了

转载 作者:行者123 更新时间:2023-11-29 04:35:44 27 4
gpt4 key购买 nike

Error:
01-14 20:59:18.266 27103-27103/com.example.android.cricketscore E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.cricketscore, PID: 27103
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.cricketscore/com.example.android.cricketscore.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:2366)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517)
at android.app.ActivityThread.access$800(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:189)
at android.app.ActivityThread.main(ActivityThread.java:5529)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
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.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:116)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:50)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.android.cricketscore.MainActivity.<init>(MainActivity.java:19)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517) 
at android.app.ActivityThread.access$800(ActivityThread.java:162) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:189) 
at android.app.ActivityThread.main(ActivityThread.java:5529) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 

主要 Activity 。 Java

package com.example.android.cricketscore;

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

public class MainActivity extends AppCompatActivity {

int scoreTeamA = 0;

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

//Figure out if Team A has scored One Run
CheckBox oneRunCheckbox = (CheckBox) findViewById(R.id.addOneForTeamA);
boolean isOneRun = oneRunCheckbox.isChecked();

/**Calculates the run of the score.
*
* @return total run
*/

//Add 1 to the score if team A scored 1 run
public void addOneForTeamA(View, view){
scoreTeamA = scoreTeamA + 1;
displayForTeamA(scoreTeamA);
}


/** This method creates a summary of the total score
* @param addOneRun is scored
* @param score summary
*/

/**This method displays the score on the screen. */
public void displayForTeamA(int score){
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.cricketscore.MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:text="Team A"
android:textSize="14sp"
android:id="@+id/textView2" />

<TextView
android:id="@+id/team_a_score"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:text="0/0"
android:textSize="56sp"
android:layout_marginTop="15dp"
android:layout_alignTop="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<CheckBox
android:id="@+id/addOneForTeamA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:text="1 Run"
android:textSize="16sp"
android:layout_below="@+id/team_a_score"
android:layout_margin="16dp" />


</RelativeLayout>

最佳答案

如 LogCat 中所述,您在第 19 行遇到了 NullPointerException:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
...
at com.example.android.cricketscore.MainActivity.<init>(MainActivity.java:19)

线条:

CheckBox oneRunCheckbox = (CheckBox) findViewById(R.id.addOneForTeamA);
boolean isOneRun = oneRunCheckbox.isChecked();

需要在你的 onCreate() 方法中,像这样:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox oneRunCheckbox = (CheckBox) findViewById(R.id.addOneForTeamA);
boolean isOneRun = oneRunCheckbox.isChecked();
}

关于java - 我正在尝试启动应用程序,但它崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41652404/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com