我看了这个家伙的教程:
https://www.youtube.com/watch?v=-SUvA1fXaKw
最后(约9:55),他写下了:
if(fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_main, fragment);
ft.commit();
我在activity_main_menu1_start_seite中重命名了我的content_main,我以为我可以用activity_main_menu1_start_seite替换content_main,但后面却说“找不到符号变量activity_main_menu1_start_seite”
也许你需要我的整个代码 我把错误标记为Blockquote 对不起任何问题(我是本网站的新用户)
package com.d_and_d_studios.test3;
import android.Manifest;
import android.app.Fragment;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentTransaction;
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.view.View;
import android.widget.TextView;
import android.widget.Toast;
import at.markushi.ui.CircleButton;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
CircleButton btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
btn = (CircleButton) findViewById(R.id.circleButton);
}
@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;
}
//Brauche ich vielleicht doch noch irgendwann!
//@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);
//}
private void displaySelectedScreen(int id) {
Fragment fragment = null;
switch (id) {
case R.id.nav_powersavingmode:
fragment = new Menu_2_Energiesparmodus();
break;
case R.id.nav_feedback:
fragment = new Menu_3_Bewerte_die_App();
break;
case R.id.nav_send:
fragment = new Menu_4_Teile_uns_Fehler_mit();
}
if(fragment!= null){ FragmentTransaction ft = getSupportFragmentManager()。beginTransaction(); ft.replace(R.id.activity_main_menu1_start_seite,片段); ft.commit(); }
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_powersavingmode) {
// Handle the camera action
} else if (id == R.id.nav_feedback) {
} else if (id == R.id.nav_send) {
}
displaySelectedScreen(id);
return true;
}
}
<?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"
android:background="@color/backgroundcolor"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.d_and_d_studios.test3.MainActivity"
tools:showIn="@layout/toolbar_main">
<at.markushi.ui.CircleButton
android:id="@+id/circleButton"
android:layout_width="116dp"
android:layout_height="116dp"
android:layout_centerInParent="true"
android:src="@drawable/ic_power_settings_new_black_24dp"
app:cb_color="#000"
app:cb_pressedRingWidth="20dp"
tools:layout_editor_absoluteY="178dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/menu1_text1"
android:fontFamily="sans-serif"
android:textColor="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.23" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
R
类是根据项目res
子文件夹中文件的名称和内容生成的。如果Android Studio不自动执行此操作,您可以通过单击工具栏上的“构建”按钮强制手动构建。
答案 1 :(得分:0)
哪个xml包含activity_main_menu1_start_seite
的ID?
重命名XML文件只会影响其他xml文件中的R.layout
值和include
标记。
如果找不到R.id.activity_main_menu1_start_seite
,则不会包含具有该名称的android:id
。具体来说,您需要activity_main_drawer.xml
内具有该值的ViewGroup(例如FrameLayout)