我刚刚开始学习Android Studio的布局和基本 UI 元素,以及它如何组织文件drawable, layouts, strings, activities
等。
因此,通过遵循一些 youtube 教程,我设法将一个基本navigation
小部件添加到activity
具有 5 个导航项的小部件中。唯一navigation item
响应点击的是第一个是仪表板。其余的返回一个“不幸的是应用程序已停止”对话框。
这是我的代码Activity.java
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
public class StudentActivity extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_dashboard: //this works
AlertDialog.Builder builder1 = new AlertDialog.Builder(StudentActivity.this);
builder1.setMessage("Dashboard To.");
builder1.setCancelable(true);
builder1.show(); // I'm able to see the dialog here
setTitle("Dashboard");
DashboardFragment dashboard_fragment = new DashboardFragment();
FragmentTransaction transaction1 = getFragmentManager().beginTransaction();
transaction1.replace(R.id.frameLayout,dashboard_fragment);
return true;
case R.id.navigation_learn: //this doesn't work
try {
AlertDialog.Builder builder2 = new AlertDialog.Builder(StudentActivity.this);
builder2.setMessage("Learn To.");
builder2.setCancelable(true);
builder2.show(); //dialog doesn't even show up
setTitle("Learn");
DashboardFragment learn_fragment = new DashboardFragment();
FragmentTransaction manager1 = getFragmentManager().beginTransaction();
manager1.replace(R.id.frameLayout, learn_fragment);
manager1.commit();
}catch(Exception e){
AlertDialog.Builder errorDialog = new AlertDialog.Builder(StudentActivity.this);
errorDialog.setMessage(e.getMessage());
errorDialog.setCancelable(true);
errorDialog.show();
}
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
下面是我的navigation.xml
样子
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_dashboard"
android:title="Dashboard" />
<item
android:id="@+id/navigation_learn"
android:icon="@drawable/ic_openbook2"
android:title="Learn" />
<item
android:id="@+id/navigation_play"
android:icon="@drawable/ic_game2"
android:title="Play" />
<item
android:id="@+id/navigation_review"
android:icon="@drawable/ic_shapes"
android:title="Review" />
<item
android:id="@+id/navigation_profile"
android:icon="@drawable/ic_user2"
android:title="My Profile" />
</menu>
我已验证所有drawable
图标都存在且名称均为小写。
我确保包含正确的导入import android.app.FragmentTransaction;
。我做了一些研究,发现还有一个android.support.v4.app.FragmentTransaction
不正确的。
构建成功,我可以使用Nox模拟器启动应用程序。Message Gradle Build窗口中没有消息。
可能是什么原因?关于如何解决这个问题的任何建议?
提前致谢。编辑:这是我点击第二个导航项时遇到的错误。
java.lang.IllegalArgumentException: No view found for id 0x7f0a004b (com.example.peace:id/frameLayout) for fragment LearnFragment{4bbf1c20 #1 id=0x7f0a004b}
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
这是LearnFragment.java
package com.example.peace.cai;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class LearnFragment extends Fragment {
public LearnFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_learn, container, false);
}
}
在这里fragment_learn.xml
<FrameLayout 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="com.example.peace.cai.LearnFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>