我的应用程序之前已经运行但现在应用程序无法正常启动。当应用程序启动到homebck.xml
时(我将在下面包含源代码),它会自动崩溃,而在Toast中它会显示crash_toast_text
中的strings.xml
。
此外,它不仅仅是一个Toast,也出现了一个共享托盘Ex。
每次都会弹出。下面我发布了我认为必要的文件来帮助我!
错误日志:
06-03 18:27:11.420 9762-9762/com.Celebration E/ACRA﹕ ACRA caught a RuntimeException for com.Celebration
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Celebration/com.abode.activity.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=10; index=10
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=10; index=10
at com.abode.activity.MainActivity.onCreate(MainActivity.java:155)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
由于30000字符限制,只有一小段MainActivity.java:
public class MainActivity extends ActionBarActivity
{
public static File path = new File(Environment.getExternalStorageDirectory() + "");
public static Item[] fileList;
public static Boolean firstLvl = true;
public static ListAdapter adapter;
private final int FileUpload = 100;
public ImageLoader imageLoader;
public static final String URL = "http://celebration.anazro.com/appData/images/celebration.jpg";
public static final String URL1 = "http://celebration.anazro.com/appData/images/celebration2.jpg";
public static final String URL2 = "http://celebration.anazro.com/appData/images/celebration3.jpg";
public static final String URL3 = "http://celebration.anazro.com/appData/images/celebration4.jpg";
public static final String URL4 = "http://celebration.anazro.com/appData/images/celebration5.jpg";
// public static final String URL5 = "";
// public static final String URL6 = "";
// public static final String URL7 = "";
public static String chosenFile;
public static ArrayList<String> str = new ArrayList<String>();
public static String ss;
public AboutPopup aboutPopup;
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;
//old code
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter navAdapter;
LayoutInflater inflater;
private View viewForPopup;
public AlarmManager alarm;
//experiment code
public static LiveSdkSampleApplication myContextManager;
public static WeakReference wrActivity = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_main);
wrActivity = new WeakReference<MainActivity>(this);
Globals.application = getApplication();
myContextManager = ((LiveSdkSampleApplication) getApplicationContext());
myContextManager.setContext(this);
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
viewForPopup = (View)findViewById(R.id.view);
alarm = (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[8], navMenuIcons.getResourceId(8, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[9], navMenuIcons.getResourceId(9, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[10], navMenuIcons.getResourceId(10, -1)));
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
navAdapter = new NavDrawerListAdapter(getApplicationContext(),navDrawerItems);
mDrawerList.setAdapter(navAdapter);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setLogo(R.drawable.logo);
// getSupportActionBar().setDisplayUseLogoEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.side_bar, R.string.app_name, R.string.app_name) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().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
selectItem(0);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
//A method found in stackOverflow, don't remember the author, to determine the right screen orientation independently of the phone or tablet device
@Override
public void onResume() {
super.onResume();
getSupportFragmentManager()
.beginTransaction()
.detach(fragment)
.attach(fragment)
.commit();
}
@Override
public void onDestroy(){
super.onDestroy();
}
@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
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
/* int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}*/
switch (item.getItemId()) {
case android.R.id.home: {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
break;
}
case R.id.admin:{
AdminPopup adminPopup = new AdminPopup(this);
adminPopup.showAtLocation(viewForPopup,0,0);
break;
}
}
return super.onOptionsItemSelected(item);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
selectItem(position);
}
}
这是幻灯片的XML代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/homefragment"
android:background="@color/white">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1">
<ImageView
android:id="@+id/image_information"
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="@drawable/ic_information"
android:layout_weight="1" />
<ImageView
android:id="@+id/image_save"
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="@drawable/ic_save"
android:layout_weight="1"
/>
<ImageView
android:id="@+id/image_wallpaper"
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="@drawable/ic_share"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
这将是最后一个幻灯片
的Java文件public class SlideShow extends Fragment {
// Bitmap bitmap=memoryCache.get(url);
public static final String URL =
"http://api.androidhive.info/json/movies/2.jpg";
ViewPager m_viewPager;
NetworkImageAdapter m_adapter;
ImageAdapter m_ImageAdapter;
ArrayList<NetworkImageView> m_NetworkImageViews;
ArrayList<ImageView> m_ImageView;
ImageView imageView;
Context context;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
context = getActivity();
View rootView = inflater.inflate(R.layout.slideshow, container, false);
m_viewPager = (ViewPager) rootView.findViewById(R.id.slide_view_pager);
m_NetworkImageViews = new ArrayList<NetworkImageView>();
m_NetworkImageViews.clear();
NetworkImageView m_newworkImageView = new NetworkImageView(context);
m_newworkImageView.setImageUrl(URL, imageLoader);
m_NetworkImageViews.add(m_newworkImageView);
m_adapter = new NetworkImageAdapter(m_NetworkImageViews);
//m_ImageView = new ArrayList<ImageView>();
//imageView = new ImageView(context);
/* GetXMLTask task = new GetXMLTask();
// Execute the task
task.execute(new String[] { URL });
*/
return rootView;
}
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
} // Autho
private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap map = null;
for (String url : urls) {
map = downloadImage(url);
}
return map;
}
// Sets the Bitmap returned by doInBackground
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
m_ImageView.add(imageView);
m_ImageAdapter = new ImageAdapter(m_ImageView);
m_viewPager.setAdapter(m_ImageAdapter);
}
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.
decodeStream(stream, null, bmOptions);
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
}
}
答案 0 :(得分:2)
删除此行:
navDrawerItems.add(new NavDrawerItem(navMenuTitles[10], navMenuIcons.getResourceId(10, -1)));
考虑用for循环替换那个几乎相同的调用块:
for (int i = 0; i < 10; ++i) {
navDrawerItems.add(new NavDrawerItem(navMenuTitles[i], navMenuIcons.getResourceId(i, -1)));
}
答案 1 :(得分:1)
@Override
public void onResume() {
super.onResume();
getSupportFragmentManager()
.beginTransaction()
.detach(fragment)
.attach(fragment)
.commit();
}
必须在代码后更改代码:
new Handler().post(new Runnable() {
public void run() {
getSupportFragmentManager()
.beginTransaction()
.detach(fragment)
.attach(fragment)
.commit();
}
});