我想在Edittext中加载数据,其中值保存在sharedpreference中,但数据未加载

时间:2017-03-28 15:27:10

标签: php android android-fragments sharedpreferences android-volley

package in.co.mdabba.m_dabbawala;

 import android.content.Context;
 import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



 public class Fragment_MyProfile extends Fragment {


public EditText Firstname,Lastname,Phoneno;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 View view = inflater.inflate(R.layout.fragment_myprofile, container, false);
   run();
    return view;
}

public void run(){
    SharedPreferences sharedPreferences = getActivity().getSharedPreferences("logininfo", Context.MODE_PRIVATE);
    String emailid = sharedPreferences.getString("emailid","");
    Firstname =(EditText) getView().findViewById(R.id.firstname);
    Lastname = (EditText) getView().findViewById(R.id.lastname);
    Phoneno = (EditText)getView().findViewById(R.id.phoneno);
    String b1url =  "https://e705bb27.ngrok.io/1/b1.php?emailid="+emailid;
    StringRequest stringRequest = new StringRequest(Request.Method.GET, b1url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try{
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray result = jsonObject.getJSONArray("result");
                        JSONObject Data = result.getJSONObject(0);
                        String f_name = Data.getString("First_name");
                        String l_name =Data.getString("Last_name");
                        String phone_no = Data.getString("Phone_no");
                        Firstname.setText(f_name);
                        Lastname.setText(l_name);
                        Phoneno.setText(phone_no);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            },new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(),"Something went wrong try again later!!!",Toast.LENGTH_LONG).show();
            error.printStackTrace();
        }
    });
    mysingleton.getInstance(getActivity()).addtoRequest(stringRequest);
}
}

我想在登录后加载用户详细信息表单,我已经使用sharedpreferences来存储电子邮件地址,这将用于完成php查询并在Edittext中加载数据但登录后我的应用程序崩溃并且即时通讯使用齐射库用于重新获取数据。logcat

Fragment_myproflie.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lightgrey"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="Edit Profile"
    android:textSize="22sp"
    android:textStyle="bold" />
 <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/white"
android:orientation="vertical">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:text="First Name:"
    android:textSize="12sp" />
<EditText
    android:id="@+id/firstname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="dd"
    android:textColor="#000000"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:text="Last Name:"
    android:textSize="12sp" />
<EditText
    android:id="@+id/lastname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:text="I"
    android:textColor="#000000"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:text="Phone no:"
    android:textSize="12sp" />
<EditText
    android:id="@+id/phoneno"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:text="lv"
    android:textColor="#000000"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="13dp"
    android:background="#000080"
    android:text="@string/UpdateDetails"
    android:textColor="#FFFFFF"
    android:textStyle="bold" />
</LinearLayout>

</ScrollView>

3 个答案:

答案 0 :(得分:1)

当你调用getView()时,你还没有设置View,将视图作为参数传递给run方法并使用该视图而不是getView()

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 View view = inflater.inflate(R.layout.fragment_myprofile, container, false);
   run(view);
    return view;
}
public void run(View view){
 SharedPreferences sharedPreferences = getActivity().getSharedPreferences("logininfo", Context.MODE_PRIVATE);
 String emailid = sharedPreferences.getString("emailid","");
 Firstname =(EditText) view.findViewById(R.id.firstname);
}

答案 1 :(得分:0)

我认为您可能正在获取此记录,因此在使用 setText之前确保值不为空并且您正在重新搜索的值不匹配,即

def main(args):
    # Set model params
    model_params = {"learning_rate": 0.1}
    # Create a RunConfig instance
    r_config = run_config.RunConfig(gpu_memory_fraction=0.6)
    #Create and fit estimator
    nn = tf.contrib.learn.Estimator(model_fn=model_fn, params=model_params, config=r_config)
    nn.fit(input_fn=input_fn, steps=5000)

答案 2 :(得分:0)

可能getView()为空,您在run()方法中使用它。

Firstname =(EditText) getView().findViewById(R.id.firstname);
Lastname = (EditText) getView().findViewById(R.id.lastname);
Phoneno = (EditText)getView().findViewById(R.id.phoneno);

创建一个全局字段View view,并在您的onCreateView()方法中,将您的视图分配给此全局view,并稍后将其用于绑定您的观看,例如

Firstname =(EditText) view.findViewById(R.id.firstname);