gpt4 book ai didi

java - Android fragment 类型不匹配

转载 作者:行者123 更新时间:2023-12-01 23:04:31 25 4
gpt4 key购买 nike

我是 Android 新手,正在学习达特茅斯的教程。 http://www.cs.dartmouth.edu/~campbell/cs65/lecture08/lecture08.html

我正在关注 MainActivity.java 中的所有代码,

// create the fragments
Fragment mFindFragment = new FindFragment();
Fragment mChatFragment = new ChatFragment();

// bind the fragments to the tabs - set up tabListeners for each tab
mFindTab.setTabListener(new MyTabsListener(mFindFragment,
getApplicationContext()));
mChatTab.setTabListener(new MyTabsListener(mChatFragment,
getApplicationContext()));

我遇到了此错误:类型不匹配:无法从 FindFragment 转换为 Fragment。所以我按照修复错误建议并将代码更改为

// create the fragments
FindFragment mFindFragment = new FindFragment();

// bind the fragments to the tabs - set up tabListeners for each tab
mFindTab.setTabListener(new MyTabsListener(mFindFragment,
getApplicationContext()));

现在,出现了一个新错误:构造函数 MyTabsListener(FindFragment, Context) 未定义。

以防万一导入很重要,它们在这里:

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

对于 myTabsListener:

  class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public Context context;

public MyTabsListener(Fragment fragment, Context context) {
this.fragment = fragment;
this.context = context;

}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(context, "Reselected!", Toast.LENGTH_SHORT).show();

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Toast.makeText(context, "Selected!", Toast.LENGTH_SHORT).show();
ft.replace(R.id.container, fragment);
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(context, "Unselected!", Toast.LENGTH_SHORT).show();
ft.remove(fragment);
}

}

对于我的 FindFragment 类:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FindFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.findfragment, container, false);
}


}

我在这里很困惑。我不确定这是否与我的导入、库设置或其他问题有关。提前致谢!

最佳答案

您需要更改此设置

import android.support.v4.app.Fragment;
// you will use this import when you want fragment from support library
// in that case you will extend FragmentActivity which is the base class for support based fragments

import android.app.Fragment;

FindFragment.java中。

ChatFragment.java 中也进行同样的操作

关于java - Android fragment 类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23005586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com