Java 安卓碎片可以';设置按钮文本
我有个奇怪的问题。我已经安装了片段,我正在加载截击数据库,将详细信息加载到片段中。但是,当我试图将从json获取的数据设置到按钮和文本视图中时,它不起作用。而且它没有显示任何错误。因此,我记录其中一个按钮的输出,并获得以下日志:Java 安卓碎片可以';设置按钮文本,java,android,json,android-fragments,Java,Android,Json,Android Fragments,我有个奇怪的问题。我已经安装了片段,我正在加载截击数据库,将详细信息加载到片段中。但是,当我试图将从json获取的数据设置到按钮和文本视图中时,它不起作用。而且它没有显示任何错误。因此,我记录其中一个按钮的输出,并获得以下日志: D/Homepage points: android.support.v7.widget.AppCompatButton{98f2495 VFED..C.. ......I. 0,0-0,0 #7f0c00a0 app:id/pointsNr} 我不知道这意味着什么。
D/Homepage points: android.support.v7.widget.AppCompatButton{98f2495 VFED..C.. ......I. 0,0-0,0 #7f0c00a0 app:id/pointsNr}
我不知道这意味着什么。下面是我从数据库中获取项目并填充按钮的post请求
StringRequest stringRequest = new StringRequest(Request.Method.POST, POST_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Homepage RESPONSE", response);
try {
JSONObject jsonResponse = new JSONObject(response);
String getusername = jsonResponse.getString("username");
String getfollowers = jsonResponse.getString("followers");
String getdonate = jsonResponse.getString("collected");
String gettotalkm = jsonResponse.getString("total_km");
String getpoints = jsonResponse.getString("points");
System.out.println("Homepage Username: " + getusername);
username.setText("Welkom " + getusername);
followersNr.setText(getfollowers);
donatedNr.setText(getdonate);
totalKmNr.setText(gettotalkm);
pointsNr.setText(getpoints);
Log.d("Homepage points raw", "" + getpoints);
Log.d("Homepage points", ""+pointsNr);
} catch (JSONException e) {
e.printStackTrace();
}
//Intent i = new Intent(login.this, login.class);
// startActivity(i);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put(POST_USEREMAIL, emailUser);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
requestQueue.add(stringRequest);
}
StringRequest StringRequest=新的StringRequest(Request.Method.POST,POST\u URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Log.d(“主页响应”,响应);
试一试{
JSONObject jsonResponse=新的JSONObject(响应);
字符串getusername=jsonResponse.getString(“用户名”);
String getfollowers=jsonResponse.getString(“followers”);
字符串getDevotal=jsonResponse.getString(“已收集”);
字符串gettotalkm=jsonResponse.getString(“总公里”);
字符串getpoints=jsonResponse.getString(“点”);
System.out.println(“主页用户名:“+getusername”);
username.setText(“Welkom”+getusername);
followersNr.setText(getfollowers);
捐赠编号setText(GetDevot);
totalKmNr.setText(gettotalkm);
pointsNr.setText(getpoints);
Log.d(“主页原始点”,“获取点”);
Log.d(“主页点”和“+pointsNr”);
}捕获(JSONException e){
e、 printStackTrace();
}
//Intent i=新的Intent(login.this,login.class);
//星触觉(i);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG.show();
}
}) {
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
参数put(POST_USEREMAIL,emailUser);
返回参数;
}
};
RequestQueue RequestQueue=Volley.newRequestQueue(getActivity().getApplicationContext());
添加(stringRequest);
}
当我在另一个不是片段的活动上运行它时,它会执行它应该执行的操作。但在片段中,一切似乎都不同了
下面是片段的xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.befitdonate.befitdonate.HomePage">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:text="@string/user"
android:textSize="18sp"
android:textColor="@color/colorBlack"
android:layout_marginTop="0dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/frontheader"
android:scaleType="fitStart"
android:adjustViewBounds="true" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_weight="0.14"
android:columnCount="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/followersNr"
android:layout_width="70dp"
android:layout_height="70dp"
android:textColor="@color/colorWhite"
android:background="@drawable/polygon"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/donatedNr"
android:layout_width="70dp"
android:layout_height="70dp"
android:textColor="@color/colorWhite"
android:background="@drawable/polygon"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/totalKmNr"
android:layout_width="70dp"
android:layout_height="70dp"
android:textColor="@color/colorWhite"
android:background="@drawable/polygon"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/pointsNr"
android:layout_width="70dp"
android:layout_height="70dp"
android:textColor="@color/colorWhite"
android:background="@drawable/polygon"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</GridLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/followersLink"
android:layout_width="70dp"
android:layout_height="25dp"
android:text="Volgers"
android:textAllCaps="false"
android:textSize="13sp"
android:background="@null"
android:textColor="@color/colorLightBlue"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/donatedLink"
android:layout_width="70dp"
android:layout_height="25dp"
android:text="Gedoneerd"
android:textAllCaps="false"
android:textSize="13sp"
android:background="@null"
android:textColor="@color/colorLightBlue"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/totalKmLink"
android:layout_width="70dp"
android:layout_height="25dp"
android:text="Total Km"
android:textAllCaps="false"
android:textSize="13sp"
android:background="@null"
android:textColor="@color/colorLightBlue"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/pointsLink"
android:layout_width="70dp"
android:layout_height="25dp"
android:text="Punten"
android:textAllCaps="false"
android:textSize="13sp"
android:background="@null"
android:textColor="@color/colorLightBlue"
/>
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
我需要构建很多片段,所以我很想知道我做错了什么
这是详细说明的完整代码
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class HomePage extends Fragment {
TextView username;
Button followersNr;
Button donatedNr;
Button totalKmNr;
Button pointsNr;
Button followersText;
Button donatedText;
Button totalKmText;
Button pointsText;
private SharedPreferences preferenceSettings;
private SharedPreferences.Editor preferenceEditor;
private static final int PREFERENCE_MODE_PRIVATE = 0;
private static final String PREF_NAME = "UserDetails";
String emailUser;
private static String TAG = MainActivity.class.getSimpleName();
public static final String POST_USEREMAIL = "username";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
preferenceSettings = this.getActivity().getSharedPreferences(PREF_NAME, PREFERENCE_MODE_PRIVATE);
preferenceEditor = preferenceSettings.edit();
emailUser = preferenceSettings.getString("Email", null);
Log.d("Saved user email:", "" + emailUser);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LayoutInflater lf = getActivity().getLayoutInflater();
View view = lf.inflate(R.layout.fragment_home_page, container, false);
FrameLayout ll = (FrameLayout)inflater.inflate(R.layout.fragment_home_page, container, false);
username = (TextView) ll.findViewById(R.id.username);
followersNr = (Button) ll.findViewById(R.id.followersNr);
donatedNr = (Button)ll.findViewById(R.id.donatedNr);
totalKmNr = (Button) ll.findViewById(R.id.totalKmNr);
pointsNr = (Button) ll.findViewById(R.id.pointsNr);
followersText = (Button) ll.findViewById(R.id.followersNr);
donatedText = (Button) ll.findViewById(R.id.donatedLink);
totalKmText = (Button) ll.findViewById(R.id.totalKmLink);
pointsText = (Button) ll.findViewById(R.id.pointsLink);
homePageDetails();
return inflater.inflate(R.layout.fragment_home_page, container, false);
}
////////////////////////// get user details //////////////////////////////////////////////
public void homePageDetails() {
Log.d("Homepage USEREMAIL", " " + emailUser);
String POST_URL = "";
StringRequest stringRequest = new StringRequest(Request.Method.POST, POST_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Homepage RESPONSE", response);
try {
JSONObject jsonResponse = new JSONObject(response);
String getusername = jsonResponse.getString("username");
String getfollowers = jsonResponse.getString("followers");
String getdonate = jsonResponse.getString("collected");
String gettotalkm = jsonResponse.getString("total_km");
String getpoints = jsonResponse.getString("points");
System.out.println("Homepage Username: " + getusername);
username.setText("Welkom " + getusername);
followersNr.setText(getfollowers);
donatedNr.setText(getdonate);
totalKmNr.setText(gettotalkm);
pointsNr.setText(getpoints);
Log.d("Homepage points raw", "" + getpoints);
Log.d("Homepage points", ""+pointsNr);
} catch (JSONException e) {
e.printStackTrace();
}
//Intent i = new Intent(login.this, login.class);
// startActivity(i);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put(POST_USEREMAIL, emailUser);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
requestQueue.add(stringRequest);
}
}
导入android.content.Context;
导入android.content.SharedReferences;
导入android.net.Uri;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.FrameLayout;
导入android.widget.LinearLayout;
导入android.widget.TextView;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.StringRequest;
导入com.android.volley.toolbox.volley;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.HashMap;
导入java.util.Map;
公共类主页扩展片段{
文本视图用户名;
按钮跟随器编号;
按钮捐赠;
按钮总公里数;
按钮点信噪比;
按钮跟随文本;
按钮捐赠文本;
按钮文本;
按钮点文本;
私有共享引用首选项设置;
私有共享引用编辑器首选编辑器;
私有静态最终整数首选项\模式\私有=0;
私有静态最终字符串PREF_NAME=“UserDetails”;
字符串用户;
私有静态字符串标记=MainActivity.class.getSimpleName();
公共静态最终字符串POST_USEREMAIL=“username”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
preferenceSettings=this.getActivity().GetSharedReferences(首选项名称、首选项模式和专用项);
preferenceEditor=preferenceSettings.edit();
emailUser=preferenceSettings.getString(“电子邮件”,null);
Log.d(“保存的用户电子邮件:”、“+emailUser”);
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
LayoutInflater lf=getActivity().getLayoutInflater();
视图=左前充气(R.layout.fragment\u主页,容器,假);
FrameLayout ll=(FrameLayout)充气器。充气(R.layout.fragment\u主页,容器,false);
username=(TextView)ll.findViewById(R.id.username);
followersNr=(按钮)ll.findViewById(R.id.followersNr);
donatedNr=(按钮)ll.findViewById(R.id.donatedNr);
totalKmNr=(按钮)ll.findViewById(R.id.totalKmNr);
pointsNr=(按钮)ll.findViewById(R.id.pointsNr);
LayoutInflater lf = getActivity().getLayoutInflater();
View view = lf.inflate(R.layout.fragment_home_page, container, false);
username = (TextView) view .findViewById(R.id.username);
followersNr = (Button) view .findViewById(R.id.followersNr);
donatedNr = (Button)view .findViewById(R.id.donatedNr);
totalKmNr = (Button) view .findViewById(R.id.totalKmNr);
pointsNr = (Button) view .findViewById(R.id.pointsNr);
followersText = (Button) view .findViewById(R.id.followersNr);
donatedText = (Button) view .findViewById(R.id.donatedLink);
totalKmText = (Button) view .findViewById(R.id.totalKmLink);
pointsText = (Button) view .findViewById(R.id.pointsLink);
homePageDetails();
return view ;