2017-02-28 106 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

约翰凯西所以你的问题是解决不.. ..? –