0

我想在我的preferenceActivity上使用自定義佈局來顯示用戶選擇了什麼,我不知道我在這裏失蹤了什麼,但似乎TextView的是空的,我不知道爲什麼,這裏是日誌說:Android java.lang.NullPointerException嘗試在偏好活動上使用自定義佈局

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

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

@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> 

回答

0

就解決了,我只好直接在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>