私は奇妙な問題があります。私はセットアップの断片を持っており、私はバレーのデータベース、断片に詳細を読み込んでいます。しかし、私がjsonからボタンやテキストビューに入っているデータを設定しようとすると、うまくいきません。また、エラーは表示されません。だから私は、ボタンの1つの出力を記録し、次のログを取得しています:アンドロイドのフラグメントはボタンのテキストを設定できません
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);
}
をデータベースから項目を取得し、ボタンを移入することで、それが行うことになって何を行います。しかし、この断片ではすべてが異なるようです。
は、以下の断片、私は破片の多くを構築する必要があり
<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>
のxmlですので、私は本当に私がここで間違ってやっているかを知りたいと思います。
これは、より明確化のための完全なコードは
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);
}
}
おかげ
あなたがでstringRequestコールを作っているのですか?フラグメントビューは、ビューの作成後(onViewCreatedイベントまたはonActivityAttachedイベント)までアクセスまたは変更することはできません。基本的に、アクティビティにアタッチされていない場合、ビューは作成されず、ビューにアクセスすることはできません。残念ながらそれがあなたの問題なのかどうかはわかりません。 – aminner
私はちょうどそれが不思議だった。私はそれをoncreateから呼び出していて、oncreateviewの前に要求を実行するかもしれないと考えました。だから今私はそれをテストしています。 – carlosx2
それはあなたの問題になります。あなたはレイアウトインフレータの後にそれを行う必要があります。私は一般的に@Overrideでそのタイプのロジックを行います public void onViewCreated(ビュービュー、Bundle savedInstanceState)もう一つのオプションは、onCreateでそれを行い、オブジェクトにレスポンスを保存し、ビューが作成された後にのみテキストを設定することです。 http://developer.android.com/guide/components/fragments.htmlにはライフサイクルがあり、それがどのように機能するかについてはかなり良い説明です。 – aminner