在android中找不到片段的视图
我正在使用四个xml文件。当我尝试启动一个片段时,我没有找到任何视图错误我正在使用四个xml文件,并且有一个带有导航栏的Main活动我想在从导航栏中单击一个选项时启动一个片段,但当我单击一个项目时,我没有找到tab1片段的视图在android中找不到片段的视图,android,android-fragments,Android,Android Fragments,我正在使用四个xml文件。当我尝试启动一个片段时,我没有找到任何视图错误我正在使用四个xml文件,并且有一个带有导航栏的Main活动我想在从导航栏中单击一个选项时启动一个片段,但当我单击一个项目时,我没有找到tab1片段的视图 **activity main xml** < ?xml version="1.0" encoding="utf-8"?> < android.support.v4.widget.Drawer
**activity main xml**
<
?xml version="1.0" encoding="utf-8"?>
<
android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
This is number 2 App main bar.xml
Code:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
This is Number 3 content main.xml
Code:
<
FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container_frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</FrameLayout>
This is number 4 nav header
Code:
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:src="@drawable/logo192"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EDX Engine Option" />
This is the mainactivity
package com.example.sabarinathan.navigationdrawer;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Toast.makeText(getApplicationContext(),"test",Toast.LENGTH_LONG).show();
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);
}
@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.main, 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);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.Sample) {
Toast.makeText(getApplicationContext(),"Sample",Toast.LENGTH_LONG).show();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Tab1 fragment = new Tab1();
fragmentTransaction.replace(R.id.container_frame_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else if (id == R.id.EnterProfile) {
} else if (id == R.id.RequestUniv) {
} else if (id == R.id.ViewSuggestions) {
}
else if(id== R.id.Home){
}
else if(id== R.id.AdiitionalApps){
}
else if(id== R.id.Coupon){
}
else if(id== R.id.FAQ){
}
else if(id== R.id.FlashHelp){
}
else if(id== R.id.Premium){
}
else if(id== R.id.ABoutUs){
}
else if(id== R.id.Tab1){
}
else if(id== R.id.Tab2){
}
else if(id== R.id.Tab3){
}
else if(id== R.id.Tab4){
}
else if(id== R.id.Tab5){
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
This is the fragment class
package com.example.sabarinathan.navigationdrawer;
// This project was created by Ferdousur Rahman Shajib
// www.androstock.com
// You use this project anytime, anywhere. There is no copywrite issue.
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
/**
* Created by Ferdousur Rahman Shajib on 27-10-2015.
*/
public class Tab1 extends Fragment {
Activity activity;
Button button1;
private WebView webView;
private Bundle webViewBundle;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1,container,false);
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.tab_1,
container, false);
activity = getActivity();
webView = (WebView) ll.findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient());
if (webViewBundle == null) {
webView.loadUrl("https://edxengine.com/services/MyUniversity/");
} else {
webView.restoreState(webViewBundle);
}
return ll;
}
@Override
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
webView.saveState(webViewBundle);
}
}
This the fragment xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
when i start a fragment i get no view found error
**活动主xml**
<
?xml version=“1.0”encoding=“utf-8”?>
<
android.support.v4.widget.DrawerLayout 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:id=“@+id/drawer\u布局”
android:layout\u width=“匹配父项”
android:layout\u height=“match\u parent”
android:fitsSystemWindows=“true”
工具:openDrawer=“start”>
这是2号App main bar.xml
代码:
这是第3个content main.xml
代码:
<
FrameLayout xmlns:android=”http://schemas.android.com/apk/res/android"
android:layout\u width=“匹配父项”
android:layout\u height=“match\u parent”>
这是4号导航头
代码:
这是主要的活动
包com.example.sabarinathan.navigationdrawer;
导入android.app.FragmentManager;
导入android.app.FragmentTransaction;
导入android.os.Bundle;
导入android.support.design.widget.FloatingActionButton;
导入android.support.design.widget.Snackbar;
导入android.support.v4.app.Fragment;
导入android.view.view;
导入android.support.design.widget.NavigationView;
导入android.support.v4.view.GravityCompat;
导入android.support.v4.widget.DrawerLayout;
导入android.support.v7.app.ActionBarDrawerToggle;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.widget.Toast;
公共类MainActivity扩展了AppCompatActivity
实现NavigationView.OnNavigationItemSelectedListener{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
Toast.makeText(getApplicationContext(),“test”,Toast.LENGTH_LONG.show();
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
ActionBarDrawerToggle切换=新建ActionBarDrawerToggle(
这,抽屉,工具栏,R.string.navigation\u drawer\u open,R.string.navigation\u drawer\u close);
抽屉。设置抽屉定位器(开关);
toggle.syncState();
NavigationView NavigationView=(NavigationView)findViewById(R.id.nav_视图);
navigationView.setNavigationItemSelectedListener(此);
}
@凌驾
public void onBackPressed(){
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
if(抽屉isDrawerOpen(重力压缩机启动)){
抽屉。关闭抽屉(重力压缩机启动);
}否则{
super.onBackPressed();
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
@SuppressWarnings(“StatementWithEmptyBody”)
@凌驾
公共布尔值onNavigationItemSelected(MenuItem项){
//处理导航视图项单击此处。
int id=item.getItemId();
if(id==R.id.Sample){
Toast.makeText(getApplicationContext(),“Sample”,Toast.LENGTH_LONG.show();
FragmentManager FragmentManager=getFragmentManager();
FragmentTransaction FragmentTransaction=fragmentManager.beginTransaction();
Tab1片段=新Tab1();
fragmentTransaction.replace(R.id.container\u frame\u布局,fragment);
fragmentTransaction.addToBackStack(空);
fragmentTransaction.commit();
}else if(id==R.id.EnterProfile){
}else if(id==R.id.RequestUniv){
}else if(id==R.id.ViewSuggestions){
}
else if(id==R.id.Home){
}
else if(id==R.id.aditionalapps){
}
否则如果(id==R.id.优惠券){
}
else if(id==R.id.FAQ){
}
else if(id==R.id.FlashHelp){
}
else if(id==R.id.Premium){
}
else if(id==R.id.ABoutUs){
}
else if(id==R.id.Tab1){
}
else if(id==R.id.Tab2){
}
else if(id==R.id.Tab3){
}
else if(id==R.id.Tab4){
}
else if(id==R.id.Tab5){
}
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
抽屉。关闭抽屉(重力压缩机启动);
返回true;
}
}
这是片段类
包com.example.sabarinathan.navigationdrawer