【问题标题】:FragmentActivity incompatible with FragmentStatePagerAdapterFragmentActivity 与 FragmentStatePagerAdapter 不兼容
【发布时间】:2014-09-16 05:16:57
【问题描述】:

我正在制作一个使用滑动视图显示数据的应用。我在所有类中都使用了 fragmentactivity,但它会导致 tabpagedapater 出现错误,即它与 fragmentstatepageadapter 不兼容我该如何解决这个问题?

Tabpageadapter.java

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class TabPagerAdapter extends FragmentStatePagerAdapter {
    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public FragmentActivity getItem(int i) {
        switch (i) {
        case 0:
            //Fragement for Android Tab
            return new Android();
        case 1:
           //Fragment for Ios Tab
            return new Ios();
        case 2:
            //Fragment for Windows Tab
            return new Windows();
        }
        return null;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }




    }

主要活动

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.learn2crack.library.DatabaseHandler;

import java.util.HashMap;

public class Android extends FragmentActivity {

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


            View android = inflater.inflate(R.layout.android_frag, container, false);
            DatabaseHandler db = new DatabaseHandler(getApplicationContext());

            HashMap<String,String> user = new HashMap<String, String>();
            user = db.getUserDetails();

            /**
             * Displays the registration details in Text view
             **/

            final TextView fname = (TextView)findViewById(R.id.fname);
            final TextView lname = (TextView)findViewById(R.id.lname);
            final TextView uname = (TextView)findViewById(R.id.uname);
            final TextView email = (TextView)findViewById(R.id.email);
            final TextView created_at = (TextView)findViewById(R.id.regat);
            fname.setText(user.get("fname"));
            lname.setText(user.get("lname"));
            uname.setText(user.get("uname"));
            email.setText(user.get("email"));
            created_at.setText(user.get("created_at"));

            return android;
}}

【问题讨论】:

  • 在你的所有片段中,将“extends FragmentActivity”更改为“extends Fragment”
  • 如何通过片段扩展类来使用 findviewbyid 和 getapplicationcontext?
  • 为此你可以这样做“getActivity().getApplicationContext()”并使用“findViewById”你必须这样做:yourView.findViewById() 这里 yourView 是你膨胀的视图你的片段。

标签: android android-activity fragment android-fragmentactivity


【解决方案1】:

改变这个。

 @Override
public int getCount() {
    // TODO Auto-generated method stub
    return 3;
}

同样在您的 Android 类中扩展为 Fragment 而不是 FragmentActivity

注意:检查您是否使用 FragmentActivity 扩展了相同的 IosWindows 类而不是 Fragment。如果是,那么就像我说的那样改变。

更新:

您正在使用Fragment,因此作为Context,您需要使用getActivity() 而不是getApplicationContext()

所以在这里改变

  DatabaseHandler db = new DatabaseHandler(getActivity());

这里也有

  final TextView fname = (TextView)android.findViewById(R.id.fname);
  final TextView lname = (TextView)android.findViewById(R.id.lname);
  final TextView uname = (TextView)android.findViewById(R.id.uname);
  final TextView email = (TextView)android.findViewById(R.id.email);
  final TextView created_at = (TextView)android.findViewById(R.id.regat);

因为当您使用 Fragments 时,您需要传递 View 的对象以查找 UI 元素的 ID 作为参考。

【讨论】:

  • 用片段扩展它会导致 findviewbyid 和 getapplicationcontext() 生成错误我如何同时使用片段和活动?
猜你喜欢
  • 1970-01-01
  • 2018-11-13
  • 2018-07-04
  • 1970-01-01
  • 2020-10-27
  • 2013-03-14
  • 2016-07-09
  • 2020-10-31
  • 2019-04-30
相关资源
最近更新 更多