单击按钮的异常

时间:2015-05-10 23:00:39

标签: android exception

这个例外是什么意思? 它只是给出一条消息,不幸的是你的应用程序已停止。

      05-11 01:52:18.189: E/AndroidRuntime(735): FATAL EXCEPTION: main
         05-11 01:52:18.189: E/AndroidRuntime(735): Process:   com.example.hospitalsdirectorysystem, PID: 735
   05-11 01:52:18.189: E/AndroidRuntime(735): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    05-11 01:52:18.189: E/AndroidRuntime(735):  at com.example.hospitalsdirectorysystem.MorningDrsGeneral$1.onClick(MorningDrsGeneral.java:72)
    05-11 01:52:18.189: E/AndroidRuntime(735):  at android.view.View.performClick(View.java:4832)
    05-11 01:52:18.189: E/AndroidRuntime(735):  at android.view.View$PerformClick.run(View.java:19839)
   05-11 01:52:18.189: E/AndroidRuntime(735):   at android.os.Handler.handleCallback(Handler.java:739)
    05-11 01:52:18.189: E/AndroidRuntime(735):  at android.os.Handler.dispatchMessage(Handler.java:95)
    05-11 01:52:18.189: E/AndroidRuntime(735):  at android.os.Looper.loop(Looper.java:211)
    05-11 01:52:18.189: E/AndroidRuntime(735):  at android.app.ActivityThread.main(ActivityThread.java:5317)
  05-11 01:52:18.189: E/AndroidRuntime(735):    at java.lang.reflect.Method.invoke(Native Method)
  05-11 01:52:18.189: E/AndroidRuntime(735):    at java.lang.reflect.Method.invoke(Method.java:372)
   05-11 01:52:18.189: E/AndroidRuntime(735):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
  05-11 01:52:18.189: E/AndroidRuntime(735):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

这是我的.java代码 public void addListenerOnButton1(){

        final Context context = this;

        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {


            if (rb0.isChecked())
            {


                texx = "you choose Dr.Kamal , he is available at 8:30 - 9:00     ";
           textView7.setText(texx);


            }


                }



        });}

并注意我将这段代码放在全局:

      RadioButton rb0 = (RadioButton) findViewById(R.id.radio0);

这就是我定义textView7

的地方
  public class MorningDrsGeneral extends ActionBarActivity {

String texx;

TextView textView7;

RadioButton rb0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.morningdrs);

     rb0 = (RadioButton) findViewById(R.id.radio0);

    et = (EditText) findViewById(R.id.et);
    addListenerOnButton1();
  }
  public void addListenerOnButton1() {

        final Context context = this;

        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

2 个答案:

答案 0 :(得分:0)

在运行此代码之前,您似乎找不到并启动了TextView。您需要在button.setOnClickListener:

之前使用此功能
textView7 = (TextView) findViewById(R.id.THE ID OF YOUR TEXT VIEW);

答案 1 :(得分:0)

问题是虽然你说有一个TextView,但你从不将它与你的布局相关联。您需要做的是通过执行操作将TextView与您的布局相关联。

textView7 = (TextView) findViewById(R.id.YourTextViewId);

如果没有这个,你会得到一个NullPointerException,因为textView7没有设置为任何东西,因此是null。