您好我创建了一个Android应用程序并将其上传到Play商店,在该应用程序中我使用导航抽屉从一个活动导航到另一个活动。
我的应用程序工作正常,最多没有具有1 GB RAM的移动设备和最新的操作系统,如KitKat或Lollipop,但在512 MB RAM移动设备和Ice_Cream_Sandwich版本中应用程序在启动后关闭,说“不幸”该应用已停止“。只有少数手机出现此问题(我根据Play商店中的用户评论说这个)。
我的最低和最高SDK版本是
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
我没有将appcompat v7添加为库,如果我添加这个,我所有的Java类文件都显示错误。
现在我需要的是,我需要知道崩溃发生的原因以及如何解决这个问题。
我已经在SO中进行了搜索,但我无法找到我需要的正确解决方案。
我进入游戏商店的崩溃报告是
java.lang.NullPointerException
in android.graphics.drawable.InsetDrawable.isStateful
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.imaginet.everwinmatriculation/com.imaginet.everwinmatriculation.MainActivity}:
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:177)
at android.app.ActivityThread.main(ActivityThread.java:4947)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.graphics.drawable.InsetDrawable.isStateful(InsetDrawable.java:208)
at android.widget.ImageView.updateDrawable(ImageView.java:697)
at android.widget.ImageView.setImageDrawable(ImageView.java:410)
at android.support.v4.app.ActionBarDrawerToggleHoneycomb.setActionBarUpIndicator
(ActionBarDrawerToggleHoneycomb.java:62)at
android.support.v4.app.ActionBarDrawerToggle$ActionBarDrawerToggleImplHC
.setActionBarUpIndicator(ActionBarDrawerToggle.java:134)
at android.support.v4.app.ActionBarDrawerToggle.setActionBarUpIndicator
(ActionBarDrawerToggle.java:400)
at android.support.v4.app.ActionBarDrawerToggle.syncState(ActionBarDrawerToggle.java:258)
at com.imaginet.everwinmatriculation.MainActivity.onPostCreate(MainActivity.java:291)
at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1157)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2104)
... 11 more
我的MainActivity是......
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
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;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//From this to - For Internet Connectivity Checking
ConnectivityManager cn=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nf=cn.getActiveNetworkInfo();
if(nf != null && nf.isConnected()==true )
{
Toast.makeText(this, "You Have Active Internet Connection", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "Network Not Available", Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You Don't Have Active Internet Connection, Click Ok To Exit")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
//Upto this - For Internet Connectivity Checking
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// About Us
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Principal Speech
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(3, -1)));
// Circulars
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(4, -1),
false, "22"));
// Internal Events
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(6, -1)));
//External Events
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(6, -1)));
// Academic Calendar
navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));
// Admissions
navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));
// Competition
navDrawerItems.add(new NavDrawerItem(navMenuTitles[8], navMenuIcons.getResourceId(8, -1)));
// Class Work
navDrawerItems.add(new NavDrawerItem(navMenuTitles[9], navMenuIcons.getResourceId(9, -1)));
// Home Work
navDrawerItems.add(new NavDrawerItem(navMenuTitles[10], navMenuIcons.getResourceId(10, -1)));
// Training & Test
navDrawerItems.add(new NavDrawerItem(navMenuTitles[11], navMenuIcons.getResourceId(11, -1)));
// Examination
navDrawerItems.add(new NavDrawerItem(navMenuTitles[12], navMenuIcons.getResourceId(12, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.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.ic_drawer, //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);
}
//Following one line is added for onBackPressed Method
mDrawerLayout.setFocusableInTouchMode(false);
}
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, 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(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new AboutUsFragment();
break;
case 2:
fragment = new PrincipalSpeechFragment();
break;
case 3:
fragment = new CircularsFragment();
break;
case 4:
fragment = new EventsFragment();
break;
case 5:
fragment = new ExternalEventsFragment();
break;
case 6:
fragment = new AC_CalenderFragment();
break;
case 7:
fragment = new AdmissionsFragment();
break;
case 8:
fragment = new CompetitionFragment();
break;
case 9:
fragment = new ClassWorkFragment();
break;
case 10:
fragment = new HomeWorkFragment();
break;
case 11:
fragment = new TrainingTestFragment();
break;
case 12:
fragment = new ExaminationFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
@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
if (mDrawerLayout != null) {
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
//super.onBackPressed();
//Upcmoning line is for confirm exiting
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainActivity.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
答案 0 :(得分:-1)
我相信您的问题的解决方案是:
只需将此行添加到清单文件中的应用程序:
android:largeHeap="true"
所以你的Manifest文件应该应该是这样的:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>