1

我有一个带有标签的应用程序,在一个标签下我有一个listfragment,如果我单击一个 listfragment,我会来到另一个 listfragment,如果我点击一个项目,我会来到一个以编程方式添加了 textviews 的片段。如果我单击另一个选项卡,我会使用以下行:

container.removeAllViews();

使选项卡之间的切换正常工作。

现在我想button在具有的片段中实现 a textviews,我想要一个变量,如果按下按钮,我可以 +1 并且我想记住这一点,因为我总是想显示在另一个选项卡中按下了多少按钮. 我还想存储按下或未按下按钮。

这样做的最佳方法是什么?有没有可能我正在使用这条线

container.removeAllViews()?

所以我调查过SharedPreferences,很多例子里面都有按钮,activity但我Buttons在其他里面Fragments。从这些片段中,我想为每个保存状态(保存它被按下)Button并在按下任何一个时增加一个变量Button

当我运行应用程序并按下按钮时,我收到一个 NullPointerException 错误,并且主线程被挂起并且调试器在该行上抱怨:

preferences.savePrefs("BUTTON", true);

现在有人为什么或如何解决这个问题?


编辑:如果我没有SharedPrefererences作为自己的类并且 insted 将这三个方法放在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();
    }   
}
4

1 回答 1

1

为您的应用保留任何数据/变量值的最佳方法是将其保存在 sharedPreference 中。

参考:http: //developer.android.com/reference/android/content/SharedPreferences.html

完整的细节。

存储在 sharedPrefrence 中的值将保留,直到用户卸载应用程序。

于 2013-04-30T09:03:28.887 回答