我正在开发我的第一个应用程序,当我在手机上运行时,我一直收到此错误。 我正在尝试制作一个tic tac toe的应用程序。 我是一个完整的编程初学者,请帮帮我。
Main_activity.java :
package com.example.test3;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
char[][] a;
char b;
int j,k,o=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(j=0;j<3;j++)
{for(k=0;k<3;k++)
{
a[j][k]=' ';
}
}
Button btn1 = (Button)findViewById(R.id.Btn1);
Button btn2 = (Button)findViewById(R.id.Btn2);
Button btn3 = (Button)findViewById(R.id.Btn3);
Button btn4 = (Button)findViewById(R.id.Btn4);
Button btn5 = (Button)findViewById(R.id.Btn5);
Button btn6 = (Button)findViewById(R.id.Btn6);
Button btn7 = (Button)findViewById(R.id.Btn7);
Button btn8 = (Button)findViewById(R.id.Btn8);
Button btn9 = (Button)findViewById(R.id.Btn9);
for(int i=0;i<9;i++)
{
if(i%2==0) {b='X';}
else {b='O';}
btn1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{TextView op1 = (TextView)findViewById(R.id.Btn1);
op1.setText(b);a[0][0]=b;j=0;k=0;
check(a,j,k,o);
}});
btn2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{TextView op2 = (TextView)findViewById(R.id.Btn2);
op2.setText(b);a[0][1]=b;j=0;k=1;
check(a,j,k,o);
}});
btn3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{TextView op3 = (TextView)findViewById(R.id.Btn3);
op3.setText(b);a[0][2]=b;j=0;k=2;
check(a,j,k,o);
}});
btn4.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{TextView op4 = (TextView)findViewById(R.id.Btn4);
op4.setText(b);a[1][0]=b;j=1;k=0;
check(a,j,k,o);
}});
btn5.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{TextView op5 = (TextView)findViewById(R.id.Btn5);
op5.setText(b);a[1][1]=b;j=1;k=1;
check(a,j,k,o);
}});
btn6.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{TextView op6 = (TextView)findViewById(R.id.Btn6);
op6.setText(b);a[1][2]=b;j=1;k=2;
check(a,j,k,o);
}});
btn7.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{TextView op7 = (TextView)findViewById(R.id.Btn7);
op7.setText(b);a[2][0]=b;j=2;k=0;
check(a,j,k,o);
}});
btn8.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{TextView op8 = (TextView)findViewById(R.id.Btn8);
op8.setText(b);a[2][1]=b;j=2;k=1;
check(a,j,k,o);
}});
btn9.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{TextView op9 = (TextView)findViewById(R.id.Btn9);
op9.setText(b);a[2][2]=b;j=2;k=2;
check(a,j,k,o);
}});
if(o!=0) {i=9;}
}
}
public void check(char[][] a,int j,int k,int o)
{
if(a[j][0]==a[j][1]&&a[j][1]==a[j][2]&&a[j][0]!=' ') o+=1;
if(a[0][k]==a[1][k]&&a[1][k]==a[2][k]&&a[0][k]!=' ') o+=1;
if(((a[0][0]==a[1][1]&&a[1][1]==a[2][2])||(a[0][2]==a[1][1]&&a[1][1]==a[2][0]))&&a[1][1]!=' ') o+=1;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml中:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:id="@+id/TableLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TableRow
android:id="@+id/TableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:text="@string/Btn1"
android:id="@+id/Btn1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="@string/Btn2"
android:id="@+id/Btn2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="@string/Btn3"
android:id="@+id/Btn3"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</TableRow>
<TableRow
android:id="@+id/TableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:text="@string/Btn4"
android:id="@+id/Btn4"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="@string/Btn5"
android:id="@+id/Btn5"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="@string/Btn6"
android:id="@+id/Btn6"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</TableRow>
<TableRow
android:id="@+id/TableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:text="@string/Btn7"
android:id="@+id/Btn7"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="@string/Btn8"
android:id="@+id/Btn8"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="@string/Btn9"
android:id="@+id/Btn9"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</TableRow>
</TableLayout>
</ScrollView>
logcat的:
03-18 00:25:39.138: I/Process(17623): Sending signal. PID: 17623 SIG: 9
03-18 00:25:51.378: D/AndroidRuntime(17794): Shutting down VM
03-18 00:25:51.378: W/dalvikvm(17794): threadid=1: thread exiting with uncaught exception (group=0x41db9ba8)
03-18 00:25:51.378: E/AndroidRuntime(17794): FATAL EXCEPTION: main
03-18 00:25:51.378: E/AndroidRuntime(17794): Process: com.example.test3, PID: 17794
03-18 00:25:51.378: E/AndroidRuntime(17794): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test3/com.example.test3.MainActivity}: java.lang.NullPointerException
03-18 00:25:51.378: E/AndroidRuntime(17794): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-18 00:25:51.378: E/AndroidRuntime(17794): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-18 00:25:51.378: E/AndroidRuntime(17794): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-18 00:25:51.378: E/AndroidRuntime(17794): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-18 00:25:51.378: E/AndroidRuntime(17794): at android.os.Handler.dispatchMessage(Handler.java:102)
03-18 00:25:51.378: E/AndroidRuntime(17794): at android.os.Looper.loop(Looper.java:136)
03-18 00:25:51.378: E/AndroidRuntime(17794): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-18 00:25:51.378: E/AndroidRuntime(17794): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 00:25:51.378: E/AndroidRuntime(17794): at java.lang.reflect.Method.invoke(Method.java:515)
03-18 00:25:51.378: E/AndroidRuntime(17794): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-18 00:25:51.378: E/AndroidRuntime(17794): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-18 00:25:51.378: E/AndroidRuntime(17794): at dalvik.system.NativeStart.main(Native Method)
03-18 00:25:51.378: E/AndroidRuntime(17794): Caused by: java.lang.NullPointerException
03-18 00:25:51.378: E/AndroidRuntime(17794): at com.example.test3.MainActivity.onCreate(MainActivity.java:22)
03-18 00:25:51.378: E/AndroidRuntime(17794): at android.app.Activity.performCreate(Activity.java:5231)
03-18 00:25:51.378: E/AndroidRuntime(17794): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-18 00:25:51.378: E/AndroidRuntime(17794): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-18 00:25:51.378: E/AndroidRuntime(17794): ... 11 more
03-18 00:25:56.948: I/Process(17794): Sending signal. PID: 17794 SIG: 9
答案 0 :(得分:0)
我发现你正在使用你的数组:
a[j][k]=' ';
在为它分配内存之前。在尝试访问元素之前,您需要为阵列分配内存。
你需要做类似的事情:
a = new char[3][3];
答案 1 :(得分:0)
在向数组插入任何值之前声明您的a[][]
大小,如下所示...
char[][] a = new char[3][3];
因为,您没有为a[][]
数组定义任何大小,所以当您尝试将任何值添加到该数组时,它无法在该数组中找到插入值的位置。这就是为什么,它抛出这个Exception
......
java.lang.NullPointerException