早些时候我的相同代码工作正常,但突然我得到 NULL指针异常,Logcat说行号:251 ,见下文:
ProgressBar progress = (ProgressBar)v.findViewById(R.id.progressBar);
现在我想知道为什么我要获得NPE,而我的相同代码几小时前工作正常?
每当我在 getView(...)中使用 startUpload(位置)方法时面临此问题,否则无法获取,但在过去我使用 startUpload(位置)处于同一位置,但未获得异常,为什么现在呢?
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);
}
}
答案 0 :(得分:2)
这种错误有时可以通过清理项目来解决。
答案 1 :(得分:0)
你知道android中已经有一个已实现的进度对话框吗? 尝试这样的事情,而不是像你一样制作自定义的xml
ProgressDialog pd = new ProgressDialog(context);
pd.setMessage("Please wait ...");
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setTitle("Loading");
pd.show();
你可以像这样结束
pd.dismiss();