我每次点击一个新活动(已经创建)时都尝试添加一个新的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
答案 0 :(得分:2)
目前您正在尝试将R.layout.activity_position
投射到RelativeLayout
。将id分配给RelativeLayout,然后在代码中使用R.id.relatv_layout:
:
<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);
答案 1 :(得分:0)
完整答案:
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>
答案 2 :(得分:0)
logcat输出中的根异常位于LocatingActivity
第101行。在该活动中没有OnClickListener
,所以让我们看看我是否正确:你正在尝试修改内容来自另一个Activity
的{{1}}(我假设第一个在堆栈中的第二个下面)?
你永远不能那样做。您只能操纵当前活动(即显示)的活动的视图/布局,所有其他活动都已暂停,甚至可能已被销毁。
我建议你阅读:http://developer.android.com/guide/components/activities.html以及网站上的其他基本文档。