【问题标题】:Android Fragment tutorial force closesAndroid Fragment教程强制关闭
【发布时间】:2012-07-18 09:32:05
【问题描述】:

我找到了this 片段教程,因为我很难弄清楚它们。应用力关闭并根据logcat“Error inflating class fragment”。

MainActivity.java

package com.example.learn.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    /* Add a class to handle fragment */
    public static class SSFFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View v = inflater.inflate(R.layout.hello_frag, container, false);
            return v;
        }
    }

}

activity_main.xml

<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" >

   <fragment class="SSFFragment"
   android:id="@+id/frag"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />
</RelativeLayout>

hello_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello_world"
/>

感谢任何帮助。我正在寻找一个简单的片段教程。

【问题讨论】:

    标签: android android-fragments android-fragmentactivity


    【解决方案1】:

    activity_main.xml 中,您错误地命名片段。

    正确的名字应该是com.example.learn.fragments.MainActivity$SSFFragment

    【讨论】:

    • @EGHDK 因为您必须使用完全限定的类名并且您的片段是内部类,并且在 xml 文件中给出内部类的 fq 名称时使用了 $ 符号。
    • 嗯...好吧,这会变得很棘手。不过谢谢。我很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 2012-01-22
    相关资源
    最近更新 更多