2017-02-28 52 views
-1

所以我有一個更早的版本。不幸的是,我忘了將工作版本複製到我的USB上,所以我試圖在家中複製它。我有5個方向單選按鈕。每次檢查一次,都應該改變TextView中的文本以與相關的方向相對應。不幸的是,這似乎並不奏效。試圖運行該應用程序時,它崩潰。單選按鈕的OnClick事件不起作用

調試屏幕也會拋出一些關於空指針異常的信息。另外,在單選按鈕的xml代碼中,有一些關於我在Main中找不到的方法。任何幫助表示讚賞。

<RadioButton 
    android:text="@string/north" 
    android:layout_width="79dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="12dp" 
    android:layout_marginStart="12dp" 
    android:layout_marginTop="29dp" 
    android:id="@+id/north" 
    android:onClick="onClick" /> 

<RadioButton 
    android:text="@string/south" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/south" /> 

<RadioButton 
    android:text="@string/east" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/east" /> 

<RadioButton 
    android:text="@string/west" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/west" /> 

<RadioButton 
    android:text="@string/center" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/center" /> 

</RadioGroup> 

<SeekBar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="22dp" 
    android:id="@+id/speed" 
    android:layout_below="@+id/Speed" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

活動:

public class MainActivity extends AppCompatActivity { 
    RadioButton n, s, e, w, c; 
    // TextView text = (TextView)findViewById(R.id.text); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    } 

    public View.OnClickListener optionOnClickListener; 

    { 
     optionOnClickListener = new OnClickListener() { 

      public View v; 

      public void onClick(View v) { 
       this.v = v; 
       TextView text = (TextView) findViewById(R.id.text); 
       String str = null; 
       n = (RadioButton) findViewById(R.id.north); 
       s = (RadioButton) findViewById(R.id.south); 
       e = (RadioButton) findViewById(R.id.east); 
       w = (RadioButton) findViewById(R.id.west); 
       c = (RadioButton) findViewById(R.id.center); 


       // you can simply copy the string of clicked button. 
       str = ((RadioButton) v).getText().toString(); 
       text.setText(str); 

       // or, you can check each radiobutton and find which one is checked. 
       if (n.isChecked()) { 
        text.setText("North"); 
       } else if (s.isChecked()) { 
        text.setText("South"); 
       } else if (e.isChecked()) { 
        text.setText("East"); 
       } else if (w.isChecked()) { 
        text.setText("West"); 
       } else if (c.isChecked()) { 
        text.setText("Center"); 
       } 

      } 

     }; 
    } 

} 
+0

你可以發佈異常的堆棧跟蹤嗎? –

+0

我想你會在這裏找到答案 http://stackoverflow.com/questions/9748070/radio-group-onclick-event-not-firing-how-do-i-tell-which-is-selected 對於你的問題 – Shyamali

+0

關於電臺**組的問題**會告訴他爲什麼這個應用程序無法啓動? –

回答

0

因爲您是從XML這個塊,你必須設置的onClick方法:

public View.OnClickListener optionOnClickListener; 

{ 
    optionOnClickListener = new OnClickListener() { 

     public View v; 

//delete above here 
     public void onClick(View v) { 
      this.v = v; //delete this 
      TextView text = (TextView) findViewById(R.id.text); 
      String str = null; 
      n = (RadioButton) findViewById(R.id.north); 
      s = (RadioButton) findViewById(R.id.south); 
      e = (RadioButton) findViewById(R.id.east); 
      w = (RadioButton) findViewById(R.id.west); 
      c = (RadioButton) findViewById(R.id.center); 


      // you can simply copy the string of clicked button. 
      str = ((RadioButton) v).getText().toString(); 
      text.setText(str); 

      // or, you can check each radiobutton and find which one is checked. 
      if (n.isChecked()) { 
       text.setText("North"); 
      } else if (s.isChecked()) { 
       text.setText("South"); 
      } else if (e.isChecked()) { 
       text.setText("East"); 
      } else if (w.isChecked()) { 
       text.setText("West"); 
      } else if (c.isChecked()) { 
       text.setText("Center"); 
      } 

     } 

//delete below here 

    }; 
} 

應該是:

//remove unneccesary variable and wrapper 

public void onClick(View v) { 
    ... // keep all the lines in this method except this.v = v; 
} 

之後它應該編譯,儘管一個此外,我會做的是做的所有findViewById(...)的onCreate而不是每做一次點擊做它們

+0

如果我刪除包裝,我得到一個錯誤的負載。新代碼是什麼樣的? –

+0

與我的例子完全一樣......我不知道你可能意味着什麼。特別是如你所說你早些時候有這個工作。什麼是「錯誤負載」? –

+0

變量和包裝都不見了。它仍然崩潰。我無法從早期獲取我的代碼。 –

0

好吧,所以事實證明,我甚至不需要監聽器,因爲圖書館似乎照顧它。另外,我認爲尼克卡多佐是正確的,他說onClick是保留的。這是一個漫長的一天。謝謝大家的幫助!晚安。

+0

約翰凱西所以你的問題是解決不.. ..? –