1

我是 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
4

7 回答 7

0

Write below code line

editor.putString("responsevalue", response);

instead of

editor.putFloat("responsevalue", response);

Add Below code into your onCreate() method after setcontentview();.

username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);

it will solve your problem.

于 2012-10-29T07:09:46.743 回答
0

我的猜测prefNamenullgetResponse不应该为空,因为你给它一个空字符串的后备。

于 2012-10-29T07:14:55.960 回答
0

尝试这个

SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    if (response == null)
        editor.putString("responsevalue", "response is null");
    else
        editor.putString("responsevalue", response);
    editor.commit();
于 2012-10-29T07:16:58.640 回答
0

尝试这个。

更换

editor.putFloat("responsevalue", response); to below code

editor.putString("responsevalue", response);

您正在尝试将字符串添加到浮点数并取回字符串,因此您会收到错误消息。

编辑 :

您在初始化之前访问提交(按钮 onClick 事件),所以请初始化它。对用户名、密码编辑文本执行相同操作。

submit   = (Button)findViewById(R.id.submit);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
于 2012-10-29T07:06:42.400 回答
0

利用

放字符串

代替

putFloat

于 2012-10-29T07:14:03.867 回答
0

prefName 在

SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);

保持任何合理的值,还是未初始化的变量?

于 2012-10-29T08:21:25.310 回答
0

将上下文对象从 Main.java (Activity) 传递给您的 WebInterface

webinterface=   new WebServiceInterface(this,wslogin);

将构造函数更改为:

Context mContext;
public WebServiceInterface(Context context, String getstring)
{       
    mContext =context;
    getWebServiceType = getstring;      
}

从您的 WebServiceInterface 显示吐司:

 String response = client.getResponse();
 Toast toast = Toast.makeText(mContext, response, duration);
 toast.show();
于 2015-05-14T16:45:43.103 回答