android片段无法设置按钮文字

时间:2016-04-07 11:57:15

标签: java android json android-fragments

我有一个奇怪的问题。我有设置片段,我正在加载volley数据库,细节到片段。但是,当我尝试将我从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);


    }

当我在另一个不是片段的活动上运行它时,它会执行它应该做的事情。但在片段中,一切似乎都不同。

下面是片段的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);


    }
}

由于

1 个答案:

答案 0 :(得分:3)

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