0

내 activity_main.xml ScrollView에 양식을 생성하고 싶습니다. XML을로드하고 올바르게 구문 분석하지만 addView (LinearLayout)를 시도하면 예외 e가 발생합니다. 내 응용 프로그램은 푸시 알림을 통해 XML 파일의 URL을 가져 와서 파싱합니다. XML에 따르면 양식을 생성하여 사용자에게 표시해야합니다. https://www.ibm.com/developerworks/xml/tutorials/x-andddyntut/#l1 다음XML의 동적 안드로이드 폼

내 주요 활동 :

public class MainActivity extends Activity { 
// label to display gcm messages 
TextView lblMessage; 
Controller aController; 
public ScrollView sv; 
Button execute; 

// Asyntask 
AsyncTask<Void, Void, Void> mRegisterTask; 

public static String name; 
public static String email; 

final Context context = this; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ScrollView sv = (ScrollView) findViewById(R.id.sv); 

... 
} 

// Create a broadcast receiver to get message and show on screen 
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     String newMessage = intent.getExtras().getString(Config.EXTRA_MESSAGE); 

     // Waking up mobile if it is sleeping 
     aController.acquireWakeLock(getApplicationContext()); 

     ScrollView sv = (ScrollView) findViewById(R.id.sv); 
     new DoInBackground(getApplicationContext(), sv).execute(newMessage); 

     // Releasing wake lock 
     aController.releaseWakeLock(); 

    } 
}; 

여기 내 비동기 클래스입니다 : 나는 주요 활동의 맥락과도 생각

public class DoInBackground extends AsyncTask<String, Void, Void> { 

Context mContext; 
ScrollView mSv; 

String tag = "DynamicFormXML"; 
XmlGuiForm theForm; 
ProgressDialog progressDialog; 
Handler progressHandler; 

public DoInBackground(Context context, ScrollView sv) { 

    this.mContext = context; 
    this.mSv = sv; 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 

} 

@Override 
protected Void doInBackground(String... params) { 

    if (GetFormData(params[0])) { 
     DisplayForm(); 
    } 
    else 
    { 
     Log.e(tag,"Couldn't parse the Form."); 
     AlertDialog.Builder bd = new AlertDialog.Builder(mContext); 
     AlertDialog ad = bd.create(); 
     ad.setTitle("Error"); 
     ad.setMessage("Could not parse the Form data"); 
     ad.show(); 

    } 
    return null; 
} 


protected void onPostExecute() { 

} 

private boolean DisplayForm() 
{ 

    try 
    {   
     final LinearLayout ll = new LinearLayout(mContext); 
     mSv.addView(ll); //Here it fails 
     ll.setOrientation(android.widget.LinearLayout.VERTICAL); 
... 
    } catch (Exception e) { // Goes to here 
     Log.e(tag,"Error Displaying Form"); 
     return false; 
    } 
} 

나는 예제로 이것을 사용 주 활동의 빈 Scrollview가 올바르게 전달됩니다 (null이 아님).하지만 100 % 확실하지는 않습니다. 어떤 도움/힌트를 부탁드립니다! :)

답변

1

백그라운드 스레드 (예 : doInBackground 메소드를 실행하는 스레드)에서 GUI를 터치 할 수 없습니다.

AsynTask에서 UI 코드를 onPostExecute에 넣을 수 있으며 결과는 doInBackground이며 UI 스레드에서 호출됩니다.

publishProgressdoInBackground에서 호출 할 수있는 경우 UI를 업데이트 할 수있는 UI 스레드에서 onProgressUpdate의 호출을 트리거합니다.

예를 들어 어떤 스레드에서 수행해야하는지에 대한 자세한 내용은 AsyncTask API을 참조하십시오.

0

솔루션

재 배열 코드 (그래서 GUI 물건 onPostExecute 할 것)을했다. 또한 onPostExecute() 얻지 못하는 함께 문제가 있지만 onPostExecute (Void 결과) 변경해야했습니다.

이제 내 코드는 다음과 같이 외모와 매력처럼 작동

: 당신이 예를 따르십시오 DisplayForm()가 (이처럼 보이는, 그래서 나는 또한 내 activity_main.xml에있는 ScrollView하고있는 LinearLayout을 추가

public class DoInBackground extends AsyncTask<String, Void, Void> { 

Context mContext; 
LinearLayout mLl; 

String tag = "DynamicFormXML"; 
XmlGuiForm theForm; 
ProgressDialog progressDialog; 
Handler progressHandler; 

public DoInBackground(Context context, LinearLayout ll) { 

    this.mContext = context; 
    this.mLl = ll; 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
} 

@Override 
protected Void doInBackground(String... params) { 

    getFormData(params[0]); 

    return null; 
} 

protected void onPostExecute(Void result) { 
    DisplayForm(); 
} 

전) :

private void DisplayForm() { 
    try 
    {   

     // walk thru our form elements and dynamically create them, leveraging our mini library of tools. 
     int i; 
     for (i=0;i<theForm.fields.size();i++) { 
      if (theForm.fields.elementAt(i).getType().equals("text")) { 
       theForm.fields.elementAt(i).obj = new XmlGuiEditBox(mContext,(theForm.fields.elementAt(i).isRequired() ? "*" : "") + theForm.fields.elementAt(i).getLabel(),""); 
       mLl.addView((View) theForm.fields.elementAt(i).obj); 
      } 
    ...