我的代码中出现了空指针异常。请帮我解决。 这是我的代码。
package com.example.game;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class GuessActivity extends ListActivity implements OnClickListener
{
EditText GNum;
Button nxt;
int[] a = new int[4];
int[] d = new int[4];
int[] b = {0,0,0,0,0,0,0,0};
int[] c = {0,0,0,0,0,0,0,0};
int[] x = new int[8];
int t = 0;
String [] gN = new String[8];
String [] gB = new String[8];
String [] gC = new String[8];
ListAdapter adapter;
/*ArrayList<HashMap<String, String>> output = new ArrayList<HashMap<String, String>>();
HashMap<String, String> out = new HashMap<String, String>();*/
//String KEY_NUM = "1" , KEY_BULLS="2" , KEY_COWS="3";
private class list
{
TextView guess;
TextView bulls;
TextView cows;
}
list lv = null;
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);
int y = getIntent().getExtras().getInt("num");
for(int i=0;i<4;i++)
{
a[i] = y%10;
y = y/10;
}
/*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});*/
nxt.setOnClickListener(this);
GNum.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if(v.getId() == R.id.btNxt)
{
t++;
if(t>8)
{
Toast.makeText(getApplicationContext(), "You Lost the Game",
Toast.LENGTH_SHORT).show();
return;
}
else
{
gN[t-1] = GNum.getText().toString();
int l = gN[t-1].length();
if(l!=4)
{
Toast.makeText(getApplicationContext(),"Number should be of 4 digits",
Toast.LENGTH_LONG).show();
return;
}
else
{
x[t-1] = Integer.parseInt(gN[t-1]);
for(int i=0;i<4;i++)
{
d[i] = x[t-1]%10;
x[t-1] = x[t-1]/10;
}
if(d[0] == a[0])
b[t-1]++;
if(d[0] == a[1] || d[0] == a[2] || d[0] == a[3])
c[t-1]++;
if(d[1] == a[1])
b[t-1]++;
if(d[1] == a[0] || d[1] == a[2] || d[1] == a[3])
c[t-1]++;
if(d[2] == a[2])
b[t-1]++;
if(d[2] == a[1] || d[2] == a[0] || d[2] == a[3])
c[t-1]++;
if(d[3] == a[3])
b[t-1]++;
if(d[3] == a[1] || d[3] == a[2] || d[3] == a[0])
c[t-1]++;
if(b[t-1] == 4)
{
Toast.makeText(getApplicationContext(), "You Win",
Toast.LENGTH_SHORT).show();
return;
}
gB[t-1] = Integer.toString(b[t-1]);
gC[t-1] = Integer.toString(c[t-1]);
lv = new list();
lv.guess = (TextView) findViewById(R.id.tvGuessNum);
lv.bulls = (TextView) findViewById(R.id.tvBulls);
lv.cows = (TextView) findViewById(R.id.tvCows);
lv.guess.setText(gN[t-1]);
lv.bulls.setText(gB[t-1]);
lv.cows.setText(gC[t-1]);
/*out.put(KEY_NUM, gN[t-1]);
out.put(KEY_BULLS ,String.valueOf(b[t-1]));
out.put(KEY_COWS , String.valueOf(c[t-1]));
output.add(t-1 , out);
setListAdapter(adapter);*/
}
}
}
else if(v.getId()==R.id.etGuessNum)
{
GNum.setText("");
}
}
}
这是我的日志猫。
03-08 02:58:55.760: E/AndroidRuntime(861): FATAL EXCEPTION: main
03-08 02:58:55.760: E/AndroidRuntime(861): Process: com.example.game, PID: 861
03-08 02:58:55.760: E/AndroidRuntime(861): java.lang.NullPointerException
03-08 02:58:55.760: E/AndroidRuntime(861): at com.example.game.GuessActivity.onClick(GuessActivity.java:138)
03-08 02:58:55.760: E/AndroidRuntime(861): at android.view.View.performClick(View.java:4438)
03-08 02:58:55.760: E/AndroidRuntime(861): at android.view.View$PerformClick.run(View.java:18422)
03-08 02:58:55.760: E/AndroidRuntime(861): at android.os.Handler.handleCallback(Handler.java:733)
03-08 02:58:55.760: E/AndroidRuntime(861): at android.os.Handler.dispatchMessage(Handler.java:95)
03-08 02:58:55.760: E/AndroidRuntime(861): at android.os.Looper.loop(Looper.java:136)
03-08 02:58:55.760: E/AndroidRuntime(861): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-08 02:58:55.760: E/AndroidRuntime(861): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 02:58:55.760: E/AndroidRuntime(861): at java.lang.reflect.Method.invoke(Method.java:515)
03-08 02:58:55.760: E/AndroidRuntime(861): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-08 02:58:55.760: E/AndroidRuntime(861): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-08 02:58:55.760: E/AndroidRuntime(861): at dalvik.system.NativeStart.main(Native Method)
第138行 - &gt; if - &gt; else - &gt; else - &gt; lv.bulls.setText(gB[t-1];
答案 0 :(得分:0)
可能是因为您没有在任何地方实例化lv
。它在开始时初始化为null,在第138行使用相同的值。
答案 1 :(得分:0)
您的lv.bulls
可能是null
。请确保您的布局中TextView
带有id
= tvBulls
(区分大小写)。