嗨,我正在编写一个程序来添加动态测试视图。我想在单击按钮时添加文本视图。但是当我添加文本视图时,我收到错误
这是我的日志文件
02-25 22:13:08.835: W/dalvikvm(3609): threadid=1: thread exiting with uncaught exception (group=0x40f102a0)
02-25 22:13:08.843: E/AndroidRuntime(3609): FATAL EXCEPTION: main
02-25 22:13:08.843: E/AndroidRuntime(3609): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
02-25 22:13:08.843: E/AndroidRuntime(3609): at android.view.ViewGroup.addViewInner(ViewGroup.java:3387)
02-25 22:13:08.843: E/AndroidRuntime(3609): at android.view.ViewGroup.addView(ViewGroup.java:3258)
02-25 22:13:08.843: E/AndroidRuntime(3609): at android.view.ViewGroup.addView(ViewGroup.java:3234)
02-25 22:13:08.843: E/AndroidRuntime(3609): at com.example.test.MainActivity$1.onClick(MainActivity.java:37)
02-25 22:13:08.843: E/AndroidRuntime(3609): at android.view.View.performClick(View.java:4222)
02-25 22:13:08.843: E/AndroidRuntime(3609): at android.view.View$PerformClick.run(View.java:17273)
02-25 22:13:08.843: E/AndroidRuntime(3609): at android.os.Handler.handleCallback(Handler.java:615)
02-25 22:13:08.843: E/AndroidRuntime(3609): at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 22:13:08.843: E/AndroidRuntime(3609): at android.os.Looper.loop(Looper.java:137)
02-25 22:13:08.843: E/AndroidRuntime(3609): at android.app.ActivityThread.main(ActivityThread.java:4895)
02-25 22:13:08.843: E/AndroidRuntime(3609): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 22:13:08.843: E/AndroidRuntime(3609): at java.lang.reflect.Method.invoke(Method.java:511)
02-25 22:13:08.843: E/AndroidRuntime(3609): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
02-25 22:13:08.843: E/AndroidRuntime(3609): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
02-25 22:13:08.843: E/AndroidRuntime(3609): at dalvik.system.NativeStart.main(Native Method)
我的活动类
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout layout=(LinearLayout)findViewById(R.id.rlayout);
LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View vs= (View) mInflater.inflate(R.layout.test, null);
TextView textView = (TextView) vs.findViewById(R.id.tview);
textView.setText("your text");
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(textView,p);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
actvity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/rlayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
测试.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/test" >
<TextView
android:id="@+id/tview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
请帮忙..提前致谢
您正在将文本视图添加到已经具有父级的 LinearLayout。您的错误消息非常清楚。
您需要先从其父布局中删除 TextView,然后才能将其添加到另一个布局。
这样做:
TextView textView = (TextView) vs.findViewById(R.id.tview);
vs.removeView(textView); // assuming "vs" is the parent layout of your TextView
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = (LinearLayout)findViewById(R.id.rlayout);
layout.addView(textView,p);
请不要说我只是假设"vs"是TextView的父布局。只需在布局文件中查找,即可获取 TextView 的父布局。然后致电:
parent.removeView(textView);
您尝试添加的文本视图(tview)已经存在于您膨胀的布局中(activity_main)。无法再次添加,因为它已经具有父布局
相反,您应该做一个
TextViewtextView = new TextView(v.getContext());
动态创建文本视图,然后将其添加到布局中
您的 TextView "textView" 已经存在并附加到视图 "vs"。
对于如此简单的操作,您无需膨胀 xml。您可以执行以下操作:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout layout=(LinearLayout)findViewById(R.id.rlayout);
TextView textView = new TextView(MainActivity.this);
textView.setText("your text");
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(textView,p);
}
IllegalStateException
正在发生,因为您尝试添加的TextView
textView layout
是vs
的孩子。您应该将vs
视图添加到layout
中。下面我更新了代码...看看吧。
将以下侦听器代码片段替换为您的代码片段。
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout layout=(LinearLayout)findViewById(R.id.rlayout);
LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View vs= (View) mInflater.inflate(R.layout.test, null);
TextView textView = (TextView) vs.findViewById(R.id.tview);
textView.setText("your text");
layout.addView(vs);
}
});