YXTS122
YXTS122
2016-11-28 22:08
采纳率: 100%
浏览 1.3k

关于Fragment使用,怎么不运行?

LeftFragment.java

 package com.example.fragmenttest;

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

public class LeftFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
    {
        View view=inflater.inflate(R.layout.left_fragment,container,false);
        return view;
    }

}

RightFragment.java

 package com.example.fragmenttest;

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

public class RightFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
    {
        View view=inflater.inflate(R.layout.right_fragment,container,false);
        return view;
    }

}

MainActivity.java

 package com.example.fragmenttest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

 <LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.fragmenttest.MainActivity" 
    android:orientation="horizontal">

    <fragment 
        android:id="@+id/left_fragment"
        android:name="com.example.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <fragment 
        android:id="@+id/right_fragment"
        android:name="com.example.fragmenttest.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>

left_fragment.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/ce"
            android:textSize="20sp" />


</LinearLayout>

right_fragment.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#00ff00">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="20sp" />



</LinearLayout>

strings.xml

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

    <string name="app_name">FragmentTest</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="ce">测试</string>

</resources>

图片说明
图片说明
图片说明

如果我加上这一句android:baselineAligned="false"就报错如下:
图片说明

请大神赐教

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • ytmfdw
    ytmfdw 2016-11-29 08:40
    已采纳

    把Activity的继承关系改成FragmentActivity,因为你的Fragment是使用了v4包中的,而Activity默认使用的是sdk中的,两边不一致导致类型转换错误

    点赞 评论
  • zgy621101
    紫风幻雪 2016-11-29 01:35
    点赞 评论
  • YXTS122
    YXTS122 2016-11-29 05:47

    改了一下双引号,并clean了一下,运行如下图片

    点赞 评论
  • dsxjinzhiqiang
    dsxjinzhiqiang 2016-11-29 07:33

    Activity改成v4包里的

    点赞 评论
  • zgy621101
    紫风幻雪 2016-11-29 08:34

    你的MainActivity怎么继承的是Activity(import android.app.Activity),一般都是继承AppCompatActivity(import android.support.v7.app.AppCompatActivity;)

    点赞 评论
  • YXTS122
    YXTS122 2016-11-29 09:24

    可以运行了,谢谢大家的回答。。。。。。。。图片

    点赞 评论

相关推荐