【问题标题】:Fatal Exception:AsyncTask (Android)致命异常:AsyncTask (Android)
【发布时间】:2014-04-01 09:48:11
【问题描述】:

我真的不知道我的android代码中的错误是什么,希望有人可以帮助我

import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ProductCollection extends Activity {

    private LayoutInflater inflater;
    private ViewGroup container;

    private static final ResourceBundle rb = ResourceBundle.getBundle("com.example.mobile_e_commerce.webserviceurl");

    private final String NAMESPACE = rb.getString("WSDLTargetNamespace");
  private  final String URL = rb.getString("SoapAddress");
   private final String SOAP_ACTION = rb.getString("SoapAction");
    private final String METHOD_NAME = rb.getString("OperationName");


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

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

这个方法执行后会报错!

@Override
        protected void onResume(){
            super.onResume();
            try{
            (new dataLoader()).execute();
            }
            catch(Exception ee)
            {
                 TextView textView = (TextView)findViewById(R.id.Product1);
                 textView.setText(ee.toString());
            }
        }  

错误应该来自doInBackground方法

    class dataLoader extends AsyncTask<TextView,String,TextView>{



            ProductCollection pc = new ProductCollection();

         @Override
          protected TextView doInBackground(TextView... arg0) {
             TextView textView = new TextView(pc);

              textView = (TextView)findViewById(R.id.Product1);     
                SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);



                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

                        SoapEnvelope.VER11);

                        envelope.dotNet = true;

                        envelope.setOutputSoapObject(request);

                        HttpTransportSE httpTransport = new HttpTransportSE("http://192.168.10.12:57491/MEC_WebService.asmx");

                        try
                        {

                        httpTransport.call(SOAP_ACTION, envelope);

                        Object response = envelope.getResponse();

                        textView.setText("Hello World");


                        }

                        catch (Exception exception)
                        {
                                        textView.setText(exception.toString());

                        }
                        return textView;
         }

    //   @Override
        // protected void onPostExecute(TextView response) {

//          response = textView;
    //       super.onPostExecute(response);     
    //}
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.product_collection, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(
                    R.layout.fragment_product_collection, container, false);
            return rootView;
        }
    }

}

【问题讨论】:

  • 向我们展示一个 logcat 堆栈跟踪,先生。
  • 您正在更改 doInBackground 中的 UI,这是您无法做到的。将 textView.setText("Hello World"); 移至 onPostExecute()
  • 在我更改了我的 gt java.lang.NullPointerException 并且我已经将 TextView textView = new TextView(pc) 移到了方法之外,以便 onPostExecute 中的 textview.settext 也可以被初始化...... .

标签: java android android-asynctask


【解决方案1】:

您无法从后台线程触摸视图.. 将更改 UI(将文本设置为 edittext)的代码移动到 onPostExecute() 方法

【讨论】:

  • 在我更改了我的 gt java.lang.NullPointerException 并且我已将 TextView textView = new TextView(pc) 移到方法之外,以便 onPostExecute 中的 textview.settext 也可以初始化... .
  • 实际上我的 textview.settext 想将它设置为我从 webservice 获得的响应~~~Hello world 只是用于测试///
  • 从 doInBackground() 返回字符串或结果,并在 onPostExecute() 中使用传递的参数结果 ...
  • 不好意思再问,但是如何传递来自doInBackground的参数结果///你能给我一个例子吗???
  • 当你扩展 AsyncTask 最后一个类型是从 doInBackgrond() 返回并自动传递给 onPostExeute() 的结果类型,所以你在 doInBackground() 中返回您可以在 onPostExecute() 中接收并使用它所以您可以返回所需的字符串而不是 textview
猜你喜欢
  • 1970-01-01
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 2015-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多