我的应用程序正在使用模拟器,但不适用于真实设备

时间:2018-05-14 10:12:18

标签: android nullpointerexception android-emulator

我正在使用简单的asynctask函数通过json从mysql数据库中获取值。它与模拟器工作正常但如果我从移动设备尝试我收到错误。比如Java.lang.NullPointerExceprtion:Attempt to invke virtual metho 'java.lang.string.java.lang.stringbuilder.toString() on a null object reference

我尝试了新项目但结果是一样的。此应用程序不适用于除模拟器之外的所有设备。你能帮我解决这个问题。

我的代码是 -

public class MainActivity extends AppCompatActivity {

    private static final String Latest_Products7 = "Questions";
    JSONArray productsArray7 = null;

    public static final int CONNECTION_TIMEOUT7=100000;
    public static final int READ_TIMEOUT7=150000;

    HashMap<String,ArrayList<WorldPopulation>> hasmap = new HashMap<String,ArrayList<WorldPopulation>>();
    ArrayList<WorldPopulation> arraylist7 = null;



    StringBuilder result7;

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

    new AsyncLogin7().execute();
}


private class AsyncLogin7 extends AsyncTask<String, String, StringBuilder> {
    ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
    HttpURLConnection conn7;
    URL url7 = null;

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

        pdLoading.setMessage("\tLoading...");
        pdLoading.setCancelable(false);
        pdLoading.show();

    }

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

            // Enter URL address where your php file resides

            url7 = new URL("http:/Samplesite/****/somephp.php");

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        try {
            // Setup HttpURLConnection class to send and receive data from php and mysql
            conn7 = (HttpURLConnection)url7.openConnection();
            conn7.setReadTimeout(READ_TIMEOUT7);
            conn7.setConnectTimeout(CONNECTION_TIMEOUT7);
            conn7.setRequestMethod("POST");

            // setDoInput and setDoOutput method depict handling of both send and receive
            conn7.setDoInput(true);
            conn7.setDoOutput(true);


            // Append parameters to URL
            Uri.Builder builder7 = new Uri.Builder().appendQueryParameter("reg_id", "hai") ;
            String query7 = builder7.build().getEncodedQuery();

            // Open connection for sending data
            OutputStream os7 = conn7.getOutputStream();
            BufferedWriter writer7 = new BufferedWriter(new OutputStreamWriter(os7, "UTF-8"));
            writer7.write(query7);
            writer7.flush();
            writer7.close();
            os7.close();
            conn7.connect();

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();

        }

        try {


            int response_code7 = conn7.getResponseCode();

            // Check if successful connection made
            if (response_code7 == HttpURLConnection.HTTP_OK) {

                // Read data sent from server
                InputStream input7 = conn7.getInputStream();
                BufferedReader reader7 = new BufferedReader(new InputStreamReader(input7));
                result7 = new StringBuilder();
                String line7;
                while ((line7 = reader7.readLine()) != null) {

                    result7.append(line7);
                }


                // Pass data to onPostExecute method

            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            conn7.disconnect();
        }

        return result7;
    }

    @Override
    protected void onPostExecute(StringBuilder result7) {
        super.onPostExecute(result7);

        Log.e("dai",result7.toString());

        Toast.makeText(MainActivity.this,result7.toString(),Toast.LENGTH_LONG).show();
        pdLoading.dismiss();

           /* Intent intnt = new Intent(Checklist_activity.this,Task_main.class);
            intnt.putExtra("task",hasmap);
            startActivity(intnt);*/

        }
    }
}

2 个答案:

答案 0 :(得分:0)

更改

try {


            int response_code7 = conn7.getResponseCode();

            // Check if successful connection made
            if (response_code7 == HttpURLConnection.HTTP_OK) {

                // Read data sent from server
                InputStream input7 = conn7.getInputStream();
                BufferedReader reader7 = new BufferedReader(new InputStreamReader(input7));
                result7 = new StringBuilder();
                String line7;
                while ((line7 = reader7.readLine()) != null) {

                    result7.append(line7);
                }


                // Pass data to onPostExecute method

            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            conn7.disconnect();
        }

        return result7;

try {


            int response_code7 = conn7.getResponseCode();
            result7 = new StringBuilder();
            // Check if successful connection made
            if (response_code7 == HttpURLConnection.HTTP_OK) {

                // Read data sent from server
                InputStream input7 = conn7.getInputStream();
                BufferedReader reader7 = new BufferedReader(new InputStreamReader(input7));
                String line7;
                while ((line7 = reader7.readLine()) != null) {

                    result7.append(line7);
                }


                // Pass data to onPostExecute method

            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            conn7.disconnect();
        }

        return result7;

答案 1 :(得分:0)

尝试这样的事情

Log.e("dai",MainActivity.this.result7.toString());

    Toast.makeText(MainActivity.this,MainActivity.this.result7.toString(),Toast.LENGTH_LONG).show();

OR

@Override
protected void onPostExecute(StringBuilder result) {
    super.onPostExecute(result);

    Log.e("dai",result.toString());

    Toast.makeText(MainActivity.this,result.toString(),Toast.LENGTH_LONG).show();
    pdLoading.dismiss();

       /* Intent intnt = new Intent(Checklist_activity.this,Task_main.class);
        intnt.putExtra("task",hasmap);
        startActivity(intnt);*/

    }
}