Android设备上查看飘 [英] Android View Gone

查看:118
本文介绍了Android设备上查看飘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新来的Andr​​ ?? iod,并试图基于共享preferences设置view.GONE。但是它不走了!我是怎么破?!

编辑
因此,测试下属发出后是设置没有被正确地完成......所以,现在是固定的让再试一次。 He'res我的onCreate ...当我删除TextView的临时code正常工作,并显示通过土司preF的变化,但是当code它有应用程序崩溃。

  @覆盖
        公共无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            共享preferences米preferences = preferenceManager.getDefaultShared preferences(getActivity());
            //充气的布局该片段            布尔symptothermal = M preferences.getBoolean(symptothermal,真正的);            如果(!symptothermal){
                上下文的背景下= getActivity()getApplicationContext()。
                Toast.makeText(背景下,你好吐司1!,Toast.LENGTH_LONG).show();
            }其他{
                上下文的背景下= getActivity()getApplicationContext()。
                Toast.makeText(背景下,你好吐司2!,Toast.LENGTH_LONG).show();
            }            如果(!symptothermal){
                TextView的TEMP =(TextView中)getView()findViewById(R.id.temp)。
                temp.setVisibility(View.GONE);
            }
        }

He're的XML FYI:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>    < RelativeLayout的
        机器人:ID =@ + ID / relativeLayout1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =0.23>        <! - 日期文本 - >
        <的EditText
            机器人:ID =@ + ID / dateselected
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentLeft =真
            机器人:layout_marginTop =10dp
            机器人:layout_toLeftOf =@ + ID / pickdate
            机器人:比重=中心
            安卓的inputType =日期/>        <! - DatePicker的按钮 - >
        <按钮
            机器人:ID =@ + ID / pickdate
            机器人:layout_width =80dp
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignBaseline =@ + ID / dateselected
            机器人:layout_alignBottom =@ + ID / dateselected
            机器人:layout_alignParentRight =真
            机器人:SRC =@绘制/ ic_datepicker
            机器人:文字=@字符串/日期/>        <! - 温度标签 - >
        <的EditText
            机器人:ID =@ + ID / TEMP
            机器人:layout_width =192dp
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentLeft =真
            机器人:layout_below =@ + ID / pickdate
            机器人:EMS =10
            安卓的inputType =TEXT
            机器人:文字=@字符串/ TEMP
            机器人:可点击=假
            机器人:cursorVisible =假
            机器人:可聚焦=假
            机器人:focusableInTouchMode =假
            机器人:layout_marginTop =10dp>
        < /&的EditText GT;         <! - 温度场 - >
        <的EditText
            机器人:ID =@ + ID / tempvalue
            机器人:layout_width =80dp
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_below =@ + ID / pickdate
            机器人:layout_toRightOf =@ + ID / dateselected
            机器人:EMS =10
            安卓的inputType =数字
            机器人:layout_marginTop =10dp/>    < / RelativeLayout的>    <! - 注释字段 - >    <的EditText
        机器人:ID =@ + ID / chartingnote
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =0dip
        机器人:layout_weight =0.58
        机器人:EMS =20
        机器人:重力=顶| CENTER_HORIZONTAL
        机器人:提示=@字符串/ hintNote
        安卓的inputType =textMultiLine/>    <! - 更新按钮 - >    <按钮
        机器人:ID =@ + ID / chartingupdate
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/ charting_update/>< / LinearLayout中>

编辑@ daniel_c05:

 包com.projectcaruso.naturalfamilyplaning;        进口com.projectcaruso.naturalfamilyplanning.R;        进口android.app.DatePickerDialog.OnDateSetListener;
        进口android.content.Context;
        进口android.content.Shared preferences;
        进口android.os.Bundle;
        。进口的Andr​​oid preference preferenceManager。
        进口android.support.v4.app.Fragment;
        进口android.view.LayoutInflater;
        进口android.view.View;
        进口android.view.ViewGroup;
        进口android.widget.DatePicker;
        进口android.widget.TextView;
        进口android.widget.Toast;        公共类ChartingFragment扩展片段实现OnDateSetListener {
            共享preferences米preferences = preferenceManager.getDefaultShared preferences(getActivity());
            布尔symptothermal = M preferences.getBoolean(symptothermal,真正的);            @覆盖
            公共无效onDateSet(查看的DatePicker,年整型,诠释monthOfYear,诠释请将dayOfMonth){
                // TODO自动生成方法存根
            }            公共无效showDatePickerDialog(视图v){
                DatePickerFragment newFragment =新DatePickerFragment();
                newFragment.show(getChildFragmentManager(),日期选择器);
            }            @覆盖
            公共无效的onCreate(捆绑savedInstanceState){
                super.onCreate(savedInstanceState);
                如果(!symptothermal){
                    上下文的背景下= getActivity()getApplicationContext()。
                    Toast.makeText(背景下,你好吐司1!,Toast.LENGTH_LONG).show();
                }其他{
                    上下文的背景下= getActivity()getApplicationContext()。
                    Toast.makeText(背景下,你好吐司2!,Toast.LENGTH_LONG).show();
                }
            }            @覆盖
            公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
                //充气视图
                查看查看= inflater.inflate(R.layout.fragment_charting,NULL);
                //现在你可以初始化的TextView
                //我们怎么不叫通知getView()? :P
                TextView的TEMP =(TextView中)view.findViewById(R.id.temp);
                如果(!symptothermal){
                //隐藏如果必要的观点
                        temp.setVisibility(View.GONE);
                    }
                返回视图。
               }
        }

日志:

  05-08 14:10:55.042:E / AndroidRuntime(22900):致命异常:主要
    05-08 14:10:55.042:E / AndroidRuntime(22900):显示java.lang.NullPointerException
    05-08 14:10:55.042:E / AndroidRuntime(22900):在Android的preference preferenceManager.getDefaultShared preferencesName(preferenceManager.java:371)。
    05-08 14:10:55.042:E / AndroidRuntime(22900):在Android的preference preferenceManager.getDefaultShared preferences(preferenceManager.java:366)。
    。05-08 14:10:55.042:E / AndroidRuntime(22900):在com.projectcaruso.naturalfamilyplaning.ChartingFragment<&初始化GT;(ChartingFragment.java:21)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在com.projectcaruso.naturalfamilyplaning.MenuFragment.onListItemClick(MenuFragment.java:36)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在android.support.v4.app.ListFragment $ 2.onItemClick(ListFragment.java:58)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在android.widget.AdapterView.performItemClick(AdapterView.java:298)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在android.widget.AbsListView.performItemClick(AbsListView.java:1100)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2749)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在android.widget.AbsListView $ 1.run(AbsListView.java:3423)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在android.os.Handler.handleCallback(Handler.java:725)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在android.os.Handler.dispatchMessage(Handler.java:92)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在android.os.Looper.loop(Looper.java:137)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在android.app.ActivityThread.main(ActivityThread.java:5041)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在java.lang.reflect.Method.invokeNative(本机方法)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在java.lang.reflect.Method.invoke(Method.java:511)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    05-08 14:10:55.042:E / AndroidRuntime(22900):在dalvik.system.NativeStart.main(本机方法)


解决方案

根据我的理解,你正在使用这个code片段。

因此​​,要知道,一个片段的生命周期,从一个活动的不同。在的onCreate 不能操作上的一个分段UI元素。

您使用这样的:

 如果(!symptothermal){
            TextView的TEMP =(TextView中)getView()findViewById(R.id.temp)。
            temp.setVisibility(View.GONE);
        }

让我们改变这一点,

第一步:更改布尔symptothermal 是一个实例变量,而不是内部变量的onCreate 。因此,使得它在不同的生命周期提供。

第二步:保持同样的code为的onCreate ,但事实上,你不叫code改变TextView的知名度,我们将继续前进了别的地方。

第三步:覆盖onCreateView代替。

  @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    //充气视图
    查看查看= inflater.inflate(R.layout.your_layout,NULL);
    //现在你可以初始化的TextView
    //我们怎么不叫通知getView()? :P
    TextView的TEMP =(TextView中)view.findViewById(R.id.temp);
    如果(!symptothermal){
    //隐藏如果必要的观点
            temp.setVisibility(View.GONE);
        }
    返回视图。
   }

这应该工作。

修改

所以,我注意到你这样做:

 共享preferences米preferences = preferenceManager.getDefaultShared preferences(getActivity());
布尔symptothermal = M preferences.getBoolean(symptothermal,真正的);

这就是问题所在,您呼叫getDefaultShared preferences以外的任何生命周期的,这就是为什么它失败。

显示方式:

 公共类ChartingFragment扩展片段实现OnDateSetListener {
//不要初始化实例变量尚未
   共享preferences米preferences;
   布尔symptothermal;@覆盖
   公共无效的onCreate(捆绑savedInstanceState){
   super.onCreate(savedInstanceState);
     //这是一个很好的时机,初始化
    米preferences = preferenceManager.getDefaultShared preferences(getActivity());
    symptothermal = M preferences.getBoolean(symptothermal,真正的);        如果(!symptothermal){
            上下文的背景下= getActivity()getApplicationContext()。
            Toast.makeText(背景下,你好吐司1!,Toast.LENGTH_LONG).show();
        }其他{
            上下文的背景下= getActivity()getApplicationContext()。
            Toast.makeText(背景下,你好吐司2!,Toast.LENGTH_LONG).show();
        }
    }
}

New to andriod and trying to set view.GONE based on shared preferences. However it's not gone! What did i break?!

EDIT So, after testing the underling issue was that the settings weren't being done correctly... So now that that is fixed lets try again. He'res my onCreate... When i remove the textview temp code it works fine and shows the change in pref via the toast, however when the code it there the app crashes.

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

EDIT @ 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;
               }


        }

Logs:

    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)

解决方案

Based on what I understand, you are using a Fragment for this code.

Therefore, be aware that the life-cycle of a Fragment differs from that of an activity. During onCreate you cannot manipulate UI elements on a Fragment.

You use this:

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

Let's change it a bit,

Step One: Change Boolean symptothermal to be a instance variable, and not an inner variable of onCreate. Therefore, making it available during the different lifecycles.

Step Two: Keep the same code for onCreate, except the fact that you don't call the code that changes the TextView visibility, we'll move that somewhere else.

Step Three: Override onCreateView instead.

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

That should work well.

EDIT

So I noticed you did this:

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

And that is the problem, you are calling getDefaultSharedPreferences outside any of the lifecycles and that's why it fails.

See this:

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();
        }
    }
}

这篇关于Android设备上查看飘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆