1

我是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() 方法上的空指针异常。任何人都可以帮助我更改我的代码以解决问题。提前致谢

我的 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
4

1 回答 1

0

移动EditText message1 = (EditText) findViewById(R.id.editText1);下面的行setContentView(R.layout.activity_main);

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

于 2012-09-24T17:14:50.933 回答