2013-05-07 78 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