Android应用在点击时崩溃:NullPointerException

时间:2015-09-23 16:58:08

标签: java android android-asynctask nullpointerexception

我正在研究一个Android项目并遇到了问题。我有一个活动,其中有一个“写一个评论”按钮,打开一个对话框,该对话框又有两个按钮“完成”和“不再谢谢”。 “完成”按钮执行AsyncTask,它将数据存储到服务器,但会导致应用程序因NullPointerException错误而崩溃。这是代码:

writeReviewBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try{
                // Create custom dialog object
                final Dialog dialog = new Dialog(GetReviewActivity.this);
                // Include dialog.xml file
                dialog.setContentView(R.layout.write_review_activity);
                // Set dialog title
                dialog.setTitle("Your review is valuable");

                // set values for custom dialog components - text, image and button
                TextView text = (TextView) dialog.findViewById(R.id.tvWR);
                //text.setText("Custom dialog Android example.");
                ImageView image = (ImageView) dialog.findViewById(R.id.smiley);


                dialog.show();

                Button writeButton = (Button) dialog.findViewById(R.id.buttonWR);

                writeButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new WriteReviewAsyncTask().execute();
                    }
                });

                Button declineButton = (Button) dialog.findViewById(R.id.buttonNoThanks);
                // if decline button is clicked, close the custom dialog
                declineButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Close dialog
                        dialog.dismiss();
                    }
                });


            }catch (Exception e){
                e.printStackTrace();
            }
        }
    });

writeButton是“完成”按钮。 这是AsyncTask实施:

public class WriteReviewAsyncTask extends AsyncTask<Void,Void,Void>{

    final EditText etWR = (EditText)findViewById(R.id.etWR);
    String Review = etWR.getText().toString();

    @Override
    protected Void doInBackground(Void... params) {
        ArrayList<NameValuePair> dataToSend = new ArrayList<>();
        try {
            Log.i("HAPPENED: ","Working");
        userLocalStore = new UserLocalStore(GetReviewActivity.this);
        User user = userLocalStore.getLoggedInUser();

        dataToSend.add(new BasicNameValuePair("reviewer_name",user.username));
        dataToSend.add(new BasicNameValuePair("item_name",ItemName));
        dataToSend.add(new BasicNameValuePair("review",Review));

        HttpParams httpParams = getHttpRequestParams();
        HttpClient httpClient = new DefaultHttpClient(httpParams);
        HttpPost httpPost = new HttpPost(SERVER_ADDRESS+"StoreReview.php");


            httpPost.setEntity(new UrlEncodedFormEntity(dataToSend));
            httpClient.execute(httpPost);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        UpdateRecyclerView();
    }

    private HttpParams getHttpRequestParams() {
        HttpParams httpRequestParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpRequestParams,
                CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpRequestParams,
                CONNECTION_TIMEOUT);
        return httpRequestParams;
    }
}

LogCat错误:

09-23 22:12:22.110  30716-30716/app.usrete.jayant.delvemitt E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at app.usrete.jayant.delvemitt.childscreen.getreviews.getreview.GetReviewActivity$WriteReviewAsyncTask.<init>(GetReviewActivity.java:330)
        at app.usrete.jayant.delvemitt.childscreen.getreviews.getreview.GetReviewActivity$2$1.onClick(GetReviewActivity.java:133)
        at android.view.View.performClick(View.java:4475)
        at android.view.View$PerformClick.run(View.java:18786)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
        at dalvik.system.NativeStart.main(Native Method)

请帮帮我们!

1 个答案:

答案 0 :(得分:0)

为什么要在protected Void doInBackground(Void... params)中传递return null 将那个争论传递给onPostExecute(Void params),它不会接受任何Argument,它的类型为Void,所以避免写回null,只需删除该行