我遇到空指针异常的问题。请说明一些一般情况。
这是我的代码:
public class GuessActivity extends ListActivity implements OnClickListener
{
EditText GNum;
Button nxt;
int[] a = new int[4];
int[] d = new int[4];
int t = 0;
ArrayList<HashMap<String, String>> output = new ArrayList<HashMap<String, String>>();
HashMap<String, String> out = new HashMap<String, String>();
String KEY_NUM , KEY_BULLS , KEY_COWS;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.guess_activity);
GNum = (EditText) findViewById(R.id.etGuessNum);
nxt = (Button) findViewById(R.id.btNxt);
a = getIntent().getExtras().getIntArray("num");
nxt.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if(v.getId() == R.id.btNxt)
{
String gN = GNum.getText().toString();
int l = gN.length();
int b =0;
int c =0;
if(l!=4)
{
Toast.makeText(getApplicationContext(),"Number should be of 4 digits",
Toast.LENGTH_LONG).show();
}
else
{
t++;
int x = Integer.parseInt(gN);
for(int i=0;i<4;i++)
{
d[i] = x%10;
x = x/10;
}
if(t>8)
Toast.makeText(getApplicationContext(), "You Lost the Game",
Toast.LENGTH_SHORT).show();
else
{
if(d[0] == a[0])
b++;
if(d[0] == a[1] || d[0] == a[2] || d[0] == a[3])
c++;
if(d[1] == a[1])
b++;
if(d[1] == a[0] || d[1] == a[2] || d[1] == a[3])
c++;
if(d[2] == a[2])
b++;
if(d[2] == a[1] || d[2] == a[0] || d[2] == a[3])
c++;
if(d[3] == a[3])
b++;
if(d[3] == a[1] || d[3] == a[2] || d[3] == a[0])
c++;
if(b == 4)
Toast.makeText(getApplicationContext(), "You Win",
Toast.LENGTH_SHORT).show();
}
out.put(KEY_NUM , gN);
out.put(KEY_BULLS ,String.valueOf(b));
out.put(KEY_COWS , String.valueOf(c));
output.add(out);
ListAdapter adapter = new SimpleAdapter(this, output,
R.layout.list_item,
new String[] { KEY_NUM , KEY_BULLS , KEY_COWS}, new int[] {
R.id.tvGuessNum , R.id.tvBulls ,R.id.tvCows });
setListAdapter(adapter);
ListView lv = getListView();
}
}
}
}
这是堆栈跟踪:
03-04 06:32:58.960: W/dalvikvm(844): threadid=1: thread exiting with uncaught exception (group=0xb3ab2ba8)
03-04 06:32:58.970: E/AndroidRuntime(844): FATAL EXCEPTION: main
03-04 06:32:58.970: E/AndroidRuntime(844): Process: com.example.game, PID: 844
03-04 06:32:58.970: E/AndroidRuntime(844): java.lang.NullPointerException
03-04 06:32:58.970: E/AndroidRuntime(844): at com.example.game.GuessActivity.onClick(GuessActivity.java:66)
03-04 06:32:58.970: E/AndroidRuntime(844): at android.view.View.performClick(View.java:4438)
03-04 06:32:58.970: E/AndroidRuntime(844): at android.view.View$PerformClick.run(View.java:18422)
03-04 06:32:58.970: E/AndroidRuntime(844): at android.os.Handler.handleCallback(Handler.java:733)
03-04 06:32:58.970: E/AndroidRuntime(844): at android.os.Handler.dispatchMessage(Handler.java:95)
03-04 06:32:58.970: E/AndroidRuntime(844): at android.os.Looper.loop(Looper.java:136)
03-04 06:32:58.970: E/AndroidRuntime(844): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-04 06:32:58.970: E/AndroidRuntime(844): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 06:32:58.970: E/AndroidRuntime(844): at java.lang.reflect.Method.invoke(Method.java:515)
03-04 06:32:58.970: E/AndroidRuntime(844): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-04 06:32:58.970: E/AndroidRuntime(844): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-04 06:32:58.970: E/AndroidRuntime(844): at dalvik.system.NativeStart.main(Native Method)
第66行采用onClick
方法:d[i] = x % 10
答案 0 :(得分:2)
看起来EditText
(R.id.etGuessNum)中没有值。
您应该清理输入以避免空引用。我还建议您检查他们输入的值是否为有效数字。
我还可以通过naming conventions快速阅读Java。
答案 1 :(得分:0)
您的3个字符串KEY_NUM, KEY_BULLS, KEY_COWS
为空。
此外,您确定a
不为空吗? a = getIntent().getExtras().getIntArray("num");
可能会返回null。
更改
if(l!=4)
{
Toast.makeText(getApplicationContext(),"Number should be of 4 digits",
Toast.LENGTH_LONG).show();
}
到
if(l!=4)
{
Toast.makeText(getApplicationContext(),"Number should be of 4 digits",
Toast.LENGTH_LONG).show();
return;
}
答案 2 :(得分:-1)
改变这个:
String gN = GNum.getText()。toString(); //可以为null
为:
String gN = GNum.getText().toString(); //can be null
if(gN == null)
return;
int l = gN.length(); //如果编辑文本为空,则为空