Android 如何在2个片段中实现AppCompative活动?

Android 如何在2个片段中实现AppCompative活动?,android,android-fragments,android-viewpager,fragment,Android,Android Fragments,Android Viewpager,Fragment,我正在尝试添加2个片段活动,但我必须使用AppCompatActivity。因此,当我添加AppCompatActivity时,程序停止。我怎样才能解决这个问题 我的CombineFragmentActivity.java import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.PagerAdapt

我正在尝试添加2个片段活动,但我必须使用AppCompatActivity。因此,当我添加AppCompatActivity时,程序停止。我怎样才能解决这个问题

我的CombineFragmentActivity.java

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;
import java.util.Vector;

/**
 * Created by KHas on 29.04.2016.
 */
public class CombineFragmentActivity extends AppCompatActivity {
    private PagerAdapter mPagerAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.combine_fragment_activity);

        initialisePaging();


    }
    private void initialisePaging() {

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this,StartingTestActivity.class.getName()));
        fragments.add(Fragment.instantiate(this,ProfileActivity.class.getName()));
        mPagerAdapter = new PagerAdapter1(this.getSupportFragmentManager(),fragments);

        ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
        pager.setAdapter(mPagerAdapter);
    }
}
导入android.graphics.Color;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.support.v4.view.PagerAdapter;
导入android.support.v4.view.ViewPager;
导入android.support.v7.app.ActionBar;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.util.TypedValue;
导入android.view.Gravity;
导入android.widget.RelativeLayout;
导入android.widget.TextView;
导入java.util.List;
导入java.util.Vector;
/**
*由KAS于2016年4月29日创建。
*/
公共类CombineFragmentActivity扩展了AppCompatActivity{
私人寻呼器;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.combine\u fragment\u活动);
初始化分页();
}
私有void initialisePaging(){
列表片段=新向量();
fragments.add(Fragment.instantiate(this,StartingTestActivity.class.getName());
add(Fragment.instantiate(this,ProfileActivity.class.getName());
mPagerAdapter=new PagerAdapter1(this.getSupportFragmentManager(),fragments);
ViewPager pager=(ViewPager)findViewById(R.id.ViewPager);
寻呼机设置适配器(mPagerAdapter);
}
}
My StartingTestActivity.java//第一个片段

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.List;
import java.util.Vector;


public class StartingTestActivity extends Fragment implements AdapterView.OnItemSelectedListener {
    private Spinner spinner;
    ArrayAdapter<CharSequence> adapter;



    public StartingTestActivity() {
        // Required empty public constructor
    }


    public static StartingTestActivity newInstance() {
        StartingTestActivity fragment = new StartingTestActivity();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.activity_startingtest, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        spinner = (Spinner)view.findViewById(R.id.spinner);
        adapter = ArrayAdapter.createFromResource(getActivity(),R.array.simple_spinner_dropdown_item,R.layout.spinner_item);
        adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);

        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        Typeface ubuntu_MI = Typeface.createFromAsset(getActivity().getAssets(),"Ubuntu-MI.ttf");
        Typeface ubuntu_L = Typeface.createFromAsset(getActivity().getAssets(),"Ubuntu-L.ttf");
        Typeface ubuntu_R = Typeface.createFromAsset(getActivity().getAssets(),"Ubuntu-R.ttf");

        TextView info = (TextView) view.findViewById(R.id.textView6);
        TextView question = (TextView) view.findViewById(R.id.textView7);

        info.setTypeface(ubuntu_R);
        question.setTypeface(ubuntu_R);


        // Get the ActionBar
        android.support.v7.app.ActionBar ab = ((AppCompatActivity)getActivity()).getSupportActionBar();


        // Create a TextView programmatically.
        TextView tv = new TextView(getActivity());

        // Create a LayoutParams for TextView
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, // Width of TextView
                RelativeLayout.LayoutParams.WRAP_CONTENT); // Height of TextView

        // Apply the layout parameters to TextView widget
        tv.setLayoutParams(lp);

        // Set text to display in TextView
        // This will set the ActionBar title text
        tv.setText("Anasayfa");

        // Set the text color of TextView
        // This will change the ActionBar title text color
        tv.setTextColor(Color.parseColor("#FFF5EE"));

        // Center align the ActionBar title
        tv.setGravity(Gravity.CENTER_HORIZONTAL);

        // Set the serif font for TextView text
        // This will change ActionBar title text font
        tv.setTypeface(ubuntu_MI);

        // Underline the ActionBar title text
        // tv.setPaintFlags(tv.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);

        // Set the ActionBar title font size
        tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP,24);

        // Display a shadow around ActionBar title text
        //tv.setShadowLayer(
        //      1.f, // radius
        //    2.0f, // dx
        //  2.0f, // dy
        //  Color.parseColor("#FF8C00") // shadow color
        //);

        // Set the ActionBar display option
        ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

        // Finally, set the newly created TextView as ActionBar custom view
        ab.setCustomView(tv);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                // Whatever you want to happen when the first item gets selected
                break;
            case 1:
                // Whatever you want to happen when the second item gets selected
                break;
            case 2:
                // Whatever you want to happen when the thrid item gets selected
                break;

        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class ProfileActivity extends Fragment implements AdapterView.OnItemSelectedListener {
    public ProfileActivity() {
        // Required empty public constructor
    }


    public static ProfileActivity newInstance() {
        ProfileActivity fragment = new ProfileActivity();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

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

        View rootView = inflater.inflate(R.layout.activity_profile, container, false);
        return rootView;
    }

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        Typeface ubuntu_MI = Typeface.createFromAsset(getActivity().getAssets(),"Ubuntu-MI.ttf");
        Typeface ubuntu_L = Typeface.createFromAsset(getActivity().getAssets(),"Ubuntu-L.ttf");
        Typeface ubuntu_R = Typeface.createFromAsset(getActivity().getAssets(),"Ubuntu-R.ttf");

        TextView info = (TextView) view.findViewById(R.id.textView6);
        TextView question = (TextView) view.findViewById(R.id.textView7);

        info.setTypeface(ubuntu_R);
        question.setTypeface(ubuntu_R);


        // Get the ActionBar
        android.support.v7.app.ActionBar ab = ((AppCompatActivity)getActivity()).getSupportActionBar();


        // Create a TextView programmatically.
        TextView tv = new TextView(getActivity());

        // Create a LayoutParams for TextView
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, // Width of TextView
                RelativeLayout.LayoutParams.WRAP_CONTENT); // Height of TextView

        // Apply the layout parameters to TextView widget
        tv.setLayoutParams(lp);

        // Set text to display in TextView
        // This will set the ActionBar title text
        tv.setText("Anasayfa");

        // Set the text color of TextView
        // This will change the ActionBar title text color
        tv.setTextColor(Color.parseColor("#FFF5EE"));

        // Center align the ActionBar title
        tv.setGravity(Gravity.CENTER_HORIZONTAL);

        // Set the serif font for TextView text
        // This will change ActionBar title text font
        tv.setTypeface(ubuntu_MI);

        // Underline the ActionBar title text
        // tv.setPaintFlags(tv.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);

        // Set the ActionBar title font size
        tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP,24);

        // Display a shadow around ActionBar title text
        //tv.setShadowLayer(
        //      1.f, // radius
        //    2.0f, // dx
        //  2.0f, // dy
        //  Color.parseColor("#FF8C00") // shadow color
        //);

        // Set the ActionBar display option
        ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

        // Finally, set the newly created TextView as ActionBar custom view
        ab.setCustomView(tv);
    }
    public boolean onOptionsItemSelected(MenuItem item) {
    //Action Bar' daki settings kısmına basıldığında ne yapacağını söylüyoruz.
        if(item.getItemId() == R.id.settings){
            Toast.makeText(getActivity(), "Ayarlara basıldı", Toast.LENGTH_SHORT).show();
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_profile_activity, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    private MenuInflater getMenuInflater() {
        return null;
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

}
导入android.graphics.Color;
导入android.graphics.Typeface;
导入android.os.Bundle;
导入android.support.annotation.Nullable;
导入android.support.v4.app.Fragment;
导入android.support.v4.view.ViewPager;
导入android.support.v7.app.ActionBar;
导入android.support.v7.app.AppActivity;
导入android.util.TypedValue;
导入android.view.Gravity;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.RelativeLayout;
导入android.widget.Spinner;
导入android.widget.TextView;
导入java.util.List;
导入java.util.Vector;
公共类StartingTestActivity扩展片段实现AdapterView.OnItemSelectedListener{
私人纺纱机;
阵列适配器;
公共启动测试活动(){
//必需的空公共构造函数
}
公共静态启动测试活动newInstance(){
StartingTestActivity片段=新的StartingTestActivity();
返回片段;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
返回充气机。充气(R.layout.activity_startingtest,容器,错误);
}
@凌驾
已创建视图上的公共void(视图,@Nullable Bundle savedInstanceState){
super.onViewCreated(视图,savedInstanceState);
微调器=(微调器)视图.findViewById(R.id.spinner);
adapter=ArrayAdapter.createFromResource(getActivity(),R.array.simple\u微调器\u下拉菜单\u项,R.layout.spinner\u项);
adapter.setDropDownViewResource(R.layout.spinner\u下拉项);
旋转器。设置适配器(适配器);
spinner.setOnItemSelectedListener(此);
Typeface ubuntu_MI=Typeface.createFromAsset(getActivity().getAssets(),“ubuntu MI.ttf”);
Typeface ubuntu_L=Typeface.createFromAsset(getActivity().getAssets(),“ubuntu-L.ttf”);
Typeface ubuntu_R=Typeface.createFromAsset(getActivity().getAssets(),“ubuntu-R.ttf”);
TextView信息=(TextView)view.findViewById(R.id.textView6);
TextView问题=(TextView)view.findViewById(R.id.textView7);
设置字体(ubuntu_R);
设置字体(ubuntu_R);
//获取操作栏
android.support.v7.app.ActionBar ab=((AppCompatActivity)getActivity()).getSupportActionBar();
//以编程方式创建TextView。
TextView tv=新的TextView(getActivity());
//为TextView创建布局参数
RelativeLayout.LayoutParams lp=新的RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_父项,//TextView的宽度
RelativeLayout.LayoutParams.WRAP_CONTENT);//文本视图的高度
//将布局参数应用于TextView小部件
tv.setLayoutParams(lp);
//设置要在TextView中显示的文本
//这将设置操作栏标题文本
tv.setText(“Anasayfa”);
//设置TextView的文本颜色
//这将更改操作栏标题文本的颜色
tv.setTextColor(Color.parseColor(“#FFF5EE”);
//将操作栏标题居中对齐
电视。设置重力(重力。水平中心);
//设置TextView文本的衬线字体
//这将更改操作栏标题文本字体
设置字体(ubuntu_-MI);
//在操作栏标题文本下面加下划线
//tv.setPaintFlags(tv.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
//设置操作栏标题字体大小
tv.setTextSize(TypedValue.COMPLEX\u UNIT\u DIP,24);
//在操作栏标题文本周围显示阴影
//电视阴影层(
//1.f,//半径
//2.0f,//dx
//2.0f,//dy
//Color.parseColor(“#FF8C00”)//阴影颜色
//);
//设置ActionBar显示选项
ab.setDisplayOptions(ActionBar.DISPLAY\u SHOW\u CUSTOM);
//最后,将新创建的TextView设置为ActionBar自定义视图
ab.setCustomView(电视);
}
@凌驾
已选择公共无效项(AdapterView父项,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>