我是Android开发的新手,所以这可能是一个新手的错误,但是我被卡住了。
我想在点击菜单时启动一项活动,但会出现以下异常:
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.mygymroutine.mygymroutine / com.mygymroutine.mygymroutine.activities.ActivitySchedule}: java.lang.IllegalArgumentException:找不到id 0x7f0d008b的视图 片段(com.mygymroutine.mygymroutine:id / fragment_schedule) ScheduleFragment {39f1839#0 id = 0x7f0d008b ...
这是相关的代码:
public class basic extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
displaySelectedScreen(R.id.nav_schedule);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.basic, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean onNavigationItemSelected(MenuItem item) {
displaySelectedScreen(item.getItemId());
return true;
}
private void displaySelectedScreen(int itemId) {
Intent intent=null;
switch (itemId) {
case R.id.nav_schedule:
intent=new Intent(this, ActivitySchedule.class);
startActivity(intent);
break;
}
活动代码
package com.mygymroutine.mygymroutine.activities;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.mygymroutine.mygymroutine.Fragments.ScheduleFragment;
import com.mygymroutine.mygymroutine.R;
public class ActivitySchedule extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acivity_schedule);
if(savedInstanceState==null){
ScheduleFragment fg = new ScheduleFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_schedule, fg)
.commit();
}
}
}
片段
package com.mygymroutine.mygymroutine.Fragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mygymroutine.mygymroutine.R;
public class ScheduleFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//returning our layout file
//change R.layout.yourlayoutfilename for each of your fragments
View rootView=inflater.inflate(R.layout.fragment_schedule, container, false);
FloatingActionButton fab=(FloatingActionButton)rootView.findViewById(R.id.fab_schedule);
return inflater.inflate(R.layout.fragment_schedule, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Schedule");
}
}
片段计划xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_schedule"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="Schedule"
android:textAppearance="?android:attr/textAppearanceLarge" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_schedule"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="41dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:clickable="true"
android:tint="@android:color/background_light"
app:backgroundTint="@android:color/holo_red_light"
app:fabSize="mini"
app:rippleColor="@android:color/transparent"
app:srcCompat="@android:drawable/ic_menu_add" />
</RelativeLayout>
活动时间表xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mygymroutine.mygymroutine.activities.ActivitySchedule">
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
在。 onOptionsItemSelected方法添加以下内容 (此方法带来一个参数 - MenuItem项)
switch (item.getItemId())
case R.id.myFirst
startActivity(new Intent(this, MyFirstActivity.class));
case R.id.mySecond
startActivity (newIntent(this, MySecondActivity.class));
等等。您可以使用必要的视图定义活动的布局。您也可以在清单文件中声明活动。
这很有效。我正在从主要开始我的活动。
答案 1 :(得分:0)
下面:
getSupportFragmentManager().beginTransaction().add(R.id.fragment_schedule, fg)
.commit();
您正在传递R.id.fragment_schedule
作为片段的容器。片段将被插入的位置。但是,R.id.fragment_schedule
是您Fragment
的一部分。所以,它还没有膨胀。您不能将其用作容器,因为它尚未创建。
我想你想要的是:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mygymroutine.mygymroutine.activities.ActivitySchedule"
<!-- ADD LINE BELOW -->
android:id="@+id/fragment_container" >
</android.support.constraint.ConstraintLayout>
并改变这一点:
getSupportFragmentManager().beginTransaction().add(R.id.fragment_schedule, fg)
.commit();
到此:
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fg)
.commit();