2014-04-01 8 views
1

임 정말 내 안드로이드 코드에서 오류가 무엇인지 잘 모릅니다과 사람이 오류가 발생합니다 실행이 방법 후 나에게치명적인 예외 : AsyncTask를 (안드로이드)

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()); 
      } 
     } 

오류 당신은 .. 백그라운드 스레드에서보기를 터치 onPostExecute()에 UI 변경 코드 (글고 치기에 세트 텍스트)을 이동할 수 없습니다

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; 
     } 
    } 

} 
+3

우리에게 logcat stacktrace를 보여주세요. – VM4

+4

할 수없는 doInBackground에서 UI를 변경하고 있습니다. gtoplang.NullPointerException을 변경 한 후 textView.setText ("Hello World");를 onPostExecute()로 이동합니다. – Coderji

+0

그리고 TextView를 이동했습니다 textView = new TextView (pc) 메서드 외부에서 onPostExecute의 textview.settext도 초기화 될 수 있습니다. – user3484436

답변

0

doInBackground 메소드로부터 제공되어야한다 방법

+0

gt java.lang.NullPointerException을 변경 한 후 TextView를 이동했습니다. textView = new TextView (pc) 메서드의 그래서 onPostExecute의 textview.settext도 초기화 될 수 있습니다 .... – user3484436

+0

그리고 실제로 내 textview.settext는 응답에 그것을 설정하고 싶습니다 ~ webservice에서 ~~~ Hello world for just testing /// – user3484436

+0

doInBackground()에서 문자열 또는 결과를 반환하고 onPostExecute()에 전달 된 매개 변수 결과를 사용합니다 ... – elmorabea

관련 문제