我在以下代码中有空指针异常。我是android的新手。请帮助我删除它,并建议一个更好的方法来为游戏中的各种难度级别设置不同尺寸的棋盘。
package com.example.prashant.minesweeper;
import android.content.Context;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int prev_board = 3;
private final static int Easy_Board = 10;
private final static int Medium_Board = 20;
private final static int Hard_Board = 40;
MyButton buttons[][];
LinearLayout rows[];
LinearLayout mainLayout;
TextView tv1,tv2;
public void setLayout(int n) {
if(prev_board==n) {
resetBoard();
return;
}
prev_board = n;
mainLayout.removeAllViews();
rows = new LinearLayout[n+1];
buttons = new MyButton[n][n];
LinearLayout.LayoutParams row_param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,2);
rows[0].setLayoutParams(row_param);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT,1);
tv1 = new TextView(getApplicationContext());
tv1.setLayoutParams(param);
tv1.setText("SCORE : ");
tv2 = new TextView(getApplicationContext());
tv2.setLayoutParams(param);
tv2.setText("0 ");
rows[0].addView(tv1);
rows[0].addView(tv2);
mainLayout.addView(rows[0]);
row_param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1);
param = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT,1);
for(int i=1; i<n+1; i++){
rows[i].setLayoutParams(row_param);
for(int j=0; j<n; j++){
buttons[i-1][j].setLayoutParams(param);
rows[i].addView(buttons[i-1][j]);
}
mainLayout.addView(rows[i]);
}
}
public void resetBoard() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (LinearLayout) findViewById(R.id.activity_main);
setLayout(Easy_Board);
}
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.Easy : setLayout(Easy_Board); break;
case R.id.Medium : setLayout(Medium_Board); break;
case R.id.Hard : setLayout(Hard_Board); break;
case R.id.NewGame: resetBoard(); break;
}
return true;
}
}
这是我的logcat
01-30 13:14:12.938 18751-18751/com.example.prashant.minesweeper E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prashant.minesweeper/com.example.prashant.minesweeper.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.access$700(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5302)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.prashant.minesweeper.MainActivity.setLayout(MainActivity.java:38)
at com.example.prashant.minesweeper.MainActivity.onCreate(MainActivity.java:72)
at android.app.Activity.performCreate(Activity.java:5326)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.access$700(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5302)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)