タブ付きのアプリケーションがあり、1 つのタブの下にlistfragment
. 別のタブをクリックすると、次の行が使用されます。
container.removeAllViews();
タブ間の切り替えが正しく機能するようにします。
ボタンが押された場合に+1できる変数が必要であり、別のタブで押されたボタンの数を常に表示したいので、それを念頭に置いてbutton
おきたいtextviews
. また、ボタンが押されたか押されていないかを保存したいと思います。
これを行う最善の方法は何ですか?回線を使用している場合は可能ですか
container.removeAllViews()
?
だから私は調べましたがSharedPreferences
、多くの例ではボタンが の中にありますがactivity
、Buttons
他の の中にありFragments
ます。これらのフラグメントから、すべての状態を保存し(押されていることを保存)、押されButton
たときに変数を1つ増やしたいと思いButton
ます。
アプリケーションを実行してボタンを押すと、NullPointerException であるエラーが発生し、メイン スレッドが中断され、デバッガーが行に文句を言います。
preferences.savePrefs("BUTTON", true);
誰でも、なぜ、またはこれを修正する方法を教えてください。
編集:私がそれ自体のクラスとして持っていない場合、3つのメソッドをそこSharedPrefererences
に置き、それが機能するように変更します。でも、複数からセーブできるようにしたいので、そのためのクラスを持つのが一番ですよね?クラスの場合、コンテキストを取得する方法に関係があるのではないかと思います。Fragment
Button
getApplicationContext()
getActivity()
Fragments
SharedPrefererences
これFragment
はButton
:
package com.example.easysave;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PlanFragment extends Fragment implements OnClickListener{
Button testedPlan;
SharedPreferences preferences;
boolean bpressed = false;
int value = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.planera_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
final LinearLayout linearLayout = (LinearLayout)getActivity().findViewById(R.id.planera_ll);
final LinearLayout horiz = new LinearLayout(getActivity());
horiz.setOrientation(LinearLayout.HORIZONTAL);
TextView text = new TextView(getActivity());
text.setText("Planera");
Button testedPlan = new Button(getActivity());
testedPlan.setText("Tried");
testedPlan.setId(1);
testedPlan.setOnClickListener(this);
horiz.addView(text);
horiz.addView(testedPlan);
linearLayout.addView(horiz);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button button = (Button)v;
preferences.savePrefs("BUTTON", true);
button.setPressed(true);
value = value++;
preferences.savePrefs("NUMBER", value);
}
}
そして、これはSharedPreferences
package com.example.easysave;
import android.app.Activity;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
public class SharedPreferences extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
loadPrefs();
}
public void loadPrefs(){
android.content.SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean bpressed = sp.getBoolean("BUTTON", false);
int value = sp.getInt("NUMBER", 0);
}
public void savePrefs(String key, boolean value){
android.content.SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor edit = sp.edit();
edit.putBoolean(key, value);
edit.commit();
System.out.println("Sparat!" +edit.commit());
}
public void savePrefs(String key, int value){
android.content.SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor edit = sp.edit();
edit.putInt(key, value);
edit.commit();
}
}