douya5194 2017-03-28 15:27
浏览 105
已采纳

我想在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 2017-03-28 15:52
    关注

    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);
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助