嘿伙计们我的应用程序打开闪存卡的未来我已经添加到我的应用程序中。当你点击闪存卡按钮时我已经输入它应该打开闪存卡类但是当你点击闪存卡按钮时我得到一个这个应用程序已经停止错误。继承logcat:
FATAL EXCEPTION: main
Process: com.llamacorp.equate, PID: 4491
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.llamacorp.equate/gabilheri.com.flashcards.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at gabilheri.com.flashcards.DrawerLayoutActivity.onCreate(DrawerLayoutActivity.java:43)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
继承人主要活动:
package gabilheri.com.flashcards;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.widget.BaseAdapter;
import com.llamacorp.equate.R;
import java.util.ArrayList;
import java.util.HashMap;
import gabilheri.com.flashcards.database.MyDbHelper;
import gabilheri.com.flashcards.fragments.DefaultFragment;
import gabilheri.com.flashcards.fragments.FragmentCategories;
import gabilheri.com.flashcards.fragments.FragmentDecks;
import gabilheri.com.flashcards.fragments.FragmentFlashCardViewer;
import gabilheri.com.flashcards.fragments.FragmentFlashCardsList;
import gabilheri.com.flashcards.fragments.FragmentNewCategory;
import gabilheri.com.flashcards.fragments.FragmentNewDeck;
import gabilheri.com.flashcards.fragments.FragmentNewFlashCard;
import gabilheri.com.flashcards.navDrawer.NavDrawerAdapter;
import gabilheri.com.flashcards.navDrawer.NavDrawerItem;
public class MainActivity extends DrawerLayoutActivity {
private static final String TAG_ACTIVE_FRAGMENT = "fragment_active";
// We use this to know which of the items has been selected.
// We name the items so we know which one is which.
// For the fragments that will be OUTSIDE of the drawer layout we use negative numbers so we avoid a conflict.
public static final int FLASHCARDS_VIEWER = -6;
public static final int FLASHCARDS_FRAG = -5;
public static final int DECKS_FRAG = -4;
public static final int NEW_FLASHCARD_FRAG = -3;
public static final int NEW_DECK_FRAG = -2;
public static final int NEW_CATEGORY_FRAG = -1;
public static final int CATEGORIES_FRAG = 0;
public static final int SETTINGS_FRAG = 1;
private DefaultFragment activeFragment = null;
private NavDrawerAdapter mNavDrawerAdapter;
private ArrayList<NavDrawerItem> navDrawerItems;
private String[] navMenuTitles;
private HashMap<Integer, String> fragmentTitles;
private Bundle currentBundle;
@Override
public void init() {
// Retrieve the typedArray from the XML. Notice the weird Syntax "obtain"
TypedArray navIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_titles); // Retrieve the titles
navDrawerItems = new ArrayList<NavDrawerItem>(); // Initialize the ArrayList
// Now let's add add items to the ArrayList of NavDrawer items.
for(int i = 0; i < navMenuTitles.length; i++) {
navDrawerItems.add(new NavDrawerItem(navMenuTitles[i], navIcons.getDrawable(i)));
}
// An typed array can be recycled to avoid waste of System Resources. In our case it wouldn't matter because we only have 2 items.. but is still a good practice.
navIcons.recycle();
mNavDrawerAdapter = new NavDrawerAdapter(this, navDrawerItems);
// We need a HashMap to map the Title of the fragments that are not on our Nav Drawer
fragmentTitles = new HashMap<Integer, String>();
fragmentTitles.put(NEW_CATEGORY_FRAG, getString(R.string.new_cat_title));
fragmentTitles.put(NEW_DECK_FRAG, getString(R.string.new_deck_title));
fragmentTitles.put(NEW_FLASHCARD_FRAG, getString(R.string.new_flashcard_title));
}
@Override
public void restoreFragment(Bundle savedInstanceState) {
//Restore the fragment's instance
activeFragment = (DefaultFragment) getFragmentManager().getFragment(savedInstanceState, "activeFragment");
}
@Override
public void displayView(int position, Bundle fragmentBundle) {
FragmentManager fragmentManager = getFragmentManager(); // Get the fragmentManager for this activity
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (position) {
case CATEGORIES_FRAG:
activeFragment = new FragmentCategories(); // Set the ActiveFragment to our selected item on the list
clearBackStack(); // Clear the back stack to avoid back presses bugs
break;
case SETTINGS_FRAG:
break;
case NEW_CATEGORY_FRAG:
activeFragment = new FragmentNewCategory();
fragmentTransaction.addToBackStack(null); // null = name of the fragment on the stack.
break;
case NEW_DECK_FRAG:
activeFragment = new FragmentNewDeck();
fragmentTransaction.addToBackStack(null);
break;
case DECKS_FRAG:
activeFragment = new FragmentDecks();
fragmentTransaction.addToBackStack(null); // null = name of the fragment on the stack.
break;
case FLASHCARDS_FRAG:
activeFragment = new FragmentFlashCardsList();
fragmentTransaction.addToBackStack(null);
break;
case NEW_FLASHCARD_FRAG:
activeFragment = new FragmentNewFlashCard();
fragmentTransaction.addToBackStack(null);
break;
case FLASHCARDS_VIEWER:
activeFragment = new FragmentFlashCardViewer();
fragmentTransaction.addToBackStack(null);
break;
default:
break;
}
if(activeFragment != null) {
if(fragmentBundle != null) {
currentBundle = fragmentBundle;
activeFragment.setArguments(fragmentBundle);
}
fragmentTransaction.setCustomAnimations(R.animator.alpha_in, R.animator.alpha_out, // Animations for the fragment in...
R.animator.alpha_in, R.animator.alpha_out) // Animations for the fragment out...
.replace(R.id.frame_container, activeFragment, TAG_ACTIVE_FRAGMENT) // We then replace whatever is inside FrameLayout to our activeFragment
.commit(); // Commit the change
// update selected item and title
if(position >= 0) {
getDrawerList().setItemChecked(position, true); // We now set the item on the drawer that has been cliced as active
getDrawerList().setSelection(position); // Same concept as above...
setTitle(navMenuTitles[position]); // We not change the title of the Action Bar to match our fragment.
} else {
if(fragmentBundle == null) {
setTitle(fragmentTitles.get(position)); // We not change the title of the Action Bar to match our fragment.
} else {
setTitle(fragmentBundle.getString(MyDbHelper.TITLE));
}
}
} else {
Log.i(getLogTag(), "Error creating fragment"); // if the fragment does not create we Log an error.
}
}
/**
* Override this method to change the log tag string;
*
* @return
*/
@Override
public String getLogTag() {
return "MainActivity";
}
@Override
protected BaseAdapter getAdapter() {
return mNavDrawerAdapter;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getFragmentManager().putFragment(outState, "activeFragment", activeFragment);
}
}
编辑这是The DrawerLayoutActivity:
package gabilheri.com.flashcards;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import com.llamacorp.equate.R;
public abstract class DrawerLayoutActivity extends Activity {
private final String LOG_TAG = getLogTag();
/**
* Nav Drawer stuff
*/
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle, mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayout());
// Enabling action bar app and Icon , and behaving it as a toggle button.
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
/**
* Drawer Layout stuff
*/
mTitle = mDrawerTitle = getTitle();
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList.setOnItemClickListener(new DrawerListener());
init();
mDrawerList.setAdapter(getAdapter());
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_navigation_drawer, // Nav drawer Icon
R.string.app_name, // Nav drawer open - description for accessibility
R.string.app_name // Nav drawer close
) {
@Override
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if(savedInstanceState != null) {
restoreFragment(savedInstanceState);
} else if(savedInstanceState == null) {
displayView(0, null);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstance has occurred
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer
mDrawerToggle.onConfigurationChanged(newConfig);
}
/**
* Drawer listener.
*/
private class DrawerListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
/**
* A handler with a postDelayed is used so it only changes fragments once the drawer is
* already closed. This can be adjusted if the timing is not right.
* Similar behavior that most Google apps offers.
*/
mDrawerLayout.closeDrawer(mDrawerList);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
displayView(position, null);
}
}, 300);
}
}
/**
* Method to define what clicking on the drawer will do.
*
* @param position
* The position of the clicked item
* @param fragmentBundle
* A bundle in case something needs to be passed to a specific fragment
*/
public abstract void displayView(int position, Bundle fragmentBundle);
/**
*
* @param savedInstanceState
*/
public abstract void restoreFragment(Bundle savedInstanceState);
/**
* Any specific initializations should go here.
*/
public abstract void init();
/**
* Override this method to change the log tag string;
* @return
*/
public String getLogTag() {
return "DrawerActivity";
}
/**
* Override this method in case of need for a different list colors, etc..
* Should use same Id's to avoid confusion
* @return
* The Activity layout for this drawer activity
*/
private int getLayout() {
return R.layout.drawer_activity;
}
/**
* Getter for the drawer toggle
* @return
* The drawer toggle for this activity
*/
public ActionBarDrawerToggle getDrawerToggle() {
return mDrawerToggle;
}
/**
*
* @return
* The List used by the drawer
*/
public ListView getDrawerList() {
return mDrawerList;
}
/**
*
* @return
* The Drawer Layout used by this activity
*/
public DrawerLayout getDrawerLayout() {
return mDrawerLayout;
}
/**
*
* Method to be Overriden that will return an Adapter that extends Base Adapter
* The adapter will them be used by the Drawer Layout
*
* @return
*/
protected abstract BaseAdapter getAdapter();
/**
* Handy method to clear the back stack. We want to do this to avoid back stack bugs.
*/
public void clearBackStack() {
FragmentManager manager = getFragmentManager();
if (manager.getBackStackEntryCount() > 0) {
FragmentManager.BackStackEntry first = manager.getBackStackEntryAt(0);
manager.popBackStack(first.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
@Override
public void onBackPressed() {
// If the back stack is empty we let android handle the back button
if(getFragmentManager().getBackStackEntryCount() == 0) {
super.onBackPressed();
} else {
// Otherwise we remove it from the back stack and the framework will handle the
// fragment change for us :)
getFragmentManager().popBackStack();
getActionBar().setTitle(mTitle);
}
}
}
和build.grade:
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
applicationId "com.llamacorp.equate"
minSdkVersion 16
targetSdkVersion 19
versionCode 1 // The version Code is used by the Play Store. Every time an update is pushed to the play store the version code should be updated as well.
versionName "1.0" // Visible version name for the users on the Play Store
testApplicationId "com.llamacorp.equate.test"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// Will compile all the Libraries inside the 'libs' folder
compile 'com.android.support:support-v4:19.1.0'
compile 'com.android.support:support-v13:20.0.0'
// We will be using the support library for the Drawer layout
// Very good library to use cards
compile files('lib/achartengine.jar')
compile files('lib/bsh-core-2.0b4.jar')
compile 'com.github.gabrielemariotti.cards:library:1.9.1'
}
我知道他们已经发布了一些关于这个错误的帖子,但没有一个与这个案例有关我已经尝试了其他表格建议的建议,但没有一个对我有用,所以任何帮助都会很棒
提前致谢:)