【问题标题】:Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference尝试在空对象引用上调用虚拟方法“java.lang.CharSequence android.widget.TextView.getText()”
【发布时间】: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


【解决方案1】:
  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();

希望对你有帮助~

【讨论】:

  • 我注意到问题出在单选按钮上:RadioGroup rg = (RadioGroup)findViewById(R.id.rg); final String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString(); 我删除了这段代码,它可以工作。但现在我需要另一种方式来读取用户选择的项目...谢谢。
  • 要获得选定的单选按钮文本,请尝试我更新的答案。希望它对你有用......
【解决方案2】:
if (radioGroup.getCheckedRadioButtonId() <= 0) {
    Toast.makeText(yourclass, "Choose Radio Button Please", Toast.LENGTH_SHORT).show();
}

【讨论】:

    【解决方案3】:

    check this

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;
    
    public class MyAndroidAppActivity extends Activity {
    
      private RadioGroup radioGroup;
      private RadioButton radioButton;
      private Button btnDisplay;
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        addListenerOnButton();
    
      }
    
      public void addListenerOnButton() {
    
        radioGroup = (RadioGroup) findViewById(R.id.radio);
        btnDisplay = (Button) findViewById(R.id.btnDisplay);
    
        btnDisplay.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                    // get selected radio button from radioGroup
                int selectedId = radioGroup.getCheckedRadioButtonId();
    
                // find the radiobutton by returned id
                radioButton = (RadioButton) findViewById(selectedId);
    
                Toast.makeText(MyAndroidAppActivity.this,
                    radioButton.getText(), Toast.LENGTH_SHORT).show();
    
            }
    
        });
    
      }
    }
    

    【讨论】:

    • 你的问题开头的这个链接是什么意思?它是另一个 StackOverflow 中答案的链接,但与您的问题有什么关系?你可以更好地描述你的问题。您收到答案只是因为您已成功发布代码并且错误是基本错误,但最好将描述放在正文中而不是标题本身中,您可以更好地组织您的帖子以便更多清除。
    猜你喜欢
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2015-06-05
    相关资源
    最近更新 更多