在 TabActivity 中使用一个 Fragment 一个 Tab
Use one Fragment for one Tab in TabActivity
目前我正在尝试编写 android 应用程序。我希望有一个 Tab Activity,我可以在其中将一个 Fragment 用于一个 Tab。我尝试遵循 this 教程,但 Android Studio 说:
Incopatible Types:
Required: android.support.v4.app.Fragment
Found: de......fragment_nameOfTheFragment
我在 Activity + 导入语句中的函数代码:
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import de.noname.MYNAME.APPNAME.fragment_geocache_details;
import de.noname.MYNAME.APPNAME.fragment_geocache_details_waypoints;
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new fragment_geocache_details_waypoints (); // Here is one fail
case 1:
return new fragment_geocache_details(); // Here is the other fail
}
}
如果需要更多代码,那么我将编辑我的 Post。但是我从教程中复制了代码,只是将名称更改为我的名称。
您没有使用正确的导入方式进入de......fragment_nameOfTheFragment
我想你有
import android.app.Fragment
你需要
import android.support.v4.app.Fragment;
编辑
@Override
public Fragment getItem(int position) {
Fragment f = null;
switch (position) {
case 0:
f = new fragment_geocache_details_waypoints (); // Here is one fail
break;
case 1:
f = new fragment_geocache_details(); // Here is the other fail
break;
}
return f;
}
或
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new fragment_geocache_details_waypoints (); // Here is one fail
case 1:
return new fragment_geocache_details(); // Here is the other fail
default:
//Do something by default like throw an exception
}
}
目前我正在尝试编写 android 应用程序。我希望有一个 Tab Activity,我可以在其中将一个 Fragment 用于一个 Tab。我尝试遵循 this 教程,但 Android Studio 说:
Incopatible Types:
Required: android.support.v4.app.Fragment
Found: de......fragment_nameOfTheFragment
我在 Activity + 导入语句中的函数代码:
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import de.noname.MYNAME.APPNAME.fragment_geocache_details;
import de.noname.MYNAME.APPNAME.fragment_geocache_details_waypoints;
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new fragment_geocache_details_waypoints (); // Here is one fail
case 1:
return new fragment_geocache_details(); // Here is the other fail
}
}
如果需要更多代码,那么我将编辑我的 Post。但是我从教程中复制了代码,只是将名称更改为我的名称。
您没有使用正确的导入方式进入de......fragment_nameOfTheFragment
我想你有
import android.app.Fragment
你需要
import android.support.v4.app.Fragment;
编辑
@Override
public Fragment getItem(int position) {
Fragment f = null;
switch (position) {
case 0:
f = new fragment_geocache_details_waypoints (); // Here is one fail
break;
case 1:
f = new fragment_geocache_details(); // Here is the other fail
break;
}
return f;
}
或
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new fragment_geocache_details_waypoints (); // Here is one fail
case 1:
return new fragment_geocache_details(); // Here is the other fail
default:
//Do something by default like throw an exception
}
}