2015-04-24 77 views
0

我的应用程序崩溃,无法找出问题。请看下面的代码。应用程序崩溃(指定的孩子已经有父母)

Country_Select.java:

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

    } 

    private static final String TAG = "BOBLogs"; 
    public static final String PREFS_NAME = "PrefsFile"; 
// 
// Button ke_button = (Button) findViewById(R.id.btn_ke); 
// Button za_button = (Button) findViewById(R.id.btn_za); 



    public void loadZA(View view){ 
     Context context = getApplicationContext(); 
     Log.i(TAG, "Started loadZA"); 
     Intent intent = new Intent(this, TnCActivity.class); 
     SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putString("Country_Selected", "ZA"); 
     editor.commit(); 
     startActivity(intent); 
     String channel = (prefs.getString("Country_Selected", "")); 
     CharSequence test = channel; 
     int duration = Toast.LENGTH_SHORT; 

     Toast toast = Toast.makeText(context, test, duration); 
     toast.show(); 
    } 


    public void loadKE(View view){ 

     Context context = getApplicationContext(); 
     Intent intent = new Intent(this, TnCActivity.class); 
     SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putString("Country_Selected", "KE"); 
     editor.commit(); 
     String channel = (prefs.getString("Country_Selected", "")); 
     CharSequence test = channel; 
     int duration = Toast.LENGTH_SHORT; 

     Toast toast = Toast.makeText(context, test, duration); 
     toast.show(); 

     startActivity(intent); 
     Log.i(TAG, "Started intent"); 
    } 

上述活动基本上允许用户选择选择的国家。根据他们的选择,将加载特定的条款和条件。基于SharedPreferences“Country_Selected”数据。

TNC活动:

private static final String TAG = "BOBLogs"; 
    public static final String PREFS_NAME = "PrefsFile"; 

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

     final EditText edittext; 
     final Button tncButtonZa; 
     final Button tncButtonKe; 

     edittext = (EditText) findViewById(R.id.txt_TnCs_heading); 
     tncButtonZa = (Button) findViewById(R.id.btn_za); 
     tncButtonKe = (Button) findViewById(R.id.btn_ke); 

     SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
     String channel = (prefs.getString("Country_Selected", "")); 
     CharSequence test = channel; 

     try { 
      if (channel.equals("ZA")) { 
       WebView webview = (WebView) findViewById(R.id.wv_tnc_html); 
       setContentView(webview); 
       webview.loadUrl("file:///android_asset/terms_conditions_sa.html"); 

      } else { 
//   WebView webview = new WebView(this); 
       WebView webview = (WebView) findViewById(R.id.wv_tnc_html); 
       setContentView(webview); 
       webview.loadUrl("file:///android_asset/terms_conditions_ke.html"); 

      } 
     }catch (InflateException e) { 


     } 

林不知道在碰撞发生的事情或者为什么,但这里是logcat中的一部分?

 04-24 08:04:27.198 22058-22058/app.MyApplication.com.MyApplication E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: app.MyApplication.com.MyApplication, PID: 22058 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{app.MyApplication.com.MyApplication/app.MyApplication.com.MyApplication.TnCActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
      at android.app.ActivityThread.access$800(ActivityThread.java:135) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5001) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
      at android.view.ViewGroup.addViewInner(ViewGroup.java:3562) 
      at android.view.ViewGroup.addView(ViewGroup.java:3415) 
      at android.view.ViewGroup.addView(ViewGroup.java:3360) 
      at android.view.ViewGroup.addView(ViewGroup.java:3336) 
      at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:231) 
      at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:107) 
      at app.MyApplication.com.MyApplication.TnCActivity.onCreate(TnCActivity.java:40) 
      at android.app.Activity.performCreate(Activity.java:5231) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
            at android.app.ActivityThread.access$800(ActivityThread.java:135) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5001) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
            at dalvik.system.NativeStart.main(Native Method) 

谢谢

回答

2

IllegalStateException异常:指定的孩子已经有一个父。您必须先调用子对象的父对象的removeView()。

您打三次电话setContentView()。您的setContentView(webview);是错误的。

+0

非常感谢你!工作完美,不知道为什么我没有看到! – x10sion

+0

不客气。 – shkschneider

相关问题