【发布时间】:2014-07-25 08:25:11
【问题描述】:
我对 Android 应用程序有点陌生,我正在尝试与它们一起玩,以更好地了解它们的工作方式。现在我正在为 android 构建您的第一个应用程序,您可以在其中将消息传递给另一个活动。我想显示该消息并有另一个可能转到另一个活动的按钮,但我无法弄清楚我的错误。我得到一个 IllegalStateException - 孩子已经有一个父母,当我尝试将 textview 和按钮添加到我的相对布局时,您必须首先在孩子的父母上调用 removeView()。我已经尝试删除这两个功能,但我的布局不包括按钮。我不确定我为什么会收到这个错误,或者我是否正在接近这个布局错误。任何帮助/建议将不胜感激,谢谢!
这是我的应用程序的代码
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
【问题讨论】:
-
为什么要显式添加 TextView 和 Button 到布局中?他们不是已经在xml中设置了吗?
-
是的,它们都在 XML 中设置。只有 TextView 会自动显示,所以我试图让它们同时显示。
-
请发布您的 xml。我怀疑您的按钮设置为不可见...
-
@DerGolem 完成。我使用的 XML 是 fragment_display_message.xml
-
抱歉,我没看到 - 都在同一个灰色区域...
标签: android android-layout textview android-button illegalstateexception