【发布时间】:2017-09-08 15:42:23
【问题描述】:
我得到了这个空异常:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.CharSequence android.widget.TextView.getText()”
我不知道为什么。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.LoginRegister);
setContentView(R.layout.activity_report);
final TextView infoid = (TextView) findViewById(R.id.idInfo);
RadioGroup rg = (RadioGroup)findViewById(R.id.rg);
final String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();
...
Button sendButton = (Button) findViewById(R.id.button2);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = infoid.getText().toString(); //what is wrong?
Log.w("message: ", radiovalue);
}
});
}
我的 xml
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/idInfo"
android:hint="Add some extra information"
android:layout_weight="0.09"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp" />
<Button
android:text="Report"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button2" />
有什么想法吗?
【问题讨论】:
-
使用调试器会给你一个快速的答案
-
你为什么要在启动 Activity 后获取文本?
-
@cricket_007 我正在处理这个活动,我将把这个意图更改为我的报告函数 onResponse...
-
从 infoid def 中取出 'final' attr 并确保在使用它之前它的 ref 存在。
标签: android android-layout nullpointerexception textview