我想将一个类中的字符串变量的值用于另一个类...
但是当我通过在被调用类的调用类中创建一个对象来这样做时,我得到的值为 null。
我尝试在网上搜索和堆栈溢出并经历了一些类似的答案,但他们的解决方案是针对这些问题的。所以我再问一次。
这是2个课程-
第一个是我想使用变量的类。类是 - 我想在另一个类中使用的 SelectedClass 变量 - SelectedClass(一个字符串)
第二类是 FEa。我想使用第一类的变量“SelectedClass”的值。
CLASS 1(SelectedClass)-
package com.attendance_trial.nirmik;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class SelectClass extends ListActivity
{
public String classes2[] = {"FEa","FEb","SEa","SEb"};
String SelectedClass;
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String SelectedClass = classes2[position];
try
{
Class MyClass = Class.forName("com.attendance_trial.nirmik." + SelectedClass);
Intent myintent = new Intent(SelectClass.this,MyClass);
startActivity(myintent);
}
catch(ClassNotFoundException cnf)
{
cnf.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setListAdapter(new ArrayAdapter<String>(SelectClass.this, android.R.layout.simple_list_item_1, classes2));
}
}
2 级 (FEa) -
package com.attendance_trial.nirmik;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class FEa extends Activity implements View.OnClickListener
{
SelectClass sc = new SelectClass();
//Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,bSend;
Button bSend;
TextView tvDisp;
String acc="";
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fea);
initialise();
}
private void initialise()//initialise all buttons etc
{
bSend = (Button) findViewById (R.id.BtnSendMsg);
bSend.setOnClickListener(this);
tvDisp=(TextView) findViewById (R.id.TxtViewDisplay);
}
public void onButtonClick(View v)
{
// TODO Auto-generated method stub
Button theButton = (Button)v;
String chk = theButton.getText().toString();
if(chk.contentEquals("1")||chk.contentEquals("2")||chk.contentEquals("3")||chk.contentEquals("4")||chk.contentEquals("5")
||chk.contentEquals("6")||chk.contentEquals("7")||chk.contentEquals("8")||chk.contentEquals("9"))
{
acc = acc + "0" + chk;
}
else
{
acc = acc +chk;
}
tvDisp.setText("String Is:" + acc);
}//method end
@Override
public void onClick(View v2)
{
// TODO Auto-generated method stub
int retid = v2.getId();
if(retid == R.id.BtnSendMsg ) //send button clicked
{
String msg = sc.SelectedClass + "2210"+ acc;
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, msg);
startActivity(emailIntent);
}
}
}//主端