所以我试图通过单击默认模板导航栏上的按钮来启动片段,我正在使用片段事务和提交,但应用程序崩溃,无法找到隐藏在android监视器错误部分中的活动在下拉。我使用替换和添加来解决此问题。
以下是我要添加的片段:
public class FoodScroll extends ListFragment implements AdapterView.OnItemClickListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_foodscroll, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.foods, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Toast.makeText(getActivity(), "Item" + position, Toast.LENGTH_SHORT).show();
}
}
以下是我尝试将其放入的活动:
public class MainActivity extends AppCompatActivity{
private FragmentManager manager = getFragmentManager();
private FragmentTransaction transaction = manager.beginTransaction();
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
FoodScroll foods = new FoodScroll();
transaction.replace(R.id.container, foods);
transaction.commit();
return true;
case R.id.navigation_dashboard:
return true;
case R.id.navigation_notifications:
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
包语句不缺,我把它从代码片段中删除了。 如果我错误地放置代码或其他东西,请道歉。
logcat无法正常工作,我不得不重新启动android studio才能让它再次运行。这就是出现的内容
03-15 22:57:17.090 3238-3238/? I/art: Not late-enabling -Xcheck:jni (already on)
03-15 22:57:17.090 3238-3238/? W/art: Unexpected CPU variant for X86 using defaults: x86
03-15 22:57:17.110 3238-3245/? E/art: Failed sending reply to debugger: Broken pipe
03-15 22:57:17.110 3238-3245/? I/art: Debugger is no longer active
03-15 22:57:17.110 3238-3245/? I/art: Starting a blocking GC Instrumentation
03-15 22:57:17.199 3238-3238/? W/System: ClassLoader referenced unknown path: /data/app/com.namename.www.name-2/lib/x86
03-15 22:57:17.253 3238-3238/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
03-15 22:57:17.393 3238-3238/? I/InstantRun: Starting Instant Run Server for com.namename.www.name
03-15 22:57:17.454 3238-3262/? I/OpenGLRenderer: Initialized EGL, version 1.4
03-15 22:57:17.454 3238-3262/? D/OpenGLRenderer: Swap behavior 1
03-15 22:57:17.454 3238-3262/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
03-15 22:57:17.454 3238-3262/? D/OpenGLRenderer: Swap behavior 0
03-15 22:57:17.488 3238-3238/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
03-15 22:57:25.564 3238-3238/com.chilosomexicanbistro.www.chiloso D/AndroidRuntime: Shutting down VM
03-15 22:57:25.564 3238-3238/com.chilosomexicanbistro.www.chiloso E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.namename.www.name, PID: 3238
java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
at android.app.ListFragment.ensureList(ListFragment.java:402)
at android.app.ListFragment.onViewCreated(ListFragment.java:203)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1010)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
at android.app.BackStackRecord.run(BackStackRecord.java:816)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1578)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
&#13;
答案 0 :(得分:1)
这里有几个问题:
问题1 - 错误地使用静态初始化
private FragmentManager manager = getFragmentManager(); //no! don't do this
private FragmentTransaction transaction = manager.beginTransaction(); //no!
使用静态初始化对Android Activity lifecycles不起作用。请仔细研究这些内容并仔细阅读Android文档中的示例 - 您很难在示例活动和碎片中找到任何静态初始化。
问题2 - 不检查FragmentManager以查看它是否已包含片段
switch (item.getItemId()) {
case R.id.navigation_home:
FoodScroll foods = new FoodScroll(); //no! check first if the fragment is already in the FragmentManager
transaction.replace(R.id.container, foods);
transaction.commit();
return true;
此代码错误 - 再次,在示例中,您几乎总是看到以getFragmentManager().beginTransaction()...
惯用写入的调用。请尝试更仔细地遵循文档中的示例片段和活动。相信我,它会节省你的时间:)。
而不是上面的代码,我想你想要这样的东西:
case R.id.navigation_home:
FoodScroll foods = getFragmentManager().findFragmentByTag("FOOD");
if (foods == null) {
foods = new FoodScroll();
}
getFragmentManager().beginTransaction().replace(R.id.container, foods, "FOOD").commit();
return true;
问题3 - 在使用正确的堆栈跟踪进行OP编辑后更新:
根据Mike M的评论,在ListFragment
中,如果使用您自己的布局,则ListView
的ID必须为@android:id/list
。