目前我正在尝试编程Android应用程序。我希望有一个Tab Activity,我可以在一个Tab上使用一个Fragment。我曾尝试按照this教程,但Android Studio说:
Incopatible Types:
Required: android.support.v4.app.Fragment
Found: de......fragment_nameOfTheFragment
我在Activity + import语句中的函数代码:
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
}
}
如果需要更多代码,那么我将编辑我的帖子。但我复制了教程中的代码,只是将名称更改为我的名称。
答案 0 :(得分:1)
您没有在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
}
}