Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

android - DemoActivity.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details

i get the following error in my gradle console. Tried looking for the deprecated API but couldn't find it. Thanks in advance!!!

Error: DemoActivity.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.

public class BaseActivity extends ActionBarActivity {
private static final String TAG = "DemoActivity";
DrawerLayout mDrawerlDrawerLayout;
FrameLayout actContent;
private LinearLayout linear;
private ActionBarDrawerToggle actionBarToggle;
private Toolbar toolbar;
private ImageView mImageView;
private int height;
private SlidingUpPanelLayout mSlidingUpPanelLayout;
private ListView drawerListView;
private ArrayAdapter<String> navigationDrawerAdapter;
private String[] drawerItem = {"Email", "Wink", "Favourite", "Match me", "About"};

    @Override
    public void setContentView(final int layoutResID) {
    // TODO Auto-generated method stub


    linear = (LinearLayout) getLayoutInflater().inflate(R.layout.base_activity, null);
    actContent = (FrameLayout) linear.findViewById(R.id.frame_container);
    mDrawerlDrawerLayout = (DrawerLayout) linear.findViewById(R.id.drawer_layout);
    // set the drawer layout as base_activity content view of Activity.


    toolbar = (Toolbar) linear.findViewById(R.id.main_toolbar);
    setSupportActionBar((Toolbar) linear.findViewById(R.id.main_toolbar));
    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            return true;
        }
    });
    // Inflate a menu to be displayed in the toolbar
    toolbar.inflateMenu(R.menu.demo);
    //toolbar.getBackground().setAlpha(0);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    setContentView(linear);
    // add layout of BaseActivities inside framelayout.i.e. frame_container
    getLayoutInflater().inflate(layoutResID, actContent, true);

    initDrawerView();
    initDrawer();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.demo, menu);

    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.e("Drawer", "clicked");
    if (actionBarToggle.onOptionsItemSelected(item)) {
        Log.e("Drawer item clicked", "clicked");
        return true;
    }

    if (item.getItemId() == android.R.id.home) {
        Log.e("Drawer item clicked item id", "clicked");
        try {
            if (mDrawerlDrawerLayout.isDrawerOpen(drawerListView)) {
                mDrawerlDrawerLayout.closeDrawer(drawerListView);
            } else {
                mDrawerlDrawerLayout.openDrawer(drawerListView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
    switch (item.getItemId()) {
        case R.id.action_search: {
            return true;
        }
        case R.id.action_settings: {
            return true;
        }

    }
    return super.onOptionsItemSelected(item);
}

private void initDrawerView() {
    drawerListView = (ListView) findViewById(R.id.list_slidermenu);
    mDrawerlDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    navigationDrawerAdapter = new ArrayAdapter<String>(BaseActivity.this,   
android.R.layout.simple_list_item_1, drawerItem);
    drawerListView.setAdapter(navigationDrawerAdapter);
    drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}

private void initDrawer() {
    actionBarToggle = new ActionBarDrawerToggle(BaseActivity.this, mDrawerlDrawerLayout, toolbar, 
R.string.open_drawer, R.string.close_drawer) {

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);

        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };
    mDrawerlDrawerLayout.setDrawerListener(actionBarToggle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    actionBarToggle.syncState();
}

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

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try {
            Log.e("Drawer item click", "click");
            mDrawerlDrawerLayout.closeDrawers();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are probably using an older version of the appcompat library. Can you show the import statements that this class uses?

Based on the newer version of appcompat, use

import android.support.v7.app.ActionBarDrawerToggle;

instead of

import android.support.v4.app.ActionBarDrawerToggle;

Also, reference the new version of appcompat in build.gradle

dependencies {
    compile 'com.android.support:appcompat-v7:21.0.+'
}

More on the deprecated class at: https://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...