我已经更新了我的应用程序以使用最新的支持库(版本23.0.0),我发现他们已经弃用了Fragment类的onAttach()函数.
代替:
onAttach (Activity activity)
Run Code Online (Sandbox Code Playgroud)
下雪了:
onAttach (Context context)
Run Code Online (Sandbox Code Playgroud)
由于我的应用程序使用了弃用之前传递的活动,我认为可能的解决方案是:
@Override
public void onAttach(Context context) {
super.onAttach(context);
activity = getActivity();
}
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?
更新:
如果我运行API低于23的设备,则甚至不会调用新的onAttach().我希望这不是他们打算做的!
更新2:
问题已通过SDK的最新更新得到解决.
我已经在我的API 22设备上进行了测试,并且正在调用onAttach(Context).
点击此处查看我几周前开设的错误报告以及Google员工的答案.
我想知道是否可以在没有支持库的情况下使用FragmentStatePagerAdapter类或其等价物?
我知道如何使用本机片段代替支持库中的那些片段,而许多其他类具有类似的等价物,但我找不到应该使用什么而不是这个类.
我基本上想扩展它在我的适配器中使用它像这样
public class ImageAdapter extends FragmentStatePagerAdapter {...
Run Code Online (Sandbox Code Playgroud) java android android-fragments android-viewpager android-support-library
我无法理解为什么我的objFragment给了我支持经理的问题,我收到了这个错误:
Error:(101, 17) error: no suitable method found for replace(int,android.support.v4.app.Fragment)
method FragmentTransaction.replace(int,android.app.Fragment,String) is not applicable
(actual and formal argument lists differ in length)
method FragmentTransaction.replace(int,android.app.Fragment) is not applicable
(actual argument android.support.v4.app.Fragment cannot be converted to android.app.Fragment by method invocation conversion)
Run Code Online (Sandbox Code Playgroud)
而这正是在这条线上的objFragment上发生的: .replace(R.id.container, objFragment)
这是我的完整代码:
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
/* public class MainActivity extends FragmentActivity {
public static FragmentManager fragmentManager;
@Override …Run Code Online (Sandbox Code Playgroud) 我是Android开发的新手。我们的老师告诉我们,我们应该开发应用程序(这是一项个人任务),应该利用片段。现在在API28中不推荐使用片段。到底为什么呢?您现在应该使用什么?对于Android开发中的不同事情,我有很多不同的看法,而作为一个新手,很难找到好的做法。