Android View Gone

时间:2013-05-07 19:56:13

标签: android

andriod并尝试根据共享首选项设置view.GONE。然而它并没有消失!我打破了什么?!

EDIT 因此,在测试底层问题后,设置没有正确完成...所以现在已经修复了让我们再试一次。他是我的onCreate ...当我删除textview临时代码时它工作正常并通过toast显示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);
            }
        }

他是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)

5 个答案:

答案 0 :(得分:1)

你试图在视图创建之前将视图设置为已经过去,你的onCreateView还没有返回任何内容,但之后如果它继续并完成你的inflater.inflate并返回视图。您应该在onStart方法中执行该检查。

答案 1 :(得分: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();
        }
    }
}

答案 2 :(得分:0)

onCreateView()用于创建视图。返回onCreateView后,将创建视图。有一个方法onViewCreated用于修改视图。

文档中的更多信息: Fragment#onViewCreated

答案 3 :(得分:0)

您永远不会将TextView的可见性设置为GONE,因为您的if声明错误:您设置值而不是检查。你应该这样做:

if (symptothermal == false)

if (!symptothermal)

答案 4 :(得分:0)

观察你的onCreate()有一些对我没有任何意义的事情。

使用Boolean symptothermal = preferences.getBoolean("symptothermal", true);将始终返回true。您将true设置为默认值,我没有看到代码中变量symptothermal设置为false的任何部分,这意味着您的if语句永远不会被执行。您是否尝试过if(symptothermal)代替? (只需确保它可以使用默认值)。

此外,TextView temp = (TextView) getView().findViewById(R.id.temp);会使您的应用程序崩溃ClassCastException!为什么?因为您的带有临时ID的视图不是TextView,而是EditText

让我知道你的进展。