如何访问主类以外的值?我想打印elementarytxtview的值 在参数的主类中但是在参数中打印null值我想将参数中的elementarytextview值传递给其他活动
public class HomeMenu extends Activity {
ImageButton imgNews, imgContact, imgSetting;
ListView listMainMenu;
ListView Middleschoollist, HighSchoollist, Atipicalschoollist;
String status;
firstscreenadapter mma;
String SelectMenuAPI;
String SelectMenuAPI2;
String url1;
String elementry;
// String High;
String message;
TextView Elementarytxt, Middletxt, Hightxt, Atypicaltxt;
static ArrayList<Long> Category_ID = new ArrayList<Long>();
static ArrayList<String> Category_name = new ArrayList<String>();
String elementarytxtview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstscreenfinal);
ExpandableHeightListView listMainMenu = (ExpandableHeightListView)
findViewById(R.id.listMainMenu11);
ExpandableHeightListView Middleschoollist = (ExpandableHeightListView)
findViewById(R.id.listMainMenu22);
ExpandableHeightListView HighSchoollist = (ExpandableHeightListView)
findViewById(R.id.listMainMenu33);
ExpandableHeightListView Atipicalschoollist = (ExpandableHeightListView)
findViewById(R.id.listMainMenu44);
Elementarytxt = (TextView) findViewById(R.id.Elementaryschool);
Middletxt = (TextView) findViewById(R.id.MiddleSchool);
Hightxt = (TextView) findViewById(R.id.HighSchool);
Atypicaltxt = (TextView) findViewById(R.id.AtipicalSchool);
mma = new firstscreenadapter(this);
if (!Utils.isNetworkAvailable(HomeMenu.this)) {
Toast.makeText(HomeMenu.this, "NO NETWORK Available",
Toast.LENGTH_SHORT).show();
}
if (!Utils.isUserOnline(this)) {
Toast.makeText(this, "No NETWORK", Toast.LENGTH_LONG).show();
}
url1 = "http://198.57.208.46/~school/ajax.php?action=get_school";
listMainMenu.setAdapter(mma);
Middleschoollist.setAdapter(mma);
HighSchoollist.setAdapter(mma);
Atipicalschoollist.setAdapter(mma);
listMainMenu.setExpanded(true);
Middleschoollist.setExpanded(true);
HighSchoollist.setExpanded(true);
Atipicalschoollist.setExpanded(true);
Toast.makeText(this, elementarytxtview, Toast.LENGTH_SHORT).show();
listMainMenu.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
Intent iMenuList = new Intent(HomeMenu.this,
SecondStep.class);
iMenuList.putExtra("category_name",xxx.xxx.xxxx/~school/index.php
/api/index/getschools?mg="+ Category_name.get(position)+ "&sl="+elementarytxtview);
startActivity(iMenuList);
}
});
parseJSONData();
}
void clearData() {
Category_ID.clear();
Category_name.clear();
}
public void parseJSONData() {
SelectMenuAPI = Utils.Homemenu2;
SelectMenuAPI2 = Utils.Homemenu;
clearData();
try {
HttpClient client = new DefaultHttpClient();
HttpConnectionParams
.setConnectionTimeout(client.getParams(), 15000);
HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
HttpUriRequest request = new HttpGet(SelectMenuAPI);
HttpResponse response = client.execute(request);
InputStream atomInputStream = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(
atomInputStream));
String line;
String str = "";
while ((line = in.readLine()) != null) {
str += line;
}
HttpClient client2 = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client2.getParams(),
15000);
HttpConnectionParams.setSoTimeout(client2.getParams(), 15000);
HttpUriRequest request2 = new HttpGet(SelectMenuAPI2);
HttpResponse response2 = client2.execute(request2);
InputStream atomInputStream2 = response2.getEntity().getContent();
BufferedReader in2 = new BufferedReader(new InputStreamReader(
atomInputStream2));
String line2;
String str2 = "";
while ((line2 = in2.readLine()) != null) {
str2 += line2;
}
JSONObject json3 = new JSONObject(str2);
// message = json2.getString("message");
status = json3.getString("status");
if (status.equals("1")) {
JSONArray school2 = json3.getJSONArray("data");
String[] mVal = new String[school2.length()];
for (int i = 0; i < school2.length(); i++) {
mVal[i] =
school2.getJSONObject(i).getString("title");
Elementarytxt.setText(mVal[0]);
Middletxt.setText(mVal[1]);
Hightxt.setText(mVal[2]);
Atypicaltxt.setText(mVal[3]);
}
elementarytxtview = mVal[0];
}
JSONObject json2 = new JSONObject(str);
// message = json2.getString("message");
status = json2.getString("status");
if (status.equals("1")) {
// JSONObject data = json.getJSONObject("data");
JSONArray school = json2.getJSONArray("data");
for (int i = 0; i < school.length(); i++) {
JSONObject object = school.getJSONObject(i);
//
Category_ID.add(Long.parseLong(object.getString("id")));
Category_ID.add((long) i);
Category_name.add(object.getString("title"));
}
} else {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
// IOConnect = 1;
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
elementarytxtview
展示之前,未初始化 Toast
。首先,你需要在其中加入一些价值。
答案 1 :(得分:0)
我不是100%肯定你的意思。
但是如果你想在类之间轻松访问变量,你应该String elementarytxtview;
public static
因此:public static String elementarytxtview;
这意味着基本上variable
是共享的,可以在所有类之间使用。只需在您的案例中写下HomeMenu.elementarytxtview;
即可。
答案 2 :(得分:0)
您在onCreate
方法之前运行的parseJSONData()
方法中显示Toast。您尚未初始化String elementarytxtview
因此它将显示null,因为您只声明了该变量。