【问题标题】:Android java.lang.NullPointerException trying to use a custom layout on preferenceactivityAndroid java.lang.NullPointerException 试图在preferenceactivity上使用自定义布局
【发布时间】:2014-10-28 01:12:29
【问题描述】:

我正在尝试在我的preferenceActivity上使用自定义布局来显示用户选择的内容,我不知道我在这里缺少什么,但似乎textview为空,我不知道为什么,日志是这样写的:

java.lang.NullPointerException
            at br.com.representemais.FiltrarActivity$1.onPreferenceChange(FiltrarActivity.java:106)

FiltrarActivity.java:106 = textValue.setText(pData.getEntries()[index]);

@SuppressWarnings("ALL")
public class FiltrarActivity extends PreferenceActivity {

    private String preferencesName = "";

    private ActionBar actionBar;
    private String defaultValue;

    private ListPreference pData;
    private ListPreference pStatus;
    private TextView textValue;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final SecurePreferences mSessao = new SecurePreferences(getApplicationContext(), "sessao");
        String menuAtual = (mSessao.getString("menuAtual") != null) ? mSessao.getString("menuAtual") : "Pedidos";

        setTitle("Filtrar " + menuAtual);


        actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        preferencesName = getIntent().getExtras().getString(NavigationMain.FILTRO);
        // set the preferences file name
        getPreferenceManager().setSharedPreferencesName(preferencesName);



        if (menuAtual.equals("Viagens")) {

        } else if (menuAtual.equals("Pedidos")) {

            addPreferencesFromResource(R.xml.filtrar);


            pData = (ListPreference) findPreference("prefData");

            pData.setLayoutResource(R.layout.pref_data);



            textValue = (TextView) findViewById(R.id.escolhaData);




            pData.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                // @Override
                public boolean onPreferenceChange(Preference preference,
                                                  Object newValue) {


                    int index = pData.findIndexOfValue(newValue.toString());


                    if (index != -1) {

                        SharedPreferences customSharedPreference = getSharedPreferences("prefData", 0);
                        SharedPreferences.Editor editor = customSharedPreference.edit();
                        editor.putString("listentries", (String) newValue);
                        editor.commit();

                        SecurePreferences mSessao = new SecurePreferences(getApplicationContext(), "sessao");
                        mSessao.put("filtro_pedidos_data", (String) newValue);



                        textValue.setText(pData.getEntries()[index]);
                    }


                    System.out.println("Escolheu >>> " + mSessao.getString("filtro_pedidos_data"));

                    return true;
                }
            });


            pStatus = (ListPreference) findPreference("prefStatus");
            pStatus.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                // @Override
                public boolean onPreferenceChange(Preference preference,
                                                  Object newValue) {
                    SharedPreferences customSharedPreference = getSharedPreferences("prefStatus", Activity.MODE_PRIVATE);
                    SharedPreferences.Editor editor = customSharedPreference.edit();
                    editor.putString("listStatus", (String) newValue);
                    editor.commit();

                    System.out.println("Escolheu >>> " + newValue);

                    return true;
                }
            });



           Preference mResetButton = findPreference("Reset");

            mResetButton.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference arg0) {
                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(FiltrarActivity.this);
                    alertDialog.setMessage("Você deseja realmente limpar os filtros atuais?");
                    alertDialog.setTitle("Filtro");
                    alertDialog.setCancelable(true);
                    alertDialog.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            SharedPreferences settings =   PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                            SharedPreferences.Editor editor = settings.edit();
                            editor.clear();
                            editor.commit();
                        } });
                    alertDialog.setNegativeButton("Não", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        } });
                    alertDialog.show();
                    return false;
                }
            });



        } else if (menuAtual.equals("Clientes")) {// codigo

        }


    }





    public interface BackPressedListener {
        void onBackPressed();

    }


    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public boolean onOptionsItemSelected(MenuItem item) {

        Fragment fragment = getFragmentManager().findFragmentByTag("");
        if (fragment != null && fragment instanceof BackPressedListener) {
            ((BackPressedListener) fragment).onBackPressed();

        } else {
            super.onBackPressed();
            overridePendingTransition(R.anim.animation_back, R.anim.animation_back_leave);
        }
        return true;

    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void onBackPressed() {

        Fragment fragment = getFragmentManager().findFragmentByTag("");
        if (fragment != null && fragment instanceof BackPressedListener) {
            ((BackPressedListener) fragment).onBackPressed();

        } else {
            super.onBackPressed();
            overridePendingTransition(R.anim.animation_back, R.anim.animation_back_leave);
        }
    }


}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2"
        android:gravity="center"
        android:paddingTop="10dp"
        android:paddingBottom="10dp">


        <TextView
            android:id="@+id/filtrarData"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Refinar Data"
            android:textColor="@color/black"
            android:textSize="@dimen/detalhe_cliente"
            android:entries="@array/listentries"
            android:entryValues="@array/listvalues"
            android:layout_weight="1"
            android:padding="10dp"
            android:gravity="left" />

        <TextView
            android:id="@+id/escolhaData"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text=""
            android:textColor="@color/blue_dark"
            android:textSize="@dimen/detalhe_cliente"
            android:entries="@array/listentries"
            android:entryValues="@array/listvalues"
            android:layout_weight="1"
            android:padding="10dp"
            android:gravity="right" />





    </LinearLayout>



</LinearLayout>

pref_xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="@string/fDataTitulo">

        <ListPreference


            android:dialogTitle="@string/fDataTitulo"
            android:summary="@string/fDataSummary"
            android:key="prefData"
            android:entries="@array/listentries"
            android:entryValues="@array/listvalues"
            android:defaultValue="Mais Antiga"/>


    </PreferenceCategory>

    <PreferenceCategory android:title="@string/fValorTitulo">

    <br.com.representemais.MyDialogShow
        android:dialogTitle="@string/fValorTitulo"
        android:key="prefValor"
        android:summary="Entre o valor desejado"
        android:positiveButtonText="OK"
        android:negativeButtonText="Cancel" />

    </PreferenceCategory>


    <PreferenceCategory android:title="@string/fStatusTitulo">

            <ListPreference

            android:dialogTitle="@string/fStatusTitulo"
            android:summary="@string/fStatusSummary"
            android:key="prefStatus"
            android:entries="@array/listStatus"
            android:entryValues="@array/list_status_values"/>

    </PreferenceCategory>

    <PreferenceCategory android:title="@string/fReset">

        <Preference
            android:layout="@layout/dialog_reset"
            android:key="Reset"
            android:enabled="true"


            />


    </PreferenceCategory>





</PreferenceScreen>

【问题讨论】:

    标签: java android android-layout android-custom-view android-preferences


    【解决方案1】:

    刚刚解决了,我只好直接在pref.xml中设置布局:

     <PreferenceCategory android:title="@string/fDataTitulo">
        <ListPreference
    
    
            android:dialogTitle="@string/fDataTitulo"
            android:summary="@string/fDataSummary"
            android:key="prefData"
            android:layout="@layout/pref_data"
            android:entries="@array/listentries"
            android:entryValues="@array/listvalues"
            android:defaultValue="Mais Antiga"/>
    
    
    </PreferenceCategory>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多