2017-06-17 100 views
0

我看着從這個傢伙的教程:
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(); 

我重新命名content_main在activity_main_menu1_start_seite我認爲我可以用activity_main_menu1_start_seite替換content_main,但它會說「找不到符號變量activity_main_menu1_start_seite」

也許你需要我的整個Co德。
我把你的錯誤標記爲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, fragment); 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> 
+1

您是否也可以發佈您的XML文件? –

+0

確定如果我知道如何......代碼長 –

+0

代碼的長度並不重要,只需複製整個代碼並將其粘貼到此處就像發佈您的Java類一樣。編輯你的問題並粘貼XML文件的代碼。 –

回答

0

R類是基於文件名稱和內容在res子文件夾中生成你的項目。如果Android Studio不自動執行此操作,則可以通過單擊工具欄上的「生成」按鈕來強制手動生成。

0

其中xml包含的標識爲activity_main_menu1_start_seite

重命名XML文件隻影響其他xml文件中的R.layout值和include標記。

如果找不到R.id.activity_main_menu1_start_seite,則沒有包含該名稱的android:id。具體來說,您需要在activity_main_drawer.xml範圍內具有該值的ViewGroup(例如FrameLayout)

相關問題