我正在尝试在我的应用中制作导航抽屉,但问题仍然存在于R.id.somecases中。我试图将项目与gradle文件同步并重新重建它并重新启动我的电脑,但没有任何改变。我会在每个错误旁边放一个注释,以帮助你理解希望帮助
package com.example.fatima.navigation;
import android.app.Activity;
import android.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.view.ActionProvider;
import android.support.v4.view.MenuItemCompat;
import android.view.MenuInflater;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;
import android.widget.AdapterView;
import android.content.res.Configuration;
import android.app.FragmentManager;
import android.content.Intent;
import android.widget.ShareActionProvider;
import layout.ContactUs;
import layout.DescriptionOfMarkets;
import layout.TopFragment;
import layout.ViewAllDevices;
import android.app.FragmentTransaction;
public class MainActivity extends Activity {
//AppCompatActivity
private String[] titles;
private ListView drawerList;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ShareActionProvider shareActionProvider;
private int currentPosition = 0;
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titles = getResources().getStringArray(R.array.titles);
drawerList = (ListView) findViewById(R.id.drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, titles));
drawerList.setOnItemClickListener(new DrawerItemClickListener());
if (savedInstanceState != null) {
currentPosition = savedInstanceState.getInt("position");
setActionbarTitle(currentPosition);
} else {
selectItem(0);
}
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open_drawer, R.string.close_drawer) {
@Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);// here MOMKN TRU7 HAIDEEEEE
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getFragmentManager().addOnBackStackChangedListener(new FragmentManager().OnBackStackChangedListener()){
public void onBackStateChangedListener() {
FragmentManager fragMan = getFragmentManager();
Fragment fragment = fragMan.findFragmentByTag("visible_fragment");
if (fragment instanceof TopFragment) {
currentPosition = 0;
}
if (fragment instanceof DescriptionOfMarkets) {
currentPosition = 1;
}
if (fragment instanceof ViewAllDevices) {
currentPosition = 2;
}
if (fragment instanceof ContactUs) {
currentPosition = 3;
}
setActionbarTitle(currentPosition);
drawerList.setItemChecked(currentPosition, true);
}
}
};
private void selectItem(int position){
Fragment fragment;
currentPosition=position;
switch (position){
case 1:
fragment = new TopFragment();
break;
case 2:
fragment = new DescriptionOfMarkets();
break;
case 3:
fragment = new ViewAllDevices();
break;
case 4:
fragment = new ContactUs();
break;
default:
fragment = new TopFragment();
}
FragmentTransaction ft=getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment,"visible_fragment");
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
setActionbarTitle(position);
//close the navigational drawer
DrawerLayout drawerlayout=(DrawerLayout)findViewById(R.id.drawer_layout);
drawerlayout.closeDrawer(drawerList);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
boolean drawerOpen= drawerLayout.isDrawerOpen(drawerList);
menu.findItem(R.id.action_share).setVisible(!drawerOpen);// HERE IS : R.id.action_share(can't resolve symbol)
return super.onPrepareOptionsMenu(menu);
}
@Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("position", currentPosition);
}
private void setActionbarTitle(int position) {
String title;
if (position == 0) {
title = getResources().getString(R.string.app_name);
} else {
title = titles[position];
}
getActionBar().setTitle(title);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);// HERE IS : R.menu.menu_main (can't resolve symbol)
MenuItem menuItem= menu.findItem(R.id.action_share);// HERE IS : R.id.action_share(can't resolve symbol)
shareActionProvider=(ShareActionProvider)menuItem.getActionProvider();
setIntent("this is example text");
return super.onCreateOptionsMenu(menu);
}
private void setIntent(String text){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,text);
shareActionProvider.setShareIntent(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
if (drawerToggle.onOptionsItemSelected(item)){
return true;
}
switch (item.getItemId()){
case R.id.action_create_order:// HERE IS : R.id.action_create_order(can't resolve symbol)
Intent intent = new Intent(this,OrderActivity.class);
startActivity(intent);
return true;
case R.id.action_settings:// HERE IS : R.id.action_settings(can't resolve symbol)
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
这是gradle构建 申请插件:&#39; com.android.application&#39;
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.example.fatima.navigation"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
}
答案 0 :(得分:0)
问题不太可能与您的代码有关。其他地方可能存在问题,例如在布局代码中,这会阻止构建成功。寻找构建错误。假设您使用的是Android Studio,请单击窗口底部附近的“0:Messages”。