problem in .java [closed]
this is my java code and i really can not find where is my wrong
package com.example开发者_高级运维.RateMates;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
public class RateMates extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue);
radio_red.setOnClickListener(radio_listener);
radio_blue.setOnClickListener(radio_listener);
}
private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
RadioButton rb = (RadioButton) v;
Toast.makeText(RateMates.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
};
Button next = (Button) findViewById(R.id.button);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), RateMates2.class);
startActivityForResult(myIntent, 0);
}
});
}}
The problem is that your statements
Button next = (Button) findViewById(R.id.button);
next.setOnClickListener....
are in the body of the class, and that's illegal, you need to move these lines into the onCreate function instead.
However if you would have given the error message, I'm sure someone would have found this quicker. I haven't read the comments properly.
Still, it's not clear why you get this error because your OnClickListeners don't need to be final here, and my answer is (I guess) still valid.
You Have to use "final OnClickListener radio_listener" instead of "private OnClickListener radio_listener"
精彩评论