我有一个活动,其中包含一个包含回收者视图的片段,我使用回收器视图适配器用数据填充回收器视图,我使用包含2个文本视图和3个按钮的布局,当旋转模拟器语言改变时,我保存活动中片段的状态,语言没有改变,但按钮上的文字没有出现我尝试保存片段中的文本状态但我得到按钮对象的错误空指针异常
片段代码
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.daily_azkaar_fragment,container, false);
rv = (RecyclerView) view.findViewById(R.id.recyclerviewcard);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(rv);
activity = getActivity().getIntent().getIntExtra("activity", 3);
position = getActivity().getIntent().getIntExtra("position", 3);
rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
rv.setLayoutManager(layoutManager);
rv.setHasFixedSize(true);
rv.scrollToPosition(position);
settings= getActivity().getSharedPreferences("MyPrefs", 0);
favourite=loadArray("fav",getActivity());
favourite_evening=loadArray("fav_evening",getActivity());
for(int i=0;i<favourite.length;i++)
Log.d("msg ",String.valueOf(favourite[i]));
if (settings.getBoolean("is_first_time_presses", true)) {
//the app is being launched for first time, do something
Log.d("TAG", "First time Presses");
settings.edit().putBoolean("is_first_time_presses", false).commit();
storePresses(presses,"presses",0);
storePresses(presses_evening,"presses_evening",0);
} else {
//second time launch..
presses=loadPresses("presses");
presses_evening=loadPresses("presses_evening");
for(int i=0;i<presses.length;i++)
Log.d("presses ",String.valueOf(presses[i]));
}
if (settings.getBoolean("is_sett", true)) {
//the app is being launched for first time, do something
Log.d("TAG", "First time");
settings.edit().putBoolean("is_sett", false).commit();
storeArraySettings(settingsArray,"sett",true,getActivity());
} else {
//second time launch..
settingsArray=loadArray("sett",getActivity());
for(int i=0;i<settingsArray.length;i++)
Log.d("msg ",String.valueOf(settingsArray[i]));
}
if(activity == 3){
azkarForms=getSunsetFavourites("fav","presses", this.getContext());
adapter=new RecyclerFavouriteAdapter(azkarForms,getActivity(),activity,0);
rv.setAdapter(adapter);
adapter.setClickListener(this);
}
else{
azkarForms=getSunsetFavourites("fav_evening","presses_evening", this.getContext());
adapter=new RecyclerFavouriteAdapter(azkarForms,getActivity(),activity,0);
rv.setAdapter(adapter);
Button next=view.findViewById(R.id.next);
if(savedInstanceState != null)
next.setText(savedInstanceState.getString("next"));
adapter.setClickListener(this);
}
return view;
}
private ArrayList<AzkarForm> getSunsetFavourites(String arrayName, String pressesArray, Context mContext) {
AzkaarData azkaarData = new AzkaarData(mContext);
list = new ArrayList<>();
int size = settings.getInt(arrayName + "_size", 0);
Boolean[] array = new Boolean[size];
int[] numPresses = new int[size];
ArrayList<MyFavourites> myResults = new ArrayList<MyFavourites>();
MyFavourites myFavourites;
for (int i = 0; i < size; i++) {
array[i] = settings.getBoolean(arrayName + "_" + i, true);
numPresses[i] = settings.getInt(pressesArray + "_" + i, 0);
myFavourites=new MyFavourites(i,array[i],numPresses[i]);
myResults.add(myFavourites);
}
if (myResults != null)
list = azkaarData.getArrayListDataFav(myResults,activity, 0);
return list;
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
savedInstanceState.putString("next", next.getText().toString());
}
适配器代码
} else if (activity == 3) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_details,parent,false);
holder=new MyViewHolder(view,activity,c);
return holder;
} else if (activity == 4) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_details,parent,false);
holder=new MyViewHolder(view,activity,c);
return holder;
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="6">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:weightSum="6">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/icon"
android:layout_weight="2"
android:src="@drawable/favorite_border"
android:clickable="true"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="@string/add_zekr_txt"
android:textSize="26sp"/>
</LinearLayout>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/cardview"
android:layout_weight="4">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:id="@+id/relative">
<TextView
android:id="@+id/sample"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:text="@string/sample_zekr"
android:textIsSelectable="true"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_below="@+id/cardview"
android:weightSum="5">
<Button
android:text="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/count"
android:layout_weight="2"
android:layout_gravity="center"
android:background="@drawable/btnborder"
android:layout_margin="10dp"/>
<Button
android:text="@string/num_press"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/press"
android:layout_weight="1"
android:background="@drawable/btncircle"
android:layout_margin="10dp"/>
<Button
android:text="@string/next"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/next"
android:layout_weight="2"
android:background="@drawable/btnborder"
android:layout_margin="10dp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
片段布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerviewcard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
答案 0 :(得分:0)
使用覆盖方法onConfigurationChanged()
来帮助您解决方向更改问题。