我想获取操作栏的实例,但是获取值为null,因此我的整个程序没有运行。
我包含此方法的代码是:
当我的代码跳转到需要操作栏的行时,它会给我空指针异常
HomeTab.java:
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
public class HomeTab extends Activity implements OnItemClickListener{
MyCustomAdapter myCustomAdapter;
Context context=HomeTab.this;
//ImageView menuimage;
String optionName[]=new String[] {"Alerts","Events","Newsletters","News","Parent Info","Logins","Parent Teacher Interviews","Flexi School LunchOrder","Contact US","Kool Content"};
//int image[]=new int[] {R.drawable.alert_small_icon,R.drawable.events,R.drawable.newsletter,R.drawable.newsletter,R.drawable.arrow_24,R.drawable.arrow_24,R.drawable.arrow_24,R.drawable.arrow_24,R.drawable.arrow_24,R.drawable.arrow_24};
int imageArrow[]=new int[]{R.drawable.right_arrow};
ArrayList<ListModel> mylist=new ArrayList<ListModel>();
// test for sliding drawer
/*
String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;*/
/*
* code from leftslide
*
*/
private DrawerLayout mDrawerLayout;
public ListView DrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
LayoutInflater inflater=null;
// code upto left slide
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view_layout);
//menuimage=(ImageView)findViewById(R.id.menuimage);
//new code below
/*menuimage=(ImageView)findViewById(R.id.menuimage);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
menu = new String[]{"Home","Android","Windows","Linux","Raspberry Pi","WordPress","Videos","Contact Us"};
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dList = (ListView) findViewById(R.id.left_drawer);*/
//new above
ListView listview=(ListView)findViewById(R.id.listView1);
getDatainList();
listview.setAdapter(new MyCustomAdapter(context, mylist));
listview.setBackgroundColor(Color.WHITE);
listview.setOnItemClickListener(this);
inflater=(LayoutInflater)HomeTab.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi=inflater.inflate(com.example.testlist.R.layout.sliding_activity_main, null);
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(com.example.testlist.R.array.nav_drawer_items);
//nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(com.example.testlist.R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) vi.findViewById(R.id.drawer_layout);
DrawerList = (ListView) vi.findViewById(R.id.left_draw);
DrawerList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// displayView(position);
Toast.makeText(getApplicationContext(), "hi", 300).show();
}
});
navDrawerItems = new ArrayList<com.ctpl.skoolbag.leftslide.NavDrawerItem>();
// adding nav drawer items to array
// Home
navDrawerItems.add(new com.ctpl.skoolbag.leftslide.NavDrawerItem(navMenuTitles[0],navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new com.ctpl.skoolbag.leftslide.NavDrawerItem(navMenuTitles[1],navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new com.ctpl.skoolbag.leftslide.NavDrawerItem(navMenuTitles[2],navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new com.ctpl.skoolbag.leftslide.NavDrawerItem(navMenuTitles[3],navMenuIcons.getResourceId(3, -1)));
// Pages
navDrawerItems.add(new com.ctpl.skoolbag.leftslide.NavDrawerItem(navMenuTitles[4],navMenuIcons.getResourceId(4, -1)));
// What's hot, We will add a counter here
navDrawerItems.add(new com.ctpl.skoolbag.leftslide.NavDrawerItem(navMenuTitles[5],navMenuIcons.getResourceId(5, -1)));
navDrawerItems.add(new com.ctpl.skoolbag.leftslide.NavDrawerItem(navMenuTitles[6],navMenuIcons.getResourceId(6, -1)));
navDrawerItems.add(new com.ctpl.skoolbag.leftslide.NavDrawerItem(navMenuTitles[7],navMenuIcons.getResourceId(7, -1)));
// Recycle the typed array
navMenuIcons.recycle();
// setting the nav drawer list adapter
adapter = new com.ctpl.skoolbag.leftslide.NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
DrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.cat, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
){
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.left_slide, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/***
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(DrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@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);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
private void getDatainList()
{
for(int i=0;i<10;i++)
{
ListModel li=new ListModel();
li.setOptionName(optionName[i]);
// li.setImage(image[i]);
li.setImageArrow(R.drawable.arrow);
mylist.add(li);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
switch (position) {
case 0:
Intent call_AlertWebService=new Intent(getApplicationContext(),AlertActivityFromService.class);
startActivity(call_AlertWebService);
break;
case 1:
Intent i=new Intent(getApplicationContext(),EventsFromWebService.class);
startActivity(i);
break;
case 2:
Intent in=new Intent(getApplicationContext(),NewslettersActivityFromService.class);
startActivity(in);
break;
case 3:
Intent inn=new Intent(getApplicationContext(),NewsFromService.class);
startActivity(inn);
break;
default:
break;
}
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
android.app.Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
/*case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;
*/
default:
break;
}
if (fragment != null) {
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(com.example.testlist.R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
DrawerList.setItemChecked(position, true);
DrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(DrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
}
答案 0 :(得分:-1)
检查你的minSdkVersion。
更改为
<uses-sdk android:minSdkVersion="11" />