Androidアプリの同じビュー上のボタンとTextViewの追加の問題 - 違法状態例外
-
21-12-2019 - |
質問
私はAndroidアプリに少し新しいもので、彼らの仕事のためにより良い感触を得るために彼らと一緒に遊ぼうとしています。今、私はAndroidのあなたの最初のアプリを構築しています。ここで、あなたはメッセージを別の活動に渡します。そのメッセージを表示し、別のアクティビティに行く可能性がある別のボタンがありますが、私のエラーを理解することはできません。IllegalStateExceptionを取得します - 子はすでに親を持っています。私はこれらの関数の両方を取り除くことを試みましたが、私のレイアウトはボタンを含めません。私はこのエラーを獲得しているのか、またはこのレイアウトに近づいているのであれば私はわからない。あらゆるヘルプ/アドバイスは大いに感謝されるでしょう、ありがとう!
これは私のAppのコードです
package com.example.app;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
// Set the text view as the activity layout
setContentView(R.layout.fragment_display_message);
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView) this.findViewById(R.id.text_view);
textView.setTextSize(20);
message = "The current address is " + message;
textView.setText(message);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, 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_display_message,
container, false);
return rootView;
}
}
}
.
これはマニフェスト
です<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.app.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.dm.zbar.android.scanner.ZBarScannerActivity"
android:screenOrientation="landscape"
android:label="@string/app_name" />
<activity
android:name="com.example.app.DisplayMessageActivity"
android:label="@string/title_activity_display_address"
android:parentActivityName="com.example.app.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
<activity android:name="AddressChange"></activity>
</application>
</manifest>
.
fragment_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >
<TextView
android:id="@+id/text_view"
android:layout_width="150dp"
android:layout_height="114dp" />
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Back"
android:text="@string/back" />
</LinearLayout>
.
LOGCATファイル
06-04 15:22:55.380: E/AndroidRuntime(820): FATAL EXCEPTION: main
06-04 15:22:55.380: E/AndroidRuntime(820): Process: com.example.app, PID: 820
06-04 15:22:55.380: E/AndroidRuntime(820): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.DisplayMessageActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
06-04 15:22:55.380: E/AndroidRuntime(820): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
06-04 15:22:55.380: E/AndroidRuntime(820): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
06-04 15:22:55.380: E/AndroidRuntime(820): at android.app.ActivityThread.access$800(ActivityThread.java:138)
06-04 15:22:55.380: E/AndroidRuntime(820): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
06-04 15:22:55.380: E/AndroidRuntime(820): at android.os.Handler.dispatchMessage(Handler.java:102)
06-04 15:22:55.380: E/AndroidRuntime(820): at android.os.Looper.loop(Looper.java:136)
06-04 15:22:55.380: E/AndroidRuntime(820): at android.app.ActivityThread.main(ActivityThread.java:5042)
06-04 15:22:55.380: E/AndroidRuntime(820): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 15:22:55.380: E/AndroidRuntime(820): at java.lang.reflect.Method.invoke(Method.java:515)
06-04 15:22:55.380: E/AndroidRuntime(820): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:776)
06-04 15:22:55.380: E/AndroidRuntime(820): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-04 15:22:55.380: E/AndroidRuntime(820): at dalvik.system.NativeStart.main(Native Method)
06-04 15:22:55.380: E/AndroidRuntime(820): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
06-04 15:22:55.380: E/AndroidRuntime(820): at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
06-04 15:22:55.380: E/AndroidRuntime(820): at android.view.ViewGroup.addView(ViewGroup.java:3415)
06-04 15:22:55.380: E/AndroidRuntime(820): at android.view.ViewGroup.addView(ViewGroup.java:3360)
06-04 15:22:55.380: E/AndroidRuntime(820): at android.view.ViewGroup.addView(ViewGroup.java:3336)
06-04 15:22:55.380: E/AndroidRuntime(820): at com.example.app.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:35)
06-04 15:22:55.380: E/AndroidRuntime(820): at android.app.Activity.performCreate(Activity.java:5231)
06-04 15:22:55.380: E/AndroidRuntime(820): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-04 15:22:55.380: E/AndroidRuntime(820): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
06-04 15:22:55.380: E/AndroidRuntime(820): ... 11 more
. 解決
XMLレイアウトは、ビューの追加のすべてを追加します。それを自分でやることによって、あなたは単一のビューのために2つの両親を作成します、それは違法です。幸いなことに、あなたは必要ではない仕事をしているだけで、あなたはそれらの線を取り除くことができます。
所属していません StackOverflow