[英]Add TextView dynamically in a different Activity (RelativeLayout)
每次我在新活动(已创建)中单击按钮时,我都会尝试添加新的TextView,但是我遇到了多个错误,例如Null Pointer Exception和Illegal State Exception。
这是我在button
活动中使用的代码,当我按下该button
时它被激活。 在代码中我回想起布局activity_position
,这是我想要添加TextView
的活动。
RelativeLayout rl = (RelativeLayout)findViewById(R.layout.activity_position);
TextView tv = new TextView(this);
tv.setId(10);
tv.setText(addr);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
rl.addView(tv);
运行此代码只能得到错误(在rl.addView(tv)行上),我无法弄明白为什么。 这是我想要动态添加TextViews的活动的XML。
<RelativeLayout 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"
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=".PositionActivity" >
02-18 12:21:47.348: E/AndroidRuntime(4995): FATAL EXCEPTION: main
02-18 12:21:47.348: E/AndroidRuntime(4995): java.lang.IllegalStateException: Could not execute method of the activity
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.view.View$1.onClick(View.java:3758)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.view.View.performClick(View.java:4377)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.view.View$PerformClick.run(View.java:18044)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.os.Handler.handleCallback(Handler.java:725)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.os.Looper.loop(Looper.java:137)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.app.ActivityThread.main(ActivityThread.java:5306)
02-18 12:21:47.348: E/AndroidRuntime(4995): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 12:21:47.348: E/AndroidRuntime(4995): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 12:21:47.348: E/AndroidRuntime(4995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-18 12:21:47.348: E/AndroidRuntime(4995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-18 12:21:47.348: E/AndroidRuntime(4995): at dalvik.system.NativeStart.main(Native Method)
02-18 12:21:47.348: E/AndroidRuntime(4995): Caused by: java.lang.reflect.InvocationTargetException
02-18 12:21:47.348: E/AndroidRuntime(4995): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 12:21:47.348: E/AndroidRuntime(4995): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 12:21:47.348: E/AndroidRuntime(4995): at android.view.View$1.onClick(View.java:3753)
02-18 12:21:47.348: E/AndroidRuntime(4995): ... 11 more
02-18 12:21:47.348: E/AndroidRuntime(4995): Caused by: java.lang.NullPointerException
02-18 12:21:47.348: E/AndroidRuntime(4995): at com.example.placeholder.LocatingActivity.getCoords(LocatingActivity.java:101)
编辑:添加了LogCat
你能帮我吗?
谢谢!
完整代码: https ://drive.google.com/folderview?id = 0B3uaHczyJIVQVXhMR1lyN3JnWjQ&usp =sharing
目前您正在尝试将R.layout.activity_position
为RelativeLayout
。 将id分配给RelativeLayout,然后在代码中使用R.id.relatv_layout:
在xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relatv_layout"
....
tools:context=".PositionActivity" >
在代码中使用R.layout.relatv_layout
来初始化rl
:
RelativeLayout rl = (RelativeLayout)findViewById(R.id.relatv_layout);
完整答案:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Test extends Activity {
private LinearLayout LL;
private RelativeLayout RL;
private Button btn;
int click_counter = 0;
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_test);
RL = (RelativeLayout)findViewById(R.id.relative_layout);
btn = (Button)findViewById(R.id.btn);
LL = new LinearLayout(this);
LL.setOrientation(LinearLayout.VERTICAL);
RL.addView(LL);
// Listener
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = new TextView(getApplicationContext());
tv.setText("Click: " + ++click_counter);
LL.addView(tv);
}
});
} // onCreate()
}
和布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
logcat输出中的根异常位于第101行的LocatingActivity
。在该活动中没有OnClickListener
,所以让我们看看我是否正确:你试图从另一个Activity
修改一个Activity
内容(我假设第一个是在堆栈中的第二个)?
你不能这样做。 您只能操纵当前活动(即显示)的活动的视图/布局,所有其他活动都已暂停,甚至可能已被销毁。
我建议你阅读这篇文章: http : //developer.android.com/guide/components/activities.html以及网站上的其他基本文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.