以下程序抛出NullPointerException。在Log cat中显示:
01-09 20:40:34.366: D/RemoteIt(2809): java.lang.NullPointerException
单击该按钮时,它不会转到Mousefragment类。我试图解决它,但我不能 - 如何解决这个问题?
public class connect extends ListActivity implements OnClickListener{
WifiApManager wifiApManager;
TextView tv;
Button ipscan,con;
ListView lv;
EditText tbIp;
private static final String TAG = "RemoteIt";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.connect);
tv =(TextView) findViewById(R.id.iptv);
ipscan=(Button) findViewById(R.id.scan);
con=(Button) findViewById(R.id.connect);
tbIp=(EditText) findViewById(R.id.etIp);
ipscan.setOnClickListener(this);
con.setOnClickListener(this);
lv = getListView();
}
class scan extends AsyncTask<Void, Void, ArrayList<ClientScanResult>>{
public Context context;
public scan(Context c) // constructor to take Context
{
context = c; // Initialize your Context variable
}
protected ArrayList<ClientScanResult> doInBackground(Void... params) {
wifiApManager = new WifiApManager(context); // use the variable here
return wifiApManager.getClientList(false);
}
protected void onPostExecute(ArrayList<ClientScanResult> clients){
CustomArrayAdapter cus = new CustomArrayAdapter(connect.this,clients);
lv.setAdapter(cus);
}
}
@Override
public void onClick(View v) {
// TODO Click Action...
if(v == ipscan){
scan myScan = new scan(this); // pass the context to the constructor
myScan.execute();
}
if(v == con){
onConnectButton();
}
}
private void onConnectButton() {
// TODO When Btn s Clicked...
String ip = this.tbIp.getText().toString();
if (ip.matches("^[0-9]{1,4}\\.[0-9]{1,4}\\.[0-9]{1,4}\\.[0-9]{1,4}$")) {
try {
Settings.setIp(ip);
Intent intent = new Intent(connect.this,MouseFragment.class);
connect.this.startActivity(intent);
//Intent i = new Intent(this, MouseFragment.class);
//this.startActivity(i);
this.finish();
} catch (Exception ex) {
Toast.makeText(this, this.getResources().getText(R.string.invalid_ip), Toast.LENGTH_LONG).show(); **//this toast is displayed**
Log.d(TAG, ex.toString());
}
} else {
//this.tvError.setText("Invalid IP address");
//this.tvError.setVisibility(View.VISIBLE);
Toast.makeText(this, this.getResources().getText(R.string.in_valid), Toast.LENGTH_LONG).show();
}
};
/** OS kills process */
public void onDestroy() {
super.onDestroy();
}
/** App starts anything it needs to start */
public void onStart() {
super.onStart();
}
/** App kills anything it started */
public void onStop() {
super.onStop();
}
}
修改
01-09 21:23:28.663: W/System.err(5657): java.lang.NullPointerException
01-09 21:23:28.665: W/System.err(5657): at com.arul.remoteit.Settings.setIp(Settings.java:67)
01-09 21:23:28.668: W/System.err(5657): at com.arul.remoteit.connect.onConnectButton(connect.java:81)
01-09 21:23:28.668: W/System.err(5657): at com.arul.remoteit.connect.onClick(connect.java:72)
01-09 21:23:28.669: W/System.err(5657): at android.view.View.performClick(View.java:4445)
01-09 21:23:28.670: W/System.err(5657): at android.view.View$PerformClick.run(View.java:18429)
01-09 21:23:28.672: W/System.err(5657): at android.os.Handler.handleCallback(Handler.java:733)
01-09 21:23:28.673: W/System.err(5657): at android.os.Handler.dispatchMessage(Handler.java:95)
01-09 21:23:28.673: W/System.err(5657): at android.os.Looper.loop(Looper.java:136)
01-09 21:23:28.674: W/System.err(5657): at android.app.ActivityThread.main(ActivityThread.java:5081)
01-09 21:23:28.675: W/System.err(5657): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 21:23:28.677: W/System.err(5657): at java.lang.reflect.Method.invoke(Method.java:515)
01-09 21:23:28.678: W/System.err(5657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
01-09 21:23:28.679: W/System.err(5657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 21:23:28.680: W/System.err(5657): at dalvik.system.NativeStart.main(Native Method)
01-09 21:25:34.395: W/IInputConnectionWrapper(5657): getTextBeforeCursor on inactive InputConnection
01-09 21:25:34.513: W/IInputConnectionWrapper(5657): getTextBeforeCursor on inactive InputConnection
单击按钮多少次,使用invalid_ip toast消息显示Exception
答案 0 :(得分:10)
如上所述,您的问题询问如何解决此问题。
你需要弄清楚null
行上的Exception
是什么。为此,您可以查看堆栈跟踪以查看导致问题的行。然后,您可以使用调试器(或一张纸和一支铅笔)逐步执行程序,或者只添加打印语句以确定哪个变量为null
。
当你知道哪个变量是null
时,你可以追溯到程序中找出为什么它是null
。如果您知道原因为null
,则可以解决问题。
答案 1 :(得分:1)
首先,故障排除例外称为调试。其次,有几种方法可以调试。
对于开始,您应该找出导致异常的行。您可以通过查看堆栈跟踪的最顶行来完成此操作。如果这不是您编写的代码的引用,请检查下一行,然后检查下一行,等等 - 直到找到您编写的内容。如果找不到这样的行,请跳到步骤3.
找到该行后,在该行之前添加几个打印语句以打印当前变量。例如:
Log.d("DEBUG", "myVariable = " + myVariable == null ? "null" : myVariable);
然后运行它并检查输出。
最后一步是使用Eclipse Debugger。如果您知道代码中断的行,请通过左键单击行号来添加断点。如果没有,只需在onCreate
的第一行添加断点即可。上面的链接详细说明了如何使用此功能。
还要记住,Android附带了许多调试工具。您可以阅读here。
答案 2 :(得分:0)
关键问题是你在catch
条款中所做的事情。只需将Log.d
更改为exception.printStackTraceprintStackTrace()
,即可获得有关导致问题的行的更多信息。