在android中使用全局变量时出错 [英] Error while using global variable in android
问题描述
我正在尝试实现一个登录屏幕,其中在休息服务的帮助下验证用户名和密码。
i创建了一个像这样的全局类
i am trying to implement a login screen where username and password are validated with the help of a rest service.
i have created a global class like this
public class GlobalClass extends Application{
String url="myRestService";
String newURL;
public String getName() {
return url;
}
public void setName(String aName) {
url = aName;
}
public String getNewURL() {
return newURL;
}
public void setNewURL(String bName) {
newURL = bName;
}
}
我创建了一个屏幕,其中superadmin可以更改其余服务,如
此课程旨在更新其余服务
I have created a screen where the superadmin can change the rest service like
this class is intented to update the rest service
final GlobalClass globalVariable = new GlobalClass();
String restMethod=globalVariable.getName();
et.setText(restMethod);
b= (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userEnteredMethod=et.getText().toString();
globalVariable.setNewURL(userEnteredMethod);
Toast.makeText(getApplicationContext(),userEnteredMethod,Toast.LENGTH_SHORT).show();
Intent login=new Intent(getApplicationContext(),Login.class);
startActivity(login);
}
});
现在在我的登录名单中
i称为全球像这样的类并获取URL
Now in my login
i called the global class like this and get the url
final GlobalClass gb = new GlobalClass();
userEnteredMethod = gb.getNewURL();
String newURL = userEnteredMethod.concat("/MethodResult");
AsyncHttpClient client = new AsyncHttpClient();
client.get(newURL, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
try {
// prgDialog.hide();
JSONObject obj = new JSONObject(response);
if (obj != null) {
JSONArray data = obj.getJSONArray("ValidateSubscriberLoginResult");
for (int i = 0; i < data.length(); i++) {
JSONObject user = data.getJSONObject(i);
//
if (!user.getString("ID").equals("0")) {
Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
} else
{
Toast.makeText(getApplicationContext(),"Check again",Toast.LENGTH_SHORT).show();
}
}
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
super.onSuccess(response);
}
执行代码我在字符串中获得空指针异常newURL = userEnteredMethod.concat(/ MethodResult);
在调试时我发现我在userEnteredMethod = gb.getNewURL()时得到一个空值;
请帮我纠正问题
on executing the code i'm getting a null pointer exception in String newURL = userEnteredMethod.concat("/MethodResult");
while debuging i found out that i am getting a null value at userEnteredMethod = gb.getNewURL();
please help me to rectify the problem
推荐答案
当然不行。
Of course it won't work.
final GlobalClass gb = new GlobalClass();<br />
String userEnteredMethod = gb.getNewURL();
您已实例化一个新对象。因此,它会给你 null
。 :)
You have instantiated a new object. Hence, it'll give you null
. :)
// obn button click, before the startACtivity()
login.putExtra("URL", globalVariable.getNewURL);
// in login
Bundle extras = getIntent().getExtras();
String url = extras.getString("URL");
String newURL = url.concat("/MethodResult");
AsyncHttpClient client = new AsyncHttpClient();
// your rest of the code
-KR
-KR
这篇关于在android中使用全局变量时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!