类型不匹配无法从 My Activity 转换为 Fragment
Type mismatch cannot convert from My Activity to Fragment
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
case 3: return ThirdFragment.newInstance("ThirdFragment, Instance 2");
case 4: return ThirdFragment.newInstance("ThirdFragment, Instance 3");
default: return ThirdFragment.newInstance("ThirdFragment, Default");
}
}
@Override
public int getCount() {
return 5;
}
}
}
我收到类型不匹配错误。基本上它说它不能将 activity 转换为片段。从我读到的内容来看,它与我的进口商品有关。谁能帮帮我?
请看下面的代码。我向您展示并扩展您如何创建寻呼机视图:
主要活动class:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
}
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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<!-- it is important to use ViewPager from support library -->
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
MyPagerAdapter.class
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return FirstFragment.newInstance("FirstFragment, Instance 1");
case 1:
return SecondFragment.newInstance("SecondFragment, Instance 1");
case 2:
return ThirdFragment.newInstance("ThirdFragment, Instance 1");
case 3:
return ThirdFragment.newInstance("ThirdFragment, Instance 2");
case 4:
return ThirdFragment.newInstance("ThirdFragment, Instance 3");
default:
return ThirdFragment.newInstance("ThirdFragment, Default");
}
}
@Override
public int getCount() {
return 5;
}
}
fragment_layout.xml
我为所有片段创建了通用布局。我将在下面的源代码中更改背景颜色和设置文本。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
FirstFragment.class
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FirstFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.fragment_layout, container, false);
v.setBackgroundColor(Color.RED);
TextView textView = (TextView) v.findViewById(R.id.text_view);
textView.setText(getArguments().getString("value"));
return v;
}
public static FirstFragment newInstance(String s) {
Bundle args = new Bundle();
args.putString("value", s);
FirstFragment result = new FirstFragment();
result.setArguments(args);
return result;
}
}
SecondFragment.class
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class SecondFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.fragment_layout, container, false);
v.setBackgroundColor(Color.GRAY);
TextView textView = (TextView) v.findViewById(R.id.text_view);
textView.setText(getArguments().getString("value"));
return v;
}
public static SecondFragment newInstance(String s) {
Bundle args = new Bundle();
args.putString("value", s);
SecondFragment result = new SecondFragment();
result.setArguments(args);
return result;
}
}
ThirdFragment.class
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ThirdFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
v.setBackgroundColor(Color.BLUE);
TextView textView = (TextView) v.findViewById(R.id.text_view);
textView.setText(getArguments().getString("value"));
return v;
}
public static ThirdFragment newInstance(String s) {
Bundle args = new Bundle();
args.putString("value", s);
ThirdFragment result = new ThirdFragment();
result.setArguments(args);
return result;
}
}
这是完整的程序 显示 5 个片段,其中包含作为参数传递的文本。请试一试。如果您还有一个问题或我的解决方案中的某些内容不清楚或者我的解决方案对您没有帮助。来吧,只是问。 :)
我知道已经晚了,但我提交了这个答案,因为它发生在很多开发人员身上。
由于库导入冲突,出现此错误。检查您在 FirstFragment
、SecondFragment
和 ThirdFragment
中的导入,并确保您使用的是相同的片段库。
如果您使用的是旧 API 的片段,则需要导入 android.support.v4.app.Fragment;
但是,如果您将 Fragments 用于较新的 API,请使用
android.app.Fragment;
当您使用自动完成来完成 class name 时,问题就来了您可能没有注意到您正在导入什么库。
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
case 3: return ThirdFragment.newInstance("ThirdFragment, Instance 2");
case 4: return ThirdFragment.newInstance("ThirdFragment, Instance 3");
default: return ThirdFragment.newInstance("ThirdFragment, Default");
}
}
@Override
public int getCount() {
return 5;
}
}
}
我收到类型不匹配错误。基本上它说它不能将 activity 转换为片段。从我读到的内容来看,它与我的进口商品有关。谁能帮帮我?
请看下面的代码。我向您展示并扩展您如何创建寻呼机视图:
主要活动class:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
}
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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<!-- it is important to use ViewPager from support library -->
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
MyPagerAdapter.class
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return FirstFragment.newInstance("FirstFragment, Instance 1");
case 1:
return SecondFragment.newInstance("SecondFragment, Instance 1");
case 2:
return ThirdFragment.newInstance("ThirdFragment, Instance 1");
case 3:
return ThirdFragment.newInstance("ThirdFragment, Instance 2");
case 4:
return ThirdFragment.newInstance("ThirdFragment, Instance 3");
default:
return ThirdFragment.newInstance("ThirdFragment, Default");
}
}
@Override
public int getCount() {
return 5;
}
}
fragment_layout.xml 我为所有片段创建了通用布局。我将在下面的源代码中更改背景颜色和设置文本。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
FirstFragment.class
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FirstFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.fragment_layout, container, false);
v.setBackgroundColor(Color.RED);
TextView textView = (TextView) v.findViewById(R.id.text_view);
textView.setText(getArguments().getString("value"));
return v;
}
public static FirstFragment newInstance(String s) {
Bundle args = new Bundle();
args.putString("value", s);
FirstFragment result = new FirstFragment();
result.setArguments(args);
return result;
}
}
SecondFragment.class
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class SecondFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.fragment_layout, container, false);
v.setBackgroundColor(Color.GRAY);
TextView textView = (TextView) v.findViewById(R.id.text_view);
textView.setText(getArguments().getString("value"));
return v;
}
public static SecondFragment newInstance(String s) {
Bundle args = new Bundle();
args.putString("value", s);
SecondFragment result = new SecondFragment();
result.setArguments(args);
return result;
}
}
ThirdFragment.class
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ThirdFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
v.setBackgroundColor(Color.BLUE);
TextView textView = (TextView) v.findViewById(R.id.text_view);
textView.setText(getArguments().getString("value"));
return v;
}
public static ThirdFragment newInstance(String s) {
Bundle args = new Bundle();
args.putString("value", s);
ThirdFragment result = new ThirdFragment();
result.setArguments(args);
return result;
}
}
这是完整的程序 显示 5 个片段,其中包含作为参数传递的文本。请试一试。如果您还有一个问题或我的解决方案中的某些内容不清楚或者我的解决方案对您没有帮助。来吧,只是问。 :)
我知道已经晚了,但我提交了这个答案,因为它发生在很多开发人员身上。
由于库导入冲突,出现此错误。检查您在 FirstFragment
、SecondFragment
和 ThirdFragment
中的导入,并确保您使用的是相同的片段库。
如果您使用的是旧 API 的片段,则需要导入 android.support.v4.app.Fragment;
但是,如果您将 Fragments 用于较新的 API,请使用
android.app.Fragment;
当您使用自动完成来完成 class name 时,问题就来了您可能没有注意到您正在导入什么库。