我的代码中没有错误,但它不会显示我希望它显示的随机字符串。我希望片段在每次加载时都有不同的字符串。我改变了我的问题.java。
question.java
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.util.Log;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.Random;
public class question extends Fragment {
private static final String TAG = MainActivity.class.getSimpleName();
private OnFragmentInteractionListener mListener;
//public randomQuestion t1 = new randomQuestion();
private String[] myQuestions;
private static final Random random = new Random();
private TextView question;
public question() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView question = (TextView)getView().findViewById(R.id.question);
EditText answere = (EditText)getView().findViewById(R.id.enterTxt);
Resources res = getResources();
myQuestions = res.getStringArray(R.array.myQuestions);
String q = myQuestions[new Random().nextInt(myQuestions.length)];
question.setText(q);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_question, container, false);
// return inflater.inflate(R.layout.fragment_question, container, false);
question = (TextView) rootView.findViewById(R.id.question);
question.setText(myQuestions[random.nextInt(myQuestions.length)]);
return rootView;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="myQuestions">
<item> String 1</item>
<item>String 2</item>
<item>String 3</item>
<item> String 4</item>
<item> String 5</item>
</string-array>
</resources>
当我打开片段时,它会出现在logcat中,它会变成红色。
06-13 10:50:53.653 13733-13784 / com.theneworange.lock E / Surface:getSlotFromBufferLocked:unknown buffer:0xaa971570
的strings.xml
<resources>
<string name="app_name">"App "</string>
<string name="turn_on">Turn On</string>
<string name="turn_off">Turn Off</string>
<string name="to_turn_on_press_turn_on_button">To turn on press Turn On button</string>
<string name="to_turn_off_press_turn_off_button">To turn off press Turn Off button</string>
<string-array name="myQuestions">
<item> String 1</item>
<item>String 2</item>
<item>String 3</item>
<item> String 4</item>
<item> String 5</item>
</string-array>
答案 0 :(得分:1)
需要将此行移动到onCreateView中,因为没有View可以获得类级别。
TextView question = (TextView)getView().findViewById(R.id.question);
所以,修复字段。像这样的东西
private String[] myQuestions;
private static final Random random = new Random();
private TextView question;
然后,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_question, container, false);
question = (TextView) rootView.findViewById(R.id.question);
question.setText(myQuestions[random.nextInt(myQuestions.length)];
return rootView;
}