我试图运行一个活动,以便将所有文本信息提交到mysql数据库并开始一个新的活动,但我总是很遗憾,应用程序已停止
下面是DonateFood.java活动

public class DonateFood extends AppCompatActivity {

EditText txt_title, txt_desc, txt_amount, txt_date;
Spinner statuss;
Button btnSubmit;
String array;
String title, amount, desc, date, status;

Context con;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_donate_food);

    txt_title = (EditText)findViewById(R.id.txt_title);
    txt_amount = (EditText)findViewById(R.id.txt_amount);
    txt_desc = (EditText)findViewById(R.id.txt_desc);
    txt_date = (EditText)findViewById(R.id.txt_date);
    statuss = (Spinner)findViewById(R.id.status);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);

    statuss.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            array=adapterView.getSelectedItem().toString();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            title =  txt_title.getText().toString();
            amount = txt_amount.getText().toString();
            desc = txt_desc.getText().toString();
            date = txt_date.getText().toString();

            String method = "addFood";

            AddFood addFood = new AddFood(con);
            addFood.execute(method,title,amount,desc,date,array);


        }
    });
}


public class AddFood extends AsyncTask<String, Void, String> {
    Context ctx;

    final String TAG = this.getClass().getName();

    //constructor
    AddFood(Context ctx){
        this.ctx=ctx;
    }

    @Override
    protected String doInBackground(String... params) {

        String food_url = "http://10.0.3.2/MySqlDemo/addFood.php";

        String method = params[0];
        if(method.equals("addFood")){


            String title = params[1];
            String amount = params[2];
            String desc = params[3];
            String date = params[4];
            String array = params[5];

            try {
                URL url = new URL (food_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String data = URLEncoder.encode("title", "UTF-8")+"="+URLEncoder.encode(title, "UTF-8")+ "&"
                        +URLEncoder.encode("amount", "UTF-8")+"="+URLEncoder.encode(amount, "UTF-8")+ "&"
                        +URLEncoder.encode("desc", "UTF-8")+"="+URLEncoder.encode(desc, "UTF-8")+ "&"
                        +URLEncoder.encode("date", "UTF-8")+"="+URLEncoder.encode(date, "UTF-8")+ "&"
                        +URLEncoder.encode("array", "UTF-8")+"="+URLEncoder.encode(array, "UTF-8");
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                inputStream.close();

                return "Food added successfully";


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
        return null;
    }

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

    @Override
    protected void onPostExecute(String result) {
        if (result == null) {
            Toast.makeText(ctx,"result was null", Toast.LENGTH_SHORT).show();
            return;
        }

       else if (result.equals("Food added successfully")) {
            Toast.makeText(ctx, result, Toast.LENGTH_SHORT).show();

            ctx.startActivity(new Intent(ctx, FoodView.class));

        }


    }


}

}
logcat错误:
02-24 17:21:45.465 5871-5871/alsaad.layla.mysqldemo E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  java.lang.NullPointerException
                                                                      at android.widget.Toast.<init>(Toast.java:92)
                                                                      at android.widget.Toast.makeText(Toast.java:238)
                                                                      at alsaad.layla.mysqldemo.DonateFood$AddFood.onPostExecute(DonateFood.java:161)
                                                                      at alsaad.layla.mysqldemo.DonateFood$AddFood.onPostExecute(DonateFood.java:90)
                                                                      at android.os.AsyncTask.finish(AsyncTask.java:631)
                                                                      at android.os.AsyncTask.access$600(AsyncTask.java:177)
                                                                      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                      at android.os.Looper.loop(Looper.java:137)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                      at java.lang.reflect.Method.invokeNative(Native Method)
                                                                      at java.lang.reflect.Method.invoke(Method.java:511)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                      at dalvik.system.NativeStart.main(Native Method)

最佳答案

尝试在您的getApplicationContext()中使用onPostExecute()

Toast.makeText(getApplicationContext(),"result was null", Toast.LENGTH_SHORT).show();

而不是:
Toast.makeText(ctx,"result was null", Toast.LENGTH_SHORT).show();

关于java - 未在mysql数据库中添加postExecute方法URL参数的Android错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42445179/

10-10 20:02