应用程序崩溃(指定的孩子已经有父母)

时间:2015-04-24 12:24:20

标签: java android crash

我的应用程序崩溃,无法找出问题所在。请看下面的代码。

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)

谢谢

1 个答案:

答案 0 :(得分:2)

  

IllegalStateException:指定的子级已有父级。您必须首先在孩子的父母身上调用removeView()。

您正在呼叫setContentView()三次。

您的setContentView(webview);错了。