java - android - 抽屉图标点击不起作用

标签 java android android-layout

我是java和android的初学者。我正在尝试添加一个抽屉来增加 Activity 。我创建了一个基础 Activity 。一切正常,除了当我单击操作栏上的抽屉图标时没有显示菜单。怎么了!?

DrawerActivity.java

package com.myapp.app;


import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.AdapterView;


public class DrawerActivity extends Activity {

    private String[] mPlanetTitles;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout fullLayout;
    private FrameLayout frameLayout;


    FrameLayout progressBarHolder;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void forceRTLIfSupported()
    {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
            getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }
    }

    @Override
    public void setContentView(int layoutResID) {

        fullLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_drawer, null);
        frameLayout = (FrameLayout) fullLayout.findViewById(R.id.content_frame);

        getLayoutInflater().inflate(layoutResID, frameLayout, true);

        super.setContentView(fullLayout);

        //Your drawer content...

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_drawer);

         mPlanetTitles = new String[]{getString(R.string.menuAdsGold), getString(R.string.menuAds), getString(R.string.menuSearch), getString(R.string.menuAcc), getString(R.string.menuAbout) };
         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
         mDrawerList = (ListView) findViewById(R.id.left_drawer);

         // Set the adapter for the list view
         mDrawerList.setAdapter(new ArrayAdapter<String>(this,
         R.layout.drawer_list_item, mPlanetTitles));
         // Set the list's click listener
         mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

         mDrawerToggle = new ActionBarDrawerToggle(
         this, /* host Activity */
         mDrawerLayout, /* DrawerLayout object */
         R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
         R.string.drawer_open, /* "open drawer" description */
         R.string.drawer_close /* "close drawer" description */
         ) {

         /** Called when a drawer has settled in a completely closed state. */
         public void onDrawerClosed(View view) {
         //getActionBar().setTitle(mTitle);
         }

         /** Called when a drawer has settled in a completely open state. */
         public void onDrawerOpened(View drawerView) {
             drawerView.bringToFront();
         //getActionBar().setTitle(mTitle);
         }
         };

         // Set the drawer toggle as the DrawerListener
         mDrawerLayout.setDrawerListener(mDrawerToggle);

         getActionBar().setDisplayHomeAsUpEnabled(true);
         getActionBar().setHomeButtonEnabled(true); 

         getActionBar().setTitle(getString(R.string.app_name));


    }


    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @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
    protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
    } 

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    //Pass the event to ActionBarDrawerToggle, if it returns
    //true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
    return true;
    }
    //Handle your other action bar items...

    return super.onOptionsItemSelected(item);
    }

    /**
    * Swaps fragments in the main content view
    */
    private void selectItem(int position) {
    //Toast.makeText(this, R.string.app_name, Toast.LENGTH_SHORT).show();

    //Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    //setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);

    switch (position) {
    case 0:
        break;
    case 1:
        break;

        /*
    case 2:
        Intent i = new Intent(this,SearchActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        this.startActivity(i);

        break;

    case 3:
        Intent i = new Intent(this,AccActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        this.startActivity(i);

        break;

    case 4:
        Intent i = new Intent(this,AboutActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        this.startActivity(i);

        break;
*/

    default:
        break;
    }

    }



    private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    selectItem(position);
    }
    } 

    public boolean isOnline() {
        ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        return netInfo != null && netInfo.isConnectedOrConnecting();
    }

    public void showProgress(Boolean show) {
        // prepare for a progress bar dialog
        if(show) {
                    progressBarHolder = (FrameLayout) findViewById(R.id.progressBarHolder);
                    progressBarHolder.setVisibility(View.VISIBLE);
                    progressBarHolder.bringToFront();

        } else {

            progressBarHolder.setVisibility(View.GONE);

        }

    }
    public void showProgress(Boolean show,int sec) {
        // prepare for a progress bar dialog
        if(show) {
                    progressBarHolder = (FrameLayout) findViewById(R.id.progressBarHolder);
                    progressBarHolder.setVisibility(View.VISIBLE);
                    progressBarHolder.bringToFront();

        } else {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {

                    progressBarHolder.setVisibility(View.GONE);

            }
        }, sec);

        }

    }
}

MainActivity.java

package com.myapp.app;


import java.util.Arrays;

import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import com.amlaksara.app.Estate;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;


public class MainActivity extends DrawerActivity {

    private TextView htext;
    private ListView listView1;

    AsyncHttpClient client =null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ads);
//      super.onCreateDrawer();


        try{
        String ex =  getIntent().getExtras().getString("bGold");
        if(ex.equals("false"))
            bGold=false;
        else
            bGold=true;
        } catch(Exception e) {

        }

         getActionBar().setTitle(getString(R.string.app_name));

//      reload_stream();

//        listView1 = (ListView)findViewById(R.id.listView1);
  //      View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
   //     listView1.addHeaderView(header);
   //     View footer = (View)getLayoutInflater().inflate(R.layout.listview_footer_row, null);
   //     listView1.addFooterView(footer);

    }




}

activity_drawer.xml

    <android.support.v4.widget.DrawerLayout  xmlns:android="http://schemas.android.com/apk/res/android"
                                             xmlns:tools="http://schemas.android.com/tools"
                                             android:id="@+id/drawer_layout"
                                             android:layout_width="match_parent"
                                             android:layout_height="match_parent"
                                             android:layoutDirection="rtl"
                                             tools:context=".AdsGoldActivity" >

            <FrameLayout
            android:id="@+id/progressBarHolder"
            android:animateLayoutChanges="true"
            android:visibility="gone"
            android:alpha="0.4"
            android:background="#000000"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ProgressBar
                style="?android:attr/progressBarStyleLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:indeterminate="true"
                android:layout_gravity="center" />
        </FrameLayout>

        <!-- The main content view -->
        <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent">





        </FrameLayout>



        <!-- The navigation drawer -->
        <ListView
                android:id="@+id/left_drawer"
                android:layout_width="240dp"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:choiceMode="singleChoice"
                android:divider="@android:color/transparent"
                android:dividerHeight="0dp"
                android:background="#111"/>




    </android.support.v4.widget.DrawerLayout>

activity_main.xml

<FrameLayout
                            xmlns:android="http://schemas.android.com/apk/res/android"
                            xmlns:tools="http://schemas.android.com/tools"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layoutDirection="rtl"
                            tools:context=".AdsGoldActivity" >



<ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</FrameLayout>

更新: 我发现当我将 DrawerActivity 更改为启动 Activity 时它可以工作,但是当我将 MainActivity 设置为启动 Activity 时,图标不起作用。

最佳答案

可以使用getSupportActionBar()代替getActionBar()方法。并使用 ActionBarActivity 而不是 Activity 扩展您的 DrawerActivity

关于java - android - 抽屉图标点击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30427005/

相关文章:

java - 相同的对象不同的哈希码?

android - 如何在 ViewPager2 中实现 Spinner

android - 尝试从 android 中的 sqlite 数据库中删除行时出现磁盘已满异常

java - Android pdf maker 代码错误

Android:如何在同一布局中将按钮状态链接到 TextView 状态

java - 无法为 com.android.build.gradle.internal.api.DefaultAndroidSourceSet 类型的源集 main 设置未知属性 's'

java - 如何在Android App中获取通话结束事件

java - 行为差异 : Intellij vs Eclipse

android - 在选项卡中滑动时如何避免 onCreateView()

java - 当尝试声明 editText 框显示在按钮单击上时,无法解析符号 'search'