android异步数据发布

时间:2012-09-24 16:49:58

标签: android post android-asynctask

我是Android开发新手。我试图异步发布数据到服务器。我有一个文本视图message1,其数据发布到Web服务。我的代码如下

public class Main extends Activity {

    ProgressDialog dialog;
    final EditText message1 = (EditText)  findViewById(R.id.editText1) ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            //generate GCM id ended
              StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
              StrictMode.setThreadPolicy(policy); 
    }

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

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


                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://xyz.com/imran/msg.php");
                try {
                    // Add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                    nameValuePairs.add(new BasicNameValuePair("message", message1.getText().toString()));


                   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    // Execute HTTP Post Request
                   //Toast.makeText(this, resId, duration)
                    HttpResponse response = httpclient.execute(httppost);
//                  Intent intent = new Intent(Register.this,Emergency.class);
//                  Register.this.startActivity(intent);        



                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }

                // TODO Auto-generated method stub
                return null;
            }

            protected void onPreExecute() {
                dialog = ProgressDialog.show(Main.this, null, "Loading", false);



            }
            @Override
            protected void onPostExecute(String result) {
             dialog.dismiss();
            }

        }

    //define function for doing in background like submit data

        public void add_data()
        {
            Submitdata task = new Submitdata();
            task.execute();
        } 

每次我尝试运行应用程序时,应用程序都会崩溃。我有谷歌问题和很多帖子说这是由于onPostExecute()方法的Null指针异常。任何帮助我,我需要改变我的代码来解决问题。 提前致谢

我的Logcat输出:

09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.os.Looper.loop(Looper.java:137)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.app.ActivityThread.main(ActivityThread.java:4424)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at java.lang.reflect.Method.invokeNative(Native Method)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at java.lang.reflect.Method.invoke(Method.java:511)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at dalvik.system.NativeStart.main(Native Method)
09-24 22:25:32.108: E/AndroidRuntime(18478): Caused by: java.lang.NullPointerException
09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.app.Activity.findViewById(Activity.java:1794)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at com.mpidc2.management.Main.<init>(Main.java:34)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at java.lang.Class.newInstanceImpl(Native Method)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at java.lang.Class.newInstance(Class.java:1319)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
09-24 22:25:32.108: E/AndroidRuntime(18478):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
09-24 22:25:32.108: E/AndroidRuntime(18478):    ... 11 more

1 个答案:

答案 0 :(得分:0)

将行EditText message1 = (EditText) findViewById(R.id.editText1);移至setContentView(R.layout.activity_main);

下方

它崩溃的原因是你在设置Activity的视图之前尝试获取EditText的引用。