douya5194
douya5194
2017-03-28 15:27

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

已采纳
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);
}
}

I want to load the user details form mysql after login to do that i have used sharedpreferences to store email address which will be used to complete the php query and load data in Edittext but after login my app crashes and i m using volley library for retriving the data.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条回答

  • dppx9253 dppx9253 4年前

    I think you may be getting this record ,so before using setText make sure values are not null and values which you are retriving doesnt match i.e

     {"result":[{"Firstname":null,"Lastname":null,"Phoneno":null}]}     
    
     String f_name = Data.getString("First_name");//it should be Firstname
     String l_name =Data.getString("Last_name");//it should be Lastname
     String phone_no = Data.getString("Phone_no");//it should be Phoneno
    
       if(f_name!=null){
         Firstname.setText(f_name);
        }
       if(l_name!=null){
        Lastname.setText(l_name);
        } if (phone_no!=null){
         Phoneno.setText(phone_no);
       }
    
    点赞 评论 复制链接分享
  • dougan5772 dougan5772 4年前

    Probably, getView() is null, which you are using in run() method.

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

    make a global field, View view and in your onCreateView() method, assign your view to this global view and use it later for binding your views like

    Firstname =(EditText) view.findViewById(R.id.firstname);
    
    点赞 评论 复制链接分享
  • dqiaw48488 dqiaw48488 4年前

    At the moment you call getView() you havent set the View yet, pass the view as an argument to your run method and use that view instead of 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);
    }
    
    点赞 评论 复制链接分享