我是 android 开发的新手,我被困在一个问题上,我需要使用共享首选项。
主.java
WebServiceInterface webinterface;
ProgressDialog progdial;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
handler = new Handler();
webinterface= new WebServiceInterface(wslogin);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getuser = username.getText().toString();
getpass = password.getText().toString();
/*****
* setting values in HASHMAP
*
*
*/
HashMap<String, Object> map = new HashMap<String,Object>();
map.put("username", getuser);
map.put("password", getpass);
/*****
* calling sendRequest() method in WebInterface class
*
*/
webinterface.sendRequest(map);
//for getting values from preferences
SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);
String getResponse = prefs.getString("responsevalue", "");
//but its getting error as showing NULLPOINTERException.
System.out.Println("response is ----- " + getResponse);
//here i am getting null value for gerResponse. .....
}
});
}
WebInterface.java(只有类不是活动)
public class WebServiceInterface{
public WebServiceInterface(String getstring)
{
getWebServiceType = getstring;
}
public void sendRequest(Map<String, Object> map) {
// TODO Auto-generated method stub
Collection<Object> strings = map.values();
Iterator iterator = strings.iterator();
while (iterator.hasNext()) {
getuser = (String) iterator.next();
ast.add(getuser);
}
String password = ast.get(0).toString();
String username = ast.get(1).toString();
//calling webservice client to request webserivce code
RestClient client = new RestClient(
"http://**********************8");
client.AddParam("username", username);
client.AddParam("password", password);
try {
client.Execute(RequestMethod.POST);
} catch (Exception e) {
e.printStackTrace();
}
//getting response back from Restclient and saving in response string.
String response = client.getResponse();
//for setting values in preferences
SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("responsevalue", response);
editor.commit();
}
这是我面临的问题:
我想访问 Main.java 文件的 onCreate 块中的响应。在按钮单击方法中的 Main.java 文件中,我需要在 Webinterface 类中显示带有以下响应的 toast。
谁能帮我解决这个问题。由于我是 android 开发的新手,任何小的帮助将不胜感激。
以下是堆栈跟踪..
10-29 13:07:44.042: I/global(379): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
10-29 13:07:44.062: D/AndroidRuntime(379): Shutting down VM
10-29 13:07:44.062: W/dalvikvm(379): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-29 13:07:44.072: E/AndroidRuntime(379): FATAL EXCEPTION: main
10-29 13:07:44.072: E/AndroidRuntime(379): java.lang.NullPointerException
10-29 13:07:44.072: E/AndroidRuntime(379): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
10-29 13:07:44.072: E/AndroidRuntime(379): at com.leads.doctorgokhale.Interface.WebServiceInterface.call(WebServiceInterface.java:44)
10-29 13:07:44.072: E/AndroidRuntime(379): at com.leads.doctorgokhale.Interface.WebServiceInterface.sendRequest(WebServiceInterface.java:39)
10-29 13:07:44.072: E/AndroidRuntime(379): at com.leads.doctorgokhalenative.Main$2.onClick(Main.java:154)
10-29 13:07:44.072: E/AndroidRuntime(379): at android.view.View.performClick(View.java:2408)
10-29 13:07:44.072: E/AndroidRuntime(379): at android.view.View$PerformClick.run(View.java:8816)
10-29 13:07:44.072: E/AndroidRuntime(379): at android.os.Handler.handleCallback(Handler.java:587)
10-29 13:07:44.072: E/AndroidRuntime(379): at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 13:07:44.072: E/AndroidRuntime(379): at android.os.Looper.loop(Looper.java:123)
10-29 13:07:44.072: E/AndroidRuntime(379): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-29 13:07:44.072: E/AndroidRuntime(379): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 13:07:44.072: E/AndroidRuntime(379): at java.lang.reflect.Method.invoke(Method.java:521)
10-29 13:07:44.072: E/AndroidRuntime(379): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-29 13:07:44.072: E/AndroidRuntime(379): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-29 13:07:44.072: E/AndroidRuntime(379): at dalvik.system.NativeStart.main(Native Method)
10-29 13:07:44.143: D/dalvikvm(379): GC_FOR_MALLOC freed 3188 objects / 215024 bytes in 64ms