2015-05-10 80 views
0

這個異常是什麼意思? 只是不幸的是你的應用程序已經停止。點擊按鈕的異常

 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代碼 公共無效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() { 
+0

你在哪裏定義textView7 –

回答

0

看起來好像你沒有找到並啓動TextView之前運行此代碼。您需要button.setOnClickListener之前有這樣的:

textView7 = (TextView) findViewById(R.id.THE ID OF YOUR TEXT VIEW); 
+0

是是:D:D thaaaaank u:D –

+0

不客氣。 – Axiom

0

的問題是,雖然你說有一個TextView,你永遠不會將其與您的佈局關聯。你需要做的是通過做TextView和你的佈局。

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

沒有這個你得到一個NullPointerException,因爲textView7沒有設置任何東西,因此無效。

+0

是的:D非常感謝 –