gpt4 book ai didi

java - android fragment 无法设置按钮文本

转载 作者:行者123 更新时间:2023-12-01 21:40:53 25 4
gpt4 key购买 nike

我有一个奇怪的问题。我已经设置了 fragment ,并且正在加载 Volley 数据库,并将详细信息放入 fragment 中。但是,当我尝试将从 json 获取的数据设置到按钮和 TextView 中时,它不起作用。它也没有显示任何错误。因此,我记录其中一个按钮的输出并获取以下日志:

D/Homepage points: android.support.v7.widget.AppCompatButton{98f2495 VFED..C.. ......I. 0,0-0,0 #7f0c00a0 app:id/pointsNr}

我不知道这意味着什么。下面是我从数据库获取项目并填充按钮的发布请求

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);


}

当我在另一个不是 fragment 的 Activity 上运行它时,它会执行它应该执行的操作。但在 fragment 中一切似乎都不同了。

下面是 fragment 的 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>

我需要构建很多 fragment ,所以我真的很想知道我在这里做错了什么。

这是更清晰的完整代码

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);


}
}

谢谢

最佳答案

将你的 onCreateView 更改为此..

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 ;

关于java - android fragment 无法设置按钮文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36475486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com