2013-05-07 52 views
0

剛剛接觸andriod並嘗試根據共享偏好設置view.GONE。但它並沒有消失!我打破了什麼?Android查看Gone

編輯 因此,在測試下屬問題之後,設置沒有正確完成......所以,現在已經修復了,我們再試一次。他是我的onCreate ...當我刪除textview臨時代碼時,它工作正常,並通過吐司顯示pref中的更改,但是當代碼存在時應用程序崩潰。

 @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
      // Inflate the layout for this fragment  

      Boolean symptothermal = mPreferences.getBoolean("symptothermal", true); 

      if (!symptothermal) { 
       Context context = getActivity().getApplicationContext(); 
       Toast.makeText(context, "Hello toast 1!", Toast.LENGTH_LONG).show(); 
      } else { 
       Context context = getActivity().getApplicationContext(); 
       Toast.makeText(context, "Hello toast 2!", Toast.LENGTH_LONG).show(); 
      } 

      if (!symptothermal) { 
       TextView temp = (TextView) getView().findViewById(R.id.temp); 
       temp.setVisibility(View.GONE); 
      } 
     } 

He're的XML FYI:

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

    <RelativeLayout 
     android:id="@+id/relativeLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.23" > 

     <!-- Date Text --> 
     <EditText 
      android:id="@+id/dateselected" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_marginTop="10dp" 
      android:layout_toLeftOf="@+id/pickdate" 
      android:gravity="center" 
      android:inputType="date" /> 

     <!-- DatePicker button --> 
     <Button 
      android:id="@+id/pickdate" 
      android:layout_width="80dp" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/dateselected" 
      android:layout_alignBottom="@+id/dateselected" 
      android:layout_alignParentRight="true" 
      android:src="@drawable/ic_datepicker" 
      android:text="@string/date" /> 

     <!-- Temp Label --> 
     <EditText 
      android:id="@+id/temp" 
      android:layout_width="192dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/pickdate" 
      android:ems="10" 
      android:inputType="text" 
      android:text="@string/temp" 
      android:clickable="false" 
      android:cursorVisible="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:layout_marginTop="10dp"> 
     </EditText> 

     <!-- Temp field --> 
     <EditText 
      android:id="@+id/tempvalue" 
      android:layout_width="80dp" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/pickdate" 
      android:layout_toRightOf="@+id/dateselected" 
      android:ems="10" 
      android:inputType="number" 
      android:layout_marginTop="10dp" /> 

    </RelativeLayout> 

    <!-- Notes Field --> 

    <EditText 
     android:id="@+id/chartingnote" 
     android:layout_width="wrap_content" 
     android:layout_height="0dip" 
     android:layout_weight="0.58" 
     android:ems="20" 
     android:gravity="top|center_horizontal" 
     android:hint="@string/hintNote" 
     android:inputType="textMultiLine" /> 

    <!-- Update Button --> 

    <Button 
     android:id="@+id/chartingupdate" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/charting_update" /> 

</LinearLayout> 

編輯@ daniel_c05:

 package com.projectcaruso.naturalfamilyplaning; 

     import com.projectcaruso.naturalfamilyplanning.R; 

     import android.app.DatePickerDialog.OnDateSetListener; 
     import android.content.Context; 
     import android.content.SharedPreferences; 
     import android.os.Bundle; 
     import android.preference.PreferenceManager; 
     import android.support.v4.app.Fragment; 
     import android.view.LayoutInflater; 
     import android.view.View; 
     import android.view.ViewGroup; 
     import android.widget.DatePicker; 
     import android.widget.TextView; 
     import android.widget.Toast; 

     public class ChartingFragment extends Fragment implements OnDateSetListener { 
      SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
      Boolean symptothermal = mPreferences.getBoolean("symptothermal", true); 

      @Override 
      public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
       // TODO Auto-generated method stub 
      }  

      public void showDatePickerDialog(View v) { 
       DatePickerFragment newFragment = new DatePickerFragment(); 
       newFragment.show(getChildFragmentManager(), "datePicker"); 
      } 

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


       if (!symptothermal) { 
        Context context = getActivity().getApplicationContext(); 
        Toast.makeText(context, "Hello toast 1!", Toast.LENGTH_LONG).show(); 
       } else { 
        Context context = getActivity().getApplicationContext(); 
        Toast.makeText(context, "Hello toast 2!", Toast.LENGTH_LONG).show(); 
       } 
      } 



      @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
       //Inflate a view 
       View view = inflater.inflate(R.layout.fragment_charting, null); 
       //now you can initialize the TextView 
       //notice how we don't call getView()? :P 
       TextView temp = (TextView) view.findViewById(R.id.temp); 
       if (!symptothermal) { 
       //Hide the view if necessary 
         temp.setVisibility(View.GONE); 
        } 
       return view; 
       } 


     } 

日誌:

05-08 14:10:55.042: E/AndroidRuntime(22900): FATAL EXCEPTION: main 
    05-08 14:10:55.042: E/AndroidRuntime(22900): java.lang.NullPointerException 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:371) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:366) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at com.projectcaruso.naturalfamilyplaning.ChartingFragment.<init>(ChartingFragment.java:21) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at com.projectcaruso.naturalfamilyplaning.MenuFragment.onListItemClick(MenuFragment.java:36) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at android.widget.AdapterView.performItemClick(AdapterView.java:298) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at android.widget.AbsListView.performItemClick(AbsListView.java:1100) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at android.widget.AbsListView$1.run(AbsListView.java:3423) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at android.os.Handler.handleCallback(Handler.java:725) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at android.os.Handler.dispatchMessage(Handler.java:92) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at android.os.Looper.loop(Looper.java:137) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at android.app.ActivityThread.main(ActivityThread.java:5041) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at java.lang.reflect.Method.invokeNative(Native Method) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at java.lang.reflect.Method.invoke(Method.java:511) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    05-08 14:10:55.042: E/AndroidRuntime(22900): at dalvik.system.NativeStart.main(Native Method) 
+0

是您的'textview'即'temp'裏面'fragment_charting'佈局? – 2013-05-07 20:02:20

+0

theres新的xml – jcaruso 2013-05-07 20:22:05

+0

@jcaruso你測試if if語句是否返回正確的值嗎? – 2013-05-07 20:23:05

回答

1

根據我的理解,您正在使用Fragment代碼。

因此,請注意片段的生命週期與活動的生命週期不同。在onCreate期間,您的不能操縱片段上的UI元素。

您可以使用此:

if (!symptothermal) { 
      TextView temp = (TextView) getView().findViewById(R.id.temp); 
      temp.setVisibility(View.GONE); 
     } 

讓我們改變了一點,

第一步:更改Boolean symptothermal是一個實例變量,而不是onCreate的內部變量。因此,可以在不同的生命週期中使用它。

第二步:爲onCreate保留相同的代碼,除非您不調用改變TextView可見性的代碼,否則我們會將其移動到其他位置。

第三步:改寫onCreateView。

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    //Inflate a view 
    View view = inflater.inflate(R.layout.your_layout, null); 
    //now you can initialize the TextView 
    //notice how we don't call getView()? :P 
    TextView temp = (TextView) view.findViewById(R.id.temp); 
    if (!symptothermal) { 
    //Hide the view if necessary 
      temp.setVisibility(View.GONE); 
     } 
    return view; 
    } 

這應該很好。

編輯

所以我注意到你這樣做:

SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
Boolean symptothermal = mPreferences.getBoolean("symptothermal", true); 

而這就是問題所在,你之外的任何的生命週期的調用getDefaultSharedPreferences,這就是爲什麼它失敗。

看到這個:

public class ChartingFragment extends Fragment implements OnDateSetListener { 
//Don't initialize the instance variables yet 
    SharedPreferences mPreferences; 
    Boolean symptothermal; 

@Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //Here's a good moment to initialize 
    mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
    symptothermal = mPreferences.getBoolean("symptothermal", true); 

     if (!symptothermal) { 
      Context context = getActivity().getApplicationContext(); 
      Toast.makeText(context, "Hello toast 1!", Toast.LENGTH_LONG).show(); 
     } else { 
      Context context = getActivity().getApplicationContext(); 
      Toast.makeText(context, "Hello toast 2!", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 
+0

丹尼爾看到新的編輯,我發佈完整的代碼...當我運行它崩潰時,我得到這個片段。我錯過了什麼? – jcaruso 2013-05-08 14:13:35

+0

日誌中的錯誤是什麼?另外,請確保如果日誌中有任何指向的行,請告訴我們是哪行代碼。 – 2013-05-08 14:15:30

+0

在那裏我貼過它 – jcaruso 2013-05-08 14:18:40

1

您正在嘗試設置視圖不見了在它被創建之前,你的onCreateView沒有任何返回但是之後,如果它完成並完成您的inflater.inflate並返回視圖。相反,您應該在onStart方法中執行該檢查。

0

onCreateView()用於創建視圖。當onCreateView已返回時,將創建該視圖。有一種方法onViewCreated用於修改視圖。在文檔中

更多信息: Fragment#onViewCreated

0

你永遠不會設置的TextView的知名度了,因爲你的if說法是錯誤的:你設置一個值,而不是檢查它。你應該這樣做:

if (symptothermal == false) 

if (!symptothermal) 
+0

謝謝這有助於一些!我用它... – jcaruso 2013-05-07 21:15:41

0

觀察你onCreate()有一些事情沒有任何意義,我。使用Boolean symptothermal = preferences.getBoolean("symptothermal", true);將始終返回true。您將true設置爲默認值,並且我看不到代碼中的任何部分,其中變量symptothermal設置爲false,這意味着您的if語句將永遠不會執行。你有沒有試圖製造if(symptothermal)而不是? (只要確保它可以使用默認值)。

此外,TextView temp = (TextView) getView().findViewById(R.id.temp);會使您的應用崩潰ClassCastException!爲什麼?因爲您使用臨時編號的視圖不是TextView而是EditText

讓我知道你的進步。

+0

好吧,幫我理解它...布爾,preferences.getBoolean返回首選項,如果沒有定義,默認它爲true。正確? – jcaruso 2013-05-07 21:15:12

+0

@jcaruso,的確如此。在'getBoolean()'上傳遞的布爾值是默認值。在你的情況'真'將被退回,這就是爲什麼你的如果不工作。 – yugidroid 2013-05-07 21:21:33

+0

是的,這是'getBoolean'行爲。 – user2340612 2013-05-07 21:21:44