早些时候,我的相同代码工作正常,但突然我得到NULL指针异常,Logcat说行号:251,见下文:
ProgressBar progress = (ProgressBar)v.findViewById(R.id.progressBar);
现在我想知道为什么我会得到 NPE,而我的相同代码在几个小时前运行良好?
每当我在 getView(...) 中使用 startUpload(position) 方法时遇到这个问题,否则不会得到,但在过去我使用 >startUpload(position) 在相同的位置,但没有得到异常,那么为什么现在呢?
Logcat:
11-21 05:45:43.822: W/dalvikvm(1559): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-21 05:45:43.872: E/AndroidRuntime(1559): FATAL EXCEPTION: main
11-21 05:45:43.872: E/AndroidRuntime(1559): java.lang.NullPointerException
11-21 05:45:43.872: E/AndroidRuntime(1559): at com.example.demo.UploadActivity$2$1.run(UploadActivity.java:251)
11-21 05:45:43.872: E/AndroidRuntime(1559): at android.os.Handler.handleCallback(Handler.java:730)
11-21 05:45:43.872: E/AndroidRuntime(1559): at android.os.Handler.dispatchMessage(Handler.java:92)
11-21 05:45:43.872: E/AndroidRuntime(1559): at android.os.Looper.loop(Looper.java:137)
11-21 05:45:43.872: E/AndroidRuntime(1559): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-21 05:45:43.872: E/AndroidRuntime(1559): at java.lang.reflect.Method.invokeNative(Native Method)
11-21 05:45:43.872: E/AndroidRuntime(1559): at java.lang.reflect.Method.invoke(Method.java:525)
11-21 05:45:43.872: E/AndroidRuntime(1559): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-21 05:45:43.872: E/AndroidRuntime(1559): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-21 05:45:43.872: E/AndroidRuntime(1559): at dalvik.system.NativeStart.main(Native Method)
代码:-
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_column, null);
}
startUpload(position);
// statusWhenFinish(position, resServer);
return convertView;
}
}
//Upload
public void startUpload(final int position) {
Runnable runnable = new Runnable() {
public void run() {
handler.post(new Runnable() {
public void run() {
View v = lstView.getChildAt(position - lstView.getFirstVisiblePosition());
// Show ProgressBar
ProgressBar progress = (ProgressBar)v.findViewById(R.id.progressBar);
progress.setVisibility(View.VISIBLE);
// Status
TextView status = (TextView)v.findViewById(R.id.ColStatus);
status.setText("Uploading..");
new UploadFileAsync().execute(String.valueOf(position));
}
});
}
};
new Thread(runnable).start();
}
// When UPload Finish
protected void statusWhenFinish(int position, String resServer) {
View v = lstView.getChildAt(position - lstView.getFirstVisiblePosition());
// Show ProgressBar
ProgressBar progress = (ProgressBar)v.findViewById(R.id.progressBar);
progress.setVisibility(View.GONE);
// Status
TextView status = (TextView)v.findViewById(R.id.ColStatus);
/*** Default Value ***/
String strStatusID = "0";
String strError = "";
try {
JSONObject c = new JSONObject(resServer);
strStatusID = c.getString("StatusID");
strError = c.getString("Error");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Prepare Status
if(strStatusID.equals("0"))
{
// When update Failed
status.setText("Already exist" + strError);
status.setTextColor(Color.RED);
// Enabled Button again
Button btnUpload = (Button) v.findViewById(R.id.btnUpload);
btnUpload.setText("Uploaded");
btnUpload.setTextColor(Color.RED);
btnUpload.setEnabled(true);
}
else
{
status.setText("Upload Completed.");
status.setTextColor(Color.GREEN);
}
}
最佳答案
这种错误有时可以通过清理项目来修复。
关于java - 突然出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20114034/