这是代码MainActivity.java文件。我认为这段代码应该没有错误。但它显示了消息
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference.
我不知道为什么它在那里出现?请帮我弄清楚。
package com.example.android.practiceset2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.*;
import android.view.*;`
public class MainActivity extends AppCompatActivity {
int score=0,wickets=0;
String value=null,number=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button b1=(Button)findViewById(R.id.b_i1);//b_i1 stands for increase in 1 run taken by team B and similarly other names are to the ids of different button.
Button b2=(Button)findViewById(R.id.b_i2);
Button b3=(Button)findViewById(R.id.b_i3);
Button b4=(Button)findViewById(R.id.b_i4);
Button b6=(Button)findViewById(R.id.b_i6);
Button bw=(Button)findViewById(R.id.b_w);
Button bwide=(Button)findViewById(R.id.b_wide);
Button a1=(Button)findViewById(R.id.a_i1);
Button a2=(Button)findViewById(R.id.a_i2);
Button a3=(Button)findViewById(R.id.a_i3);
Button a4=(Button)findViewById(R.id.a_i4);
Button a6=(Button)findViewById(R.id.a_i6);
Button aw=(Button)findViewById(R.id.a_w);
Button awide=(Button)findViewById(R.id.a_wide);
TextView ta= (TextView)findViewById(R.id.a);
TextView tb= (TextView)findViewById(R.id.b);
public void aorb(View view{
if(getResources().getResourceEntryName((view.getId())).startsWith("a"))
{ value="a";}
else{value="b";}
number=getResources().getResourceEntryName((view.getId())).substring(2);
score();}
public void a_enabled(Boolean b)
{
a1.setEnabled(b);
a2.setEnabled(b);
a3.setEnabled(b);
a4.setEnabled(b);
a6.setEnabled(b);
aw.setEnabled(b);
awide.setEnabled(b);
}
public void b_enabled(Boolean b)
{
b1.setEnabled(b);
b2.setEnabled(b);
b3.setEnabled(b);
b4.setEnabled(b);
b6.setEnabled(b);
bw.setEnabled(b);
bwide.setEnabled(b);
}
public void display(String s){
if(value.equals("a"))
{
ta.setText(s);
b_enabled(false);
a_enabled(true);
}
else {
tb.setText(s);
a_enabled(false);
b_enabled(true);
}
}
public void score(){
int i=0;
if(number.equals("i1"))
i=1;
if(number.equals("i2"))
i=2;
if(number.equals("i3"))
i=3;
if(number.equals("i4"))
i=4;
if(number.equals("i6"))
i=6;
if(number.equals("wide"))
i=1;
score+=i;
if(number.equals("w"))
wickets+=1;
if(wickets==10)//if a team loses all its 10 wickets , the score is finalised and next team is invited for play
{
display(score+"/"+wickets);
if(value.equals("a"))
{
value="b";
}
else{value="a";}
score=0;
wickets=0;
display(score+"/"+wickets);
}
display(score+"/"+wickets);
}
public void reset(View view){
ta.setText("0/0");
tb.setText("0/0");
a_enabled(true);
b_enabled(true);
}}
这是我的content_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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Team A"
android:textAlignment="center"
android:fontFamily="sans-serif-medium"
android:textSize="20sp"
android:textColor="#616161"
android:layout_margin="16dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="0/0"
android:textAlignment="center"
android:fontFamily="sans-serif-light"
android:textSize="56dp"
android:textColor="#000000"
android:id="@+id/a"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="aorb"
android:id="@+id/a_i1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:onClick="aorb"
android:id="@+id/a_i3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:onClick="aorb"
android:id="@+id/a_i6"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:onClick="aorb"
android:id="@+id/a_i2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:onClick="aorb"
android:id="@+id/a_i4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Out"
android:onClick="aorb"
android:id="@+id/a_w"
/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:text="Wide"
android:onClick="aorb"
android:id="@+id/a_wide"/>
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"></View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Team B"
android:textAlignment="center"
android:fontFamily="sans-serif-medium"
android:textSize="20sp"
android:textColor="#616161"
android:layout_margin="16dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="0/0"
android:textAlignment="center"
android:fontFamily="sans-serif-light"
android:textSize="56dp"
android:textColor="#000000"
android:id="@+id/b"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="aorb"
android:id="@+id/b_i1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:onClick="aorb"
android:id="@+id/b_i3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:onClick="aorb"
android:id="@+id/b_i6"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:onClick="aorb"
android:id="@+id/b_i2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:onClick="aorb"
android:id="@+id/b_i4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Out"
android:onClick="aorb"
android:id="@+id/b_w"
/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:text="Wide"
android:onClick="aorb"
android:id="@+id/b_wide"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:onClick="reset"
android:layout_marginBottom="32dp"
/>
</RelativeLayout>
这是它的图片,看起来应该像it is the screenshot the preview
答案 0 :(得分:0)
所有这些findViewById
必须包含在您的onCreate()
方法中。您可以将声明排除在onCreate()
之外。您的视图在setContentView(R.layout.activity_main);
我的意思是在onCreate()
之前您可以拥有Button b1;
并且在setContentView(R.layout.activity_main);
中的onCreate()
之后,您必须拥有b1=(Button)findViewById(R.id.b_i1);
每个视图。
答案 1 :(得分:0)
我已经在您中更正了,现在该应用程序没有崩溃。尝试使用下面的代码段
MainActivity
public class MainActivity extends AppCompatActivity {
int score = 0, wickets = 0;
String value = null, number = null;
Button b1;
Button b2;
Button b3;
Button b4;
Button b6;
Button bw;
Button bwide;
Button a1;
Button a2;
Button a3;
Button a4;
Button a6;
Button aw;
Button awide;
TextView ta;
TextView tb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
b1 = (Button) findViewById(R.id.b_i1);//b_i1 stands for increase in 1 run taken by team B and similarly other names are to the ids of different button.
b2 = (Button) findViewById(R.id.b_i2);
b3 = (Button) findViewById(R.id.b_i3);
b4 = (Button) findViewById(R.id.b_i4);
b6 = (Button) findViewById(R.id.b_i6);
bw = (Button) findViewById(R.id.b_w);
bwide = (Button) findViewById(R.id.b_wide);
a1 = (Button) findViewById(R.id.a_i1);
a2 = (Button) findViewById(R.id.a_i2);
a3 = (Button) findViewById(R.id.a_i3);
a4 = (Button) findViewById(R.id.a_i4);
a6 = (Button) findViewById(R.id.a_i6);
aw = (Button) findViewById(R.id.a_w);
awide = (Button) findViewById(R.id.a_wide);
ta = (TextView) findViewById(R.id.a);
tb = (TextView) findViewById(R.id.b);
}
public void aorb(View view) {
if (getResources().getResourceEntryName((view.getId())).startsWith("a")) {
value = "a";
} else {
value = "b";
}
number = getResources().getResourceEntryName((view.getId())).substring(2);
score();
}
public void a_enabled(Boolean b) {
a1.setEnabled(b);
a2.setEnabled(b);
a3.setEnabled(b);
a4.setEnabled(b);
a6.setEnabled(b);
aw.setEnabled(b);
awide.setEnabled(b);
}
public void b_enabled(Boolean b) {
b1.setEnabled(b);
b2.setEnabled(b);
b3.setEnabled(b);
b4.setEnabled(b);
b6.setEnabled(b);
bw.setEnabled(b);
bwide.setEnabled(b);
}
public void display(String s) {
if (value.equals("a")) {
ta.setText(s);
b_enabled(false);
a_enabled(true);
} else {
tb.setText(s);
a_enabled(false);
b_enabled(true);
}
}
public void score() {
int i = 0;
if (number.equals("i1"))
i = 1;
if (number.equals("i2"))
i = 2;
if (number.equals("i3"))
i = 3;
if (number.equals("i4"))
i = 4;
if (number.equals("i6"))
i = 6;
if (number.equals("wide"))
i = 1;
score += i;
if (number.equals("w"))
wickets += 1;
if (wickets == 10)//if a team loses all its 10 wickets , the score is finalised and next team is invited for play
{
display(score + "/" + wickets);
if (value.equals("a")) {
value = "b";
} else {
value = "a";
}
score = 0;
wickets = 0;
display(score + "/" + wickets);
}
display(score + "/" + wickets);
}
public void reset(View view) {
ta.setText("0/0");
tb.setText("0/0");
a_enabled(true);
b_enabled(true);
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:fontFamily="sans-serif-medium"
android:text="Team A"
android:textAlignment="center"
android:textColor="#616161"
android:textSize="20sp" />
<TextView
android:id="@+id/a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:fontFamily="sans-serif-light"
android:text="0/0"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="56dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/a_i1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="1" />
<Button
android:id="@+id/a_i3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="3" />
<Button
android:id="@+id/a_i6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/a_i2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="2" />
<Button
android:id="@+id/a_i4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="4" />
<Button
android:id="@+id/a_w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="Out" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/a_wide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="aorb"
android:text="Wide"
android:textAlignment="center" />
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"></View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:fontFamily="sans-serif-medium"
android:text="Team B"
android:textAlignment="center"
android:textColor="#616161"
android:textSize="20sp" />
<TextView
android:id="@+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:fontFamily="sans-serif-light"
android:text="0/0"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="56dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/b_i1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="1" />
<Button
android:id="@+id/b_i3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="3" />
<Button
android:id="@+id/b_i6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/b_i2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="2" />
<Button
android:id="@+id/b_i4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="4" />
<Button
android:id="@+id/b_w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="Out" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/b_wide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="aorb"
android:text="Wide"
android:textAlignment="center" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:onClick="reset"
android:text="Reset"
android:textAlignment="center" />
</RelativeLayout>
</RelativeLayout>
希望这会有所帮助。