【问题标题】:App Crashes When Navigaiton Drawer Item is Pressed按下导航抽屉项目时应用程序崩溃
【发布时间】:2017-08-09 15:16:59
【问题描述】:

更新:问题已解决

根据这张图片,我正在尝试根据每个抽屉项目打开片段。目前我只为包含 a ListView 但我的应用程序 CRASHES 每次按下它的“Easy”制作片段。我的手机上运行的是 Android Nougat (v7.0)。

这是我的代码...

片段布局(fragment_easy.xml):

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="afm.pymonk.EasyFragment"
    android:id="@+id/easyFragment">

    <!-- TODO: Update blank fragment layout -->

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/easyList"/>

</RelativeLayout>

EasyFragment.java 中 onCreateView 内的片段 JAVA 代码:

View view = inflater.inflate(R.layout.fragment_easy, container, false);

String easyListContents[] = {"Introduction", "Hello World", "Operators", "Data Types",
            "Simple Arithmetic Operations", "Take input from user"};

ListView easyList = (ListView) view.findViewById(R.id.easyList);
ArrayAdapter<String> easyAdapter = new ArrayAdapter<String>(EasyFragment.this.getActivity(),
            android.R.layout.simple_spinner_dropdown_item, easyListContents);
easyList.setAdapter(easyAdapter);

easyList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        return;
    }
});

return view;

在主要活动 (Home.java) 中的 onNavigationItemSelected 中打开片段的代码:

int id = item.getItemId();

if (id == R.id.icon_easy) {
    EasyFragment easyFragment = new EasyFragment();
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.mainBase, easyFragment, easyFragment.getTag()).commit();
}

设备错误日志:

【问题讨论】:

  • 你能分享你的 logcat 错误日志吗?
  • @MoraS。我怎么做 ?告诉我,我一定会分享的。
  • 在 AndroidStudio 中,有一个底部面板“Android Monitor”应该可以让您访问设备日志developer.android.com/studio/profile/android-monitor.html
  • 感谢@MoraS。 ,现在我已将其包含在我的问题中。

标签: java android android-fragments navigation-drawer crash


【解决方案1】:

在 onCreateView 方法的片段中返回 view 而不是 return inflater.inflate(R.layout.fragment_easy, container, false);

已编辑:

在 MainActvity 中实现 OnFragmentInteractionListener

例如:

public class MainActivity extends AppCompatActivity
        implements OnFragmentInteractionListener {

// Implement the methods here
}

【讨论】:

  • 但我想,即使没有这个改变,应用程序也不应该崩溃,而是应该只显示一个什么都不做的静态布局
  • 它仍然崩溃。
  • 查看错误日志!我什至不明白日志的事情!也许你能找到...
  • @Shivam 你的片段中有 OnFragmentInteractionListener 接口吗..?
  • 不,我没有@MuthukrishnanRajendran
【解决方案2】:

您的easyFragment.getTag() 调用将返回null,因为没有setTag 调用,所以只需使用一些String 标记将其与您的片段绑定,而不是null

 manager.beginTransaction().replace(R.id.mainBase, easyFragment, "easyFrag").commit();
//                                                                 ^^^^^^

更新:

您正在片段中使用接口侦听器回调,但您的活动中没有实现,因此请确保这样做

class Home extends AppCompatActivity implements OnFragmentInteractionListener {
  //                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 // override methods of OnFragmentInteractionListener and provide their implementation
}

【讨论】:

    猜你喜欢
    • 2018-06-12
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多