繁体   English   中英

突然得到空指针异常

[英]suddenly getting Null Pointer Exception

早些时候我的相同代码工作正常,但突然我得到NULL指针异常 ,Logcat说行号:251 ,见下文:

   ProgressBar progress = (ProgressBar)v.findViewById(R.id.progressBar);

现在我想知道为什么我要获得NPE,而我的相同代码几小时前工作正常?

每当我在getView(...)中使用startUpload(position)方法面临这个问题,否则没有得到,但是在过去我在同一位置使用startUpload(位置)但是没有得到Exception ,为什么现在呢?

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);
    }

    }

这种错误有时可以通过清理项目来解决。

你知道android中已经有一个已实现的进度对话框吗? 尝试类似这样的东西,而不是像你那样制作自定义的xml

    ProgressDialog pd = new ProgressDialog(context);
    pd.setMessage("Please wait ...");
    pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    pd.setTitle("Loading");
    pd.show();

你可以像这样结束它

pd.dismiss();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM