java.lang.RuntimeException错误,无法弄清楚原因

时间:2014-01-17 00:42:11

标签: android runtimeexception

我在我的应用程序的logcat中遇到java.lang.RuntimeException错误。然后它继续显示错误来自:

protected String doInBackground(String... args) {

        // getting updated data from EditTexts
        gps = new GPSTracker(EditProductActivity.this);
        double inputlat = gps.getLatitude();
        double inputlong = gps.getLongitude();

        String inputLat=Double.toString(inputlat);
        String inputLong=Double.toString(inputlong);

        // getting updated data from EditTexts
        String name = txtName.getText().toString();
        String longitude = inputLong;
        String latitude = inputLat;

        Log.d("Get this far?", pid);//something wrong with these when I want to save
        String pavement = spinpavement.getSelectedItem().toString();
        String traffic = spintraffic.getSelectedItem().toString();
        String environment = spinenviro.getSelectedItem().toString();
        //Oh no something is wrong here
        // Building Parameters

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair(TAG_PID, pid));
        params.add(new BasicNameValuePair(TAG_NAME, name));
        params.add(new BasicNameValuePair(TAG_LONG, longitude));
        params.add(new BasicNameValuePair(TAG_LAT, latitude));
        params.add(new BasicNameValuePair(TAG_PAVEMENT, pavement));
        params.add(new BasicNameValuePair(TAG_TRAFFIC, traffic));
        params.add(new BasicNameValuePair(TAG_ENVIRONMENT, environment));

        // sending modified data through http request
        // Notice that update product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_update_product,
                "POST", params);

        // check json success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully updated
                //Intent i = getIntent();
                // send result code 100 to notify about product update
                //setResult(100, i);
                finish();
            } else {
                // failed to update product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

正如您所见,错误来自:

String pavement = spinpavement.getSelectedItem().toString();
        String traffic = spintraffic.getSelectedItem().toString();
        String environment = spinenviro.getSelectedItem().toString();

但我无法弄清楚它有什么问题。

以下是错误开始的整个logcat

01-16 19:20:29.446: E/AndroidRuntime(24403): FATAL EXCEPTION: AsyncTask #2
01-16 19:20:29.446: E/AndroidRuntime(24403): Process: com.example.androidhive, PID: 24403
01-16 19:20:29.446: E/AndroidRuntime(24403): java.lang.RuntimeException: An error occured while executing doInBackground()
01-16 19:20:29.446: E/AndroidRuntime(24403):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at java.lang.Thread.run(Thread.java:841)
01-16 19:20:29.446: E/AndroidRuntime(24403): Caused by: java.lang.NullPointerException
01-16 19:20:29.446: E/AndroidRuntime(24403):    at com.example.androidhive.EditProductActivity$SaveProductDetails.doInBackground(EditProductActivity.java:225)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at com.example.androidhive.EditProductActivity$SaveProductDetails.doInBackground(EditProductActivity.java:1)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-16 19:20:29.446: E/AndroidRuntime(24403):    ... 3 more
01-16 19:20:30.019: E/WindowManager(24403): android.view.WindowLeaked: Activity com.example.androidhive.EditProductActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{421bbd78 V.E..... R.....ID 0,0-684,192} that was originally added here
01-16 19:20:30.019: E/WindowManager(24403):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
01-16 19:20:30.019: E/WindowManager(24403):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
01-16 19:20:30.019: E/WindowManager(24403):     at  android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
01-16 19:20:30.019: E/WindowManager(24403):     at android.app.Dialog.show(Dialog.java:286)
01-16 19:20:30.019: E/WindowManager(24403):     at com.example.androidhive.EditProductActivity$SaveProductDetails.onPreExecute(EditProductActivity.java:202)
01-16 19:20:30.019: E/WindowManager(24403):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
01-16 19:20:30.019: E/WindowManager(24403):     at android.os.AsyncTask.execute(AsyncTask.java:535)
01-16 19:20:30.019: E/WindowManager(24403):     at com.example.androidhive.EditProductActivity.onClick(EditProductActivity.java:87)
01-16 19:20:30.019: E/WindowManager(24403):     at java.lang.reflect.Method.invokeNative(Native Method)
01-16 19:20:30.019: E/WindowManager(24403):     at java.lang.reflect.Method.invoke(Method.java:515)
01-16 19:20:30.019: E/WindowManager(24403):     at android.view.View$1.onClick(View.java:3837)
01-16 19:20:30.019: E/WindowManager(24403):     at android.view.View.performClick(View.java:4452)
01-16 19:20:30.019: E/WindowManager(24403):     at android.view.View$PerformClick.run(View.java:18498)
01-16 19:20:30.019: E/WindowManager(24403):     at android.os.Handler.handleCallback(Handler.java:733)
01-16 19:20:30.019: E/WindowManager(24403):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-16 19:20:30.019: E/WindowManager(24403):     at android.os.Looper.loop(Looper.java:137)
01-16 19:20:30.019: E/WindowManager(24403):     at android.app.ActivityThread.main(ActivityThread.java:5083)
01-16 19:20:30.019: E/WindowManager(24403):     at java.lang.reflect.Method.invokeNative(Native Method)
01-16 19:20:30.019: E/WindowManager(24403):     at java.lang.reflect.Method.invoke(Method.java:515)
01-16 19:20:30.019: E/WindowManager(24403):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-16 19:20:30.019: E/WindowManager(24403):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-16 19:20:30.019: E/WindowManager(24403):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

如果第一行对应于第225行,那么NullPointerException可能是因为在微调器中没有选择任何内容。尝试:

String pavement = (spinpavement!=null)?String.valueOf(spinpavement.getSelectedItem()):"NULL Pavement";
String traffic = (spintraffic!=null)?String.valueOf(spintraffic.getSelectedItem()):"NULL Traffic";
String environment = (spinenviro!=null)?String.valueOf(spinenviro.getSelectedItem()):"NULL Environment";

并且您将能够意识到是否存在某些问题或丢失了对任何成员变量的引用。

希望它有所帮助。

答案 1 :(得分:1)

检查..在获取选定的字符串之前..

if(spinner.getselectedpostion!=0)
{`
`
String selected_string= spinner.getSelectedItem().toString();

}`enter code here`