从PreferenceActivity访问主UI布局组件

时间:2013-11-18 20:04:45

标签: android preferences

在我的PreferenceActivity中,我有:

public class SettingsActivity extends PreferenceActivity implements
    SharedPreferences.OnSharedPreferenceChangeListener {
SharedPreferences sp;
private final String TAG = getClass().getSimpleName();
private SimpleFacebook mSimpleFacebook;
ProgressDialog  progressBar;
Context ctx;
RelativeLayout connection;
SlidingUpPanelLayout slider;


protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    ctx = this;
    addPreferencesFromResource(R.xml.preferences);
    Preference button = (Preference)findPreference("logoutButton");

    Context context = getBaseContext();
    LayoutInflater lif = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    connection = (RelativeLayout) lif.inflate(R.id.conexion, null);

    }
}

我需要夸大布局的参考,以便我可以消失一个元素。 当我选择整个布局(R.layout.activity_main)时,我收到错误:

android.view.InflateException: Binary XML file line #12: Error inflating class fragment

(在activity_main中,我有一个显示地图的片段)

当我尝试仅对布局(连接)的一个组件进行充气时,我收到错误:

 android.content.res.Resources$NotFoundException: Resource ID #0x7f060007 type #0x12 is not valid

我该怎么办?

编辑: 案例1:

RelativeLayout layout = (RelativeLayout)lif.inflate(R.layout.activity_main, null);

日志 - >

11-18 14:26:06.417: E/AndroidRuntime(28342): FATAL EXCEPTION: main
11-18 14:26:06.417: E/AndroidRuntime(28342): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xoco.checker/com.xoco.checker.pref.SettingsActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.os.Looper.loop(Looper.java:137)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.app.ActivityThread.main(ActivityThread.java:4944)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at java.lang.reflect.Method.invokeNative(Native Method)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at java.lang.reflect.Method.invoke(Method.java:511)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at dalvik.system.NativeStart.main(Native Method)
11-18 14:26:06.417: E/AndroidRuntime(28342): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at com.xoco.checker.pref.SettingsActivity.onCreate(SettingsActivity.java:55)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.app.Activity.performCreate(Activity.java:5188)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
11-18 14:26:06.417: E/AndroidRuntime(28342):    ... 11 more
11-18 14:26:06.417: E/AndroidRuntime(28342): Caused by: java.lang.ClassNotFoundException: android.view.fragment
11-18 14:26:06.417: E/AndroidRuntime(28342):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.view.LayoutInflater.createView(LayoutInflater.java:558)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:649)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
11-18 14:26:06.417: E/AndroidRuntime(28342):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
11-18 14:26:06.417: E/AndroidRuntime(28342):    ... 20 more

案例2:
    Context context = getBaseContext();     LayoutInflater lif =(LayoutInflater)getApplicationContext()。getSystemService(Context.LAYOUT_INFLATER_SERVICE);

connection = (RelativeLayout) lif.inflate(R.id.conexion, null);

记录:

11-18 14:31:13.587: E/AndroidRuntime(29144): FATAL EXCEPTION: main
11-18 14:31:13.587: E/AndroidRuntime(29144): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xoco.checker/com.xoco.checker.pref.SettingsActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f060007 type #0x12 is not valid
11-18 14:31:13.587: E/AndroidRuntime(29144):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at android.os.Looper.loop(Looper.java:137)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at android.app.ActivityThread.main(ActivityThread.java:4944)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at java.lang.reflect.Method.invokeNative(Native Method)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at java.lang.reflect.Method.invoke(Method.java:511)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at dalvik.system.NativeStart.main(Native Method)
11-18 14:31:13.587: E/AndroidRuntime(29144): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f060007 type #0x12 is not valid
11-18 14:31:13.587: E/AndroidRuntime(29144):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2450)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at android.content.res.Resources.getLayout(Resources.java:1178)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at com.xoco.checker.pref.SettingsActivity.onCreate(SettingsActivity.java:57)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at android.app.Activity.performCreate(Activity.java:5188)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
11-18 14:31:13.587: E/AndroidRuntime(29144):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
11-18 14:31:13.587: E/AndroidRuntime(29144):    ... 11 more

我清空([...])activity_main的几个布局,用于清洁源。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapActivity" >

<com.sothree.slidinguppanel.SlidingUpPanelLayout
    android:id="@+id/sliding_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

    <RelativeLayout
        android:id="@+id/reviewsPanel"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone" >

        <!-- l1 is the header, it doesn't move-->

        <RelativeLayout
            android:id="@+id/l1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            [...]    
        </RelativeLayout>
        <!-- end l1 -->

        <RelativeLayout
            android:id="@+id/headerComment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/l1" >

        [...] 

        </RelativeLayout>

        <ListView
            android:id="@+id/reviews"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/headerComment"
            android:padding="@dimen/activity_horizontal_margin" />
    </RelativeLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
<!-- Appear when no internet connection -->

<Button
    android:id="@+id/bNoConnection"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_gravity="center_vertical|center_horizontal"
    android:text="No hay conexion internet. ¿Reintentar?"
    android:visibility="gone" />
<!-- User connection layout -->

<RelativeLayout
    android:id="@+id/conexion"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="@dimen/activity_vertical_margin"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:visibility="gone" >

    <!-- Login to facebook -->

    <Button
        android:id="@+id/button_facebook_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/active_404" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

  

我需要夸大布局的引用,这样我才能消失元素。

您不能以这种方式隐藏主要活动中的元素。您的设置活动应该更改设置,就是这样。这取决于处理自己观点的主要活动。在onResume()中,您可以检查设置并在适当的时候隐藏它。

答案 1 :(得分:1)

首先,您的布局和错误java.lang.ClassNotFoundException: android.view.fragment表示您的活动必须从FragmentActivity延伸,否则您无法通过<fragment>来填充布局