gpt4 book ai didi

android - 尝试在空对象引用上调用虚拟方法 'java.lang.CharSequence android.widget.TextView.getText()'

转载 作者:行者123 更新时间:2023-11-29 19:16:29 24 4
gpt4 key购买 nike

我得到这个空异常:

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" />

有什么想法吗?

最佳答案

  1. 确保 TextView idInfo 存在于您的 activity_report.xml
  2. 尝试将 idInfo 声明为全局。

    ..............
    ....................

    TextView infoid;

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_report);

    infoid = (TextView) findViewById(R.id.idInfo);

    .............
    ...................

    Button sendButton = (Button) findViewById(R.id.button2);
    sendButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    String text = infoid.getText().toString();
    ..........
    .................
    }
    });

    }

更新:

获取选中的radioButton文本:

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rg);

// get selected radioButton from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();

// find the radioButton by returned id
radioButton = (RadioButton) findViewById(selectedId);

// radioButton text
String radiovalue = radioButton.getText();

希望对你有帮助~

关于android - 尝试在空对象引用上调用虚拟方法 'java.lang.CharSequence android.widget.TextView.getText()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43376654/

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