您好我是android,java的新手,我想使用来自另一个类的方法的字符串。具体来说,我想使用String来从类Test到类MainActivity。我试着称之为
Test.AnswerQ1
但它表示无法解析符号" AnswerQ1"
这是我的测试类
public class Test extends Activity {
private RadioGroup radioGroupQuestion1;
private RadioButton radioButtonQuestion1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroupQuestion1 = (RadioGroup) findViewById(R.id.radioGroupQ1);
Button NextButton = (Button) findViewById(R.id.Q1NextButton);
NextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int selectedId = radioGroupQuestion1.getCheckedRadioButtonId();
radioButtonQuestion1 = (RadioButton) findViewById(selectedId);
String AnswerQ1 = radioButtonQuestion1.getText().toString();
String Question1 = getString(R.string.Q1);
System.out.println(Question1);
System.out.println(AnswerQ1);
Intent myIntent = new Intent(view.getContext(), Question2.class);
startActivityForResult(myIntent, 0);
}
});
}
这是我的MainActivity类
public class MainActivity extends Activity{
String myData = Test.s;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println(myData);
}
}
答案 0 :(得分:0)
Try with initializing string on the top of an onCreate method.
public class Test extends Activity {
public static String s = "hello world";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
答案 1 :(得分:0)
不要在实例方法中声明静态成员。
您必须将静态成员声明为类成员而非本地成员。
示例:
// This function will run inside of a web-page, not in the CasperJS script
function putInObject() {
var obj = {}; // UPDATED: here we also need to init this object
obj.title = document.querySelector('[itemprop="title"]').innerText;
obj.description = document.querySelector('[itemprop="description"]').innerText;
// return data from a web page context to CasperJS context
return obj;
}
...
// receive data in CasperJS context
obj = casper.evaluate(putInObject);
// If you want to view object's contents
console.log(JSON.stringify(obj));
答案 2 :(得分:0)
你可以制作s public static
但是还有很多其他解决方案。根据您的需要,您可以使用Shared Preference
,Broadcast Listener
,Interface
等。