gpt4 book ai didi

java - 在不同的Activity中动态添加TextView(RelativeLayout)

转载 作者:行者123 更新时间:2023-12-02 06:11:21 27 4
gpt4 key购买 nike

每次单击按钮时,我都尝试在新 Activity (已创建)中添加一个新的 TextView,但我也遇到了多个错误,例如空指针异常和非法状态异常。

这是我在 button Activity 中使用的代码,当我按下 button 时它就会被激活。在代码中,我回想起布局 activity_position,这是我想要添加 TextView 的 Activity 。

 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) 行上),我不明白为什么。这是我想动态添加 TextView 的 Activity 的 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);

关于java - 在不同的Activity中动态添加TextView(RelativeLayout),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21851967/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com