为什么这只是一个扩展FragmentActivity主机类的内部工作? [英] Why does this only work inside a host class that extends FragmentActivity?

查看:167
本文介绍了为什么这只是一个扩展FragmentActivity主机类的内部工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段具有以下布局:my_fragment.xml

 <?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
          机器人:方向=垂直
          机器人:layout_width =FILL_PARENT
          机器人:layout_height =FILL_PARENT><的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/ APPNAME」
        机器人:ID =@ + ID / TextView的机器人:layout_gravity =中心
        机器人:layout_margin =5DP/><按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/按钮1
        机器人:ID =@ + ID /按钮1
        机器人:layout_gravity =中心/>

和相应的MyFragment.java

 包com.example.appdemo;进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Button;
进口android.widget.Toast;公共类MyFragment扩展片段{@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){    查看查看= inflater.inflate(R.layout.top_bar,集装箱,FALSE);    按钮按钮1 =(按钮)view.findViewById(R.id.button1);
    button1.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            Toast.makeText(getActivity()按钮pressed,Toast.LENGTH_SHORT).show();
        }
    });    返回视图。
    }
}

和本托管内MainActivity.java

 包com.example.appdemo;进口android.support.v4.app.FragmentActivity;
进口android.os.Bundle;公共类MainActivity扩展FragmentActivity {@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
}
}

具有以下布局activity_main.xml中

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。><片段
    机器人:ID =@ + ID /片段
    机器人:名字=com.example.appdemo.MyFragment
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT/>< / RelativeLayout的>

现在,我的问题是,当我向FragmentActivity为什么这只是工作的?我想扩展活动。我该如何实现这个目标?


解决方案

  

为什么这只扩展FragmentActivity主机类的内部工作的?


由于您从 android.support.v4.app.Fragment 继承,而不是 android.app.Fragment

I have a fragment with the following layout: my_fragment.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/appName"
        android:id="@+id/textView" android:layout_gravity="center"
        android:layout_margin="5dp"/>

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1"
        android:id="@+id/button1"
        android:layout_gravity="center"/>

and corresponding MyFragment.java

package com.example.appdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.top_bar, container, false);

    Button button1 = (Button)view.findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(getActivity(), "Button Pressed", Toast.LENGTH_SHORT).show();
        }
    });

    return view;
    }
}

and this is hosted inside MainActivity.java

package com.example.appdemo;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

public class MainActivity extends FragmentActivity {

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

with the following layout 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"
tools:context=".MainActivity" >

<fragment
    android:id="@+id/fragment"
    android:name="com.example.appdemo.MyFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

Now, my question is why does this only work when I extend FragmentActivity? I'd like to extend Activity. How do I accomplish that?

解决方案

Why does this only work inside a host class that extends FragmentActivity?

Because you are inheriting from android.support.v4.app.Fragment instead of android.app.Fragment.

这篇关于为什么这只是一个扩展FragmentActivity主机类的内部工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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