没有调用OnPostExecute中的MyIntent并崩溃我的应用程序?

时间:2013-12-15 18:45:10

标签: android android-intent

我注意到之前已经问过这个问题,我尝试了一些建议但没有成功。这可能是一个拼写错误或范围问题,但我找不到问题。正在发送引号,只有意图调用不起作用时才能看到吐司。

我的QuoteFormActivity.java的一部分

private class AsyncHttpPost extends AsyncTask<String, Void, String> 
    {       

        /*
        Context context;
        private AsyncHttpPost(Context context) {
            this.context = context.getApplicationContext();
        }
        */
        @Override
        protected String doInBackground(String... params)   
        {           
            BufferedReader inBuffer = null;
            String url = "http://www.mydomain.com/submitted_quotes.php";
            String result = "fail";
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost request = new HttpPost(url);
                List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("text_quote", params[0]));
                postParameters.add(new BasicNameValuePair("text_user", params[1]));

                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
                        postParameters);

                request.setEntity(formEntity);
            httpClient.execute(request);
            result="got it";

            } catch(Exception e) {
                // Do something about exceptions
                result = e.getMessage();
            } finally {
                if (inBuffer != null) {
                    try {
                        inBuffer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return  result;
        }

        protected void onPostExecute(String result)
        {       
            Toast toast = Toast.makeText(QuoteFormActivity.this, result, Toast.LENGTH_LONG);
            toast.show();
            //Log.d("test_app","httppost thingy from private class: "+result);
            Intent myIntent = new Intent(QuoteFormActivity.this, DisplayQuoteActivity.class);
            //view.getContext().startActivity(myIntent);
            startActivity(myIntent);
            super.onPostExecute(result);
            //startActivity(new Intent(context, DisplayQuoteActivity.class));

        }   
    }  

[编辑(评论太长)]这是清单中的活动

<activity
        android:name="com.mydomain.quotes.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.mydomain.quotes.DisplayQuoteActivity"
        android:label="@string/title_activity_display_quote" >
    </activity>
    <activity
        android:name="com.mydomain.quotes.QuoteFormActivity"
        android:label="@string/title_activity_quote_form"
        >

    </activity>

编辑堆栈跟踪?

12-17 17:23:55.101: D/test_app(1554): Show Send Quote Page
12-17 17:23:55.981: I/Choreographer(1554): Skipped 75 frames!  The application may be doing too much work on its main thread.
12-17 17:24:16.091: I/Choreographer(1554): Skipped 32 frames!  The application may be doing too much work on its main thread.
12-17 17:24:23.491: D/test_app(1554): Send quote form: test Afz: stack
12-17 17:24:23.902: D/dalvikvm(1554): GC_CONCURRENT freed 184K, 10% free 3059K/3364K, paused 73ms+113ms, total 277ms
12-17 17:24:24.542: D/test_app(1554): httppost thingy from private class: got it
12-17 17:24:24.981: D/AndroidRuntime(1554): Shutting down VM
12-17 17:24:25.021: W/dalvikvm(1554): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
12-17 17:24:25.081: E/AndroidRuntime(1554): FATAL EXCEPTION: main
12-17 17:24:25.081: E/AndroidRuntime(1554): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.quotes/com.mydomain.quotes.DisplayQuoteActivity}: java.lang.NullPointerException
12-17 17:24:25.081: E/AndroidRuntime(1554):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-17 17:24:25.081: E/AndroidRuntime(1554):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-17 17:24:25.081: E/AndroidRuntime(1554):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-17 17:24:25.081: E/AndroidRuntime(1554):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-17 17:24:25.081: E/AndroidRuntime(1554):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 17:24:25.081: E/AndroidRuntime(1554):     at android.os.Looper.loop(Looper.java:137)
12-17 17:24:25.081: E/AndroidRuntime(1554):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-17 17:24:25.081: E/AndroidRuntime(1554):     at java.lang.reflect.Method.invokeNative(Native Method)
12-17 17:24:25.081: E/AndroidRuntime(1554):     at java.lang.reflect.Method.invoke(Method.java:511)
12-17 17:24:25.081: E/AndroidRuntime(1554):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-17 17:24:25.081: E/AndroidRuntime(1554):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-17 17:24:25.081: E/AndroidRuntime(1554):     at dalvik.system.NativeStart.main(Native Method)
12-17 17:24:25.081: E/AndroidRuntime(1554): Caused by: java.lang.NullPointerException
12-17 17:24:25.081: E/AndroidRuntime(1554):     at com.mydomain.quotes.DisplayQuoteActivity.onCreate(DisplayQuoteActivity.java:30)
12-17 17:24:25.081: E/AndroidRuntime(1554):     at android.app.Activity.performCreate(Activity.java:5104)
12-17 17:24:25.081: E/AndroidRuntime(1554):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-17 17:24:25.081: E/AndroidRuntime(1554):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-17 17:24:25.081: E/AndroidRuntime(1554):     ... 11 more

btw:在应用程序设计中,这是一个“谢谢你提交”的好方法,只需将用户重定向到选定的活动。

2 个答案:

答案 0 :(得分:1)

问题似乎是您尝试启动活动的方式。尝试将onPostExecute更改为:

 protected void onPostExecute(String result)
    {       
        Toast toast = Toast.makeText(QuoteFormActivity.this, result, Toast.LENGTH_LONG);
        toast.show();

        QuoteFormActivity.this.startActivity(new Intent(QuoteFormActivity.this, BuiltInCamera.class));
        super.onPostExecute(result);

    }   

答案 1 :(得分:1)

更详细地阅读您的例外情况,您会发现:

DisplayQuoteActivity.onCreate(DisplayQuoteActivity.java:30) NullPointerException

只需检查第30行的DisplayQuoteActivity,您就会发现Android未创建新活动的问题。 startActivity代码工作正常!