java.lang.RuntimeException:无法启动活动 ComponentInfo。 java.lang.ArrayIndexOutOfBoundsException:长度=7;指数=7
Posted
技术标签:
【中文标题】java.lang.RuntimeException:无法启动活动 ComponentInfo。 java.lang.ArrayIndexOutOfBoundsException:长度=7;指数=7【英文标题】:java.lang.RuntimeException: Unable to start activity ComponentInfo. java.lang.ArrayIndexOutOfBoundsException: length=7; index=7 【发布时间】:2016-12-05 10:35:42 【问题描述】:public class Drawing_Activity extends Activity
ViewPager viewpager;
TextView txtstep;
ImageView imageView;
Button btnback,btnnext;
RelativeLayout reldraw;
ImageView imgcolorpick,imgpencil,imgstroke,imgeraser,imgclearr,imgsave;
private static int TOTAL_IMAGES;
private int currentPosition = 0;
protected Dialog dialog;
protected View layout;
protected int progress;
protected float stroke = 6;
private String fileName;
PaintView pv;
int count=0;
String get1;
private AdView mAdView;
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.drawing_activity);
mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(new AdRequest.Builder().build());
btnback=(Button)findViewById(R.id.button_prev);
btnnext=(Button)findViewById(R.id.button_next);
reldraw=(RelativeLayout)findViewById(R.id.rell);
viewpager=(ViewPager)findViewById(R.id.view_pagercake);
txtstep=(TextView)findViewById(R.id.textView_stepsize);
Intent i=getIntent();
get1=i.getExtras().getString("Image_Array");
//Log.e("get1---", get1);
imgcolorpick=(ImageView)findViewById(R.id.imageView2_color);
imgstroke=(ImageView)findViewById(R.id.imageView4_stroke);
imgclearr=(ImageView)findViewById(R.id.imageView5_clear);
imgeraser=(ImageView)findViewById(R.id.imageView3_eraser);
imgpencil=(ImageView)findViewById(R.id.imageView1_pencil);
imgsave=(ImageView)findViewById(R.id.imageView1_save);
imgpencil.setImageResource(R.drawable.brush_active_btn); //click of pencil
this.pv = new PaintView(this);
this.pv.togglePencil(true);
reldraw .addView(pv);
imgcolorpick.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
showColorPickerDialogDemo();
//Toast.makeText(getApplicationContext(), "hiiii", Toast.LENGTH_SHORT).show();
);
imgstroke.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
strokeDialog();
);
imgclearr.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Drawing_Activity.this.pv.clear();
);
imgeraser.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Drawing_Activity.this.pv.togglePencil(false);
imgeraser.setImageResource(R.drawable.erase_active_btn);
imgpencil.setImageResource(R.drawable.brush_btn);
);
imgpencil.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Drawing_Activity.this.pv.togglePencil(true);
imgpencil.setImageResource(R.drawable.brush_active_btn);
imgeraser.setImageResource(R.drawable.erase_btn);
);
imgsave.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Drawing_Activity.this.save();
);
viewpager.setOnPageChangeListener(new OnPageChangeListener()
@Override
public void onPageSelected(int arg0)
// TODO Auto-generated method stub
txtstep.setText(String.valueOf(arg0)+"/"+TOTAL_IMAGES);
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
// TODO Auto-generated method stub
@Override
public void onPageScrollStateChanged(int arg0)
// TODO Auto-generated method stub
);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewpager.setAdapter(adapter);
if(get1.equals("image_car"))
TOTAL_IMAGES = (Image_Arrays.image_car.length - 1);
else if(get1.equals("image_house"))
TOTAL_IMAGES = (Image_Arrays.image_house.length - 1);
else if(get1.equals("image_horse"))
TOTAL_IMAGES = (Image_Arrays.image_horse.length - 1);
else if(get1.equals("image_bird"))
TOTAL_IMAGES = (Image_Arrays.image_bird.length - 1);
else if(get1.equals("image_elephant"))
TOTAL_IMAGES = (Image_Arrays.image_elephant.length - 1);
else if(get1.equals("image_shoes"))
TOTAL_IMAGES = (Image_Arrays.image_shoes.length - 1);
else if(get1.equals("image_bottle"))
TOTAL_IMAGES = (Image_Arrays.image_bottle.length - 1);
else
TOTAL_IMAGES = (Image_Arrays.image_car.length - 1);
txtstep.setText(String.valueOf(0)+"/"+TOTAL_IMAGES);
btnback.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
currentPosition = viewpager.getCurrentItem();
int positionToMoveTo = currentPosition;
positionToMoveTo--;
if (positionToMoveTo < 0)
positionToMoveTo = TOTAL_IMAGES;
viewpager.setCurrentItem(positionToMoveTo);
);
btnnext.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
currentPosition = viewpager.getCurrentItem();
int positionToMoveTo = currentPosition;
positionToMoveTo++;
if (currentPosition == TOTAL_IMAGES)
positionToMoveTo = 0;
viewpager.setCurrentItem(positionToMoveTo);
);
protected int getItem(int i)
// TODO Auto-generated method stub
return 0;
private class ImagePagerAdapter extends PagerAdapter
@Override
public int getCount()
if(get1.equals("image_car"))
return Image_Arrays.image_car.length;
else if(get1.equals("image_house"))
return Image_Arrays.image_house.length;
else if(get1.equals("image_horse"))
return Image_Arrays.image_horse.length;
else if(get1.equals("image_bird"))
return Image_Arrays.image_bird.length;
else if(get1.equals("image_elephant"))
return Image_Arrays.image_elephant.length;
else if(get1.equals("image_shoes"))
return Image_Arrays.image_shoes.length;
else if(get1.equals("image_bottle"))
return Image_Arrays.image_bottle.length;
else
return Image_Arrays.image_car.length;
@Override
public boolean isViewFromObject(View view, Object object)
return view == ((ImageView) object);
@Override
public Object instantiateItem(ViewGroup container, int position)
Context context = Drawing_Activity.this;
imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
if(get1.equals("image_car"))
imageView.setImageResource(Image_Arrays.image_car[position]);
else if(get1.equals("image_house"))
imageView.setImageResource(Image_Arrays.image_house[position]);
else if(get1.equals("image_horse"))
imageView.setImageResource(Image_Arrays.image_horse[position]);
else if(get1.equals("image_bird"))
imageView.setImageResource(Image_Arrays.image_bird[position]);
else if(get1.equals("image_elephant"))
imageView.setImageResource(Image_Arrays.image_elephant[position]);
else if(get1.equals("image_shoes"))
imageView.setImageResource(Image_Arrays.image_shoes[position]);
else if(get1.equals("image_bottle"))
imageView.setImageResource(Image_Arrays.image_bottle[position]);
else
imageView.setImageResource(Image_Arrays.image_car[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
@Override
public void destroyItem(ViewGroup container, int position, Object object)
((ViewPager) container).removeView((ImageView) object);
private void showColorPickerDialogDemo()
int initialColor = Color.WHITE;
ColorPickerDialog colorPickerDialog = new ColorPickerDialog(this, initialColor, new OnColorSelectedListener()
@Override
public void onColorSelected(int color)
Drawing_Activity.this.pv.setColor(color);
);
colorPickerDialog.show();
private class PaintView extends View
private Paint paint;
private Bitmap bmp;
private Paint bmpPaint;
private Canvas canvas;
private Context context;
private float mX, mY;
private Path path;
private static final float TOUCH_TOLERANCE = 0.8f;
private int colour;
private Bitmap bgImage; // image that gets loaded
protected Boolean pencil;
private PaintView(Context c)
super(c);
setDrawingCacheEnabled(true); // to save images
this.context = c;
this.colour = Color.BLACK;
this.path = new Path();
this.bmpPaint = new Paint();
this.paint = new Paint();
this.paint.setAntiAlias(true);
this.paint.setDither(true);
this.paint.setColor(this.colour);
this.paint.setStyle(Paint.Style.STROKE);
this.paint.setStrokeJoin(Paint.Join.ROUND);
this.paint.setStrokeCap(Paint.Cap.ROUND);
this.paint.setStrokeWidth(3);
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
super.onSizeChanged(w, h, oldw, oldh);
this.bgImage = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
this.bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
this.canvas = new Canvas(this.bmp);
bgImage = BitmapFactory.decodeResource(getResources(),
R.drawable.inner_bg).copy(Bitmap.Config.ARGB_8888, true);
private void touchStart(float x, float y)
this.path.reset();
this.path.moveTo(x, y);
this.mX = x;
this.mY = y;
private void touchUp()
this.path.lineTo(mX, mY);
// commit the path to our offscreen
this.canvas.drawPath(this.path, paint);
// kill this so we don't double draw
this.path.reset();
private void touchMove(float x, float y)
float dx = Math.abs(x - this.mX);
float dy = Math.abs(y - this.mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
// draws a quadratic curve
this.path.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
@Override
public boolean onTouchEvent(MotionEvent e)
float x = e.getX();
float y = e.getY();
switch (e.getAction())
case MotionEvent.ACTION_DOWN:
this.touchStart(x, y);
this.touchMove(x + 0.8f, y + 0.8f);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
this.touchMove(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
this.touchUp();
invalidate();
break;
return true;
// Called on invalidate();
@Override
protected void onDraw (Canvas canvas)
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(this.bgImage, 0, 0, this.bmpPaint);
canvas.drawBitmap(this.bmp, 0, 0, this.bmpPaint);
canvas.drawPath(this.path, this.paint);
/*
* Menu called methods
*/
protected void togglePencil(Boolean b)
if (b) // set pencil
paint.setXfermode(null);
this.pencil = true;
else // set eraser
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
this.pencil = false;
//Draw_Dog.this.setTitle();
public void setColor(int c)
this.paint.setColor(c);
this.colour = c;
protected int getColor()
return this.colour;
protected void clear()
this.path = new Path(); // empty path
this.canvas.drawColor(Color.WHITE);
if (this.bgImage != null)
this.canvas.drawBitmap(this.bgImage, 0, 0, null);
this.invalidate();
public void strokeDialog()
this.dialog = new Dialog(this);
this.dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(LAYOUT_INFLATER_SERVICE);
this.layout = inflater.inflate(R.layout.stroke_dialog,
(ViewGroup) findViewById(R.id.dialog_root_element));
SeekBar dialogSeekBar = (SeekBar) layout
.findViewById(R.id.dialog_seekbar);
dialogSeekBar.setThumbOffset(convertDipToPixels(9.5f));
dialogSeekBar.setProgress((int) this.stroke * 2);
this.setTextView(this.layout, String.valueOf(Math.round(this.stroke)));
dialogSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
@Override
public void onStopTrackingTouch(SeekBar seekBar)
// herp
@Override
public void onStartTrackingTouch(SeekBar seekBar)
// derp
@Override
public void onProgressChanged(SeekBar seekBark, int progress,
boolean fromUser)
Drawing_Activity.this.progress = progress / 2;
Drawing_Activity.this
.setTextView(Drawing_Activity.this.layout, "" + Drawing_Activity.this.progress);
Button b = (Button) Drawing_Activity.this.layout
.findViewById(R.id.dialog_button);
b.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
Drawing_Activity.this.stroke = Drawing_Activity.this.progress;
Drawing_Activity.this.pv.paint.setStrokeWidth(Drawing_Activity.this.stroke);
Drawing_Activity.this.dialog.dismiss();
);
);
dialog.setContentView(layout);
dialog.show();
protected void setTextView(View layout, String s)
TextView text = (TextView) layout.findViewById(R.id.stroke_text);
text.setText(s);
private int convertDipToPixels(float dip)
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float density = metrics.density;
return (int) (dip * density);
@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;
public boolean onOptionsItemSelected(MenuItem item)
switch (item.getItemId())
// Toast.makeText(this, "Save", Toast.LENGTH_SHORT).show();
case R.id.Share:
reldraw.setDrawingCacheEnabled(true);
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path,
"android/data/com.viavilab.androiddrawing;/cache/share_cache.jpg");
file.getParentFile().mkdirs();
try
file.createNewFile();
catch (Exception e)
//Log.e("draw_save", e.toString());
try
fOut = new FileOutputStream(file);
catch (Exception e)
//Log.e("draw_save1", e.toString());
if (this.reldraw.getDrawingCache() == null)
//Log.e("lal", "tis null");
this.reldraw.getDrawingCache()
.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
try
fOut.flush();
fOut.close();
catch (IOException e)
//Log.e("draw_save1", e.toString());
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getAbsolutePath()));
startActivity(Intent.createChooser(share, "Share Image"));
return true;
case R.id.RateApp:
final String appName = getPackageName();
try
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + appName)));
catch (android.content.ActivityNotFoundException anfe)
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id="
+ appName)));
return true;
case R.id.About:
Intent intentabout=new Intent(getApplicationContext(),AboutActivity.class);
startActivity(intentabout);
return(super.onOptionsItemSelected(item));
public void save() // called on save menu
reldraw.setDrawingCacheEnabled(true);
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path,
getString(R.string.app_name)+"/"+"AD_"+System.currentTimeMillis()+".jpg");
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
file.getParentFile().mkdirs();
try
file.createNewFile();
catch (Exception e)
Log.e("draw_save", e.toString());
try
fOut = new FileOutputStream(file);
catch (Exception e)
Log.e("draw_save1", e.toString());
if (this.reldraw.getDrawingCache() == null)
Log.e("lal", "tis null");
this.reldraw.getDrawingCache()
.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
try
fOut.flush();
fOut.close();
catch (IOException e)
Log.e("draw_save1", e.toString());
公共类 MainActivity 扩展 Activity
ListView lsvmain;
ArrayList<List_Item> ListArray = new ArrayList<List_Item>();
List_Adapter listadapter;
String[] title,description;
private InterstitialAd mInterstitial;
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState)
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title= getResources().getStringArray(R.array.names);
description= getResources().getStringArray(R.array.descriptions);
lsvmain=(ListView)findViewById(R.id.listView_main);
for(int i=0; i<title.length;i++)
List_Item item=new List_Item(description[i], title[i],Image_Arrays.icons[i]);
ListArray.add(item);
listadapter = new List_Adapter(this, R.layout.list_item,ListArray);
lsvmain.setAdapter(listadapter);
lsvmain.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3)
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), ""+position, Toast.LENGTH_SHORT).show();
if(position==0)
Intent intentnew=new Intent(getApplicationContext(),Drawing_Activity.class);
intentnew.putExtra("Image_Array", "image_car");
startActivity(intentnew);
else if(position==1)
Intent intentnew=new Intent(getApplicationContext(),Drawing_Activity.class);
intentnew.putExtra("Image_Array", "image_house");
startActivity(intentnew);
else if(position==2)
Intent intentnew=new Intent(getApplicationContext(),Drawing_Activity.class);
intentnew.putExtra("Image_Array", "image_horse");
startActivity(intentnew);
else if(position==3)
Intent intentnew=new Intent(getApplicationContext(),Drawing_Activity.class);
intentnew.putExtra("Image_Array", "image_bird");
startActivity(intentnew);
else if(position==4)
Intent intentnew=new Intent(getApplicationContext(),Drawing_Activity.class);
intentnew.putExtra("Image_Array", "image_elephant");
startActivity(intentnew);
else if(position==5)
Intent intentnew=new Intent(getApplicationContext(),Drawing_Activity.class);
intentnew.putExtra("Image_Array", "image_shoes");
startActivity(intentnew);
else if(position==6)
Intent intentnew=new Intent(getApplicationContext(),Drawing_Activity.class);
intentnew.putExtra("Image_Array", "image_bottle");
startActivity(intentnew);
);
@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 onKeyDown(int keyCode, KeyEvent event)
if (keyCode == KeyEvent.KEYCODE_BACK)
// Toast.makeText(appContext, "BAck", Toast.LENGTH_LONG).show();
AlertDialog.Builder alert = new AlertDialog.Builder(
MainActivity.this);
alert.setTitle(getString(R.string.app_name));
alert.setIcon(R.drawable.app_icon);
alert.setMessage("Are You Sure You Want To Quit?");
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog,
int whichButton)
if (mInterstitial.isLoaded())
mInterstitial.show();
finish();
);
alert.setNegativeButton("Rate App",
new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int which)
final String appName = getPackageName();
try
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id="
+ appName)));
catch (android.content.ActivityNotFoundException anfe)
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id="
+ appName)));
);
alert.show();
return true;
return super.onKeyDown(keyCode, event);
07-31 10:28:10.311: E/AndroidRuntime(6707): 致命异常: main 07-31 10:28:10.311: E/AndroidRuntime(6707): java.lang.RuntimeException:无法启动活动 组件信息com.viavilab.androiddrawing/com.viavilab.androiddrawing.MainActivity: java.lang.ArrayIndexOutOfBoundsException:长度=7;指数=7 07-31 10:28:10.311:E/AndroidRuntime(6707):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2204) 07-31 10:28:10.311: E/AndroidRuntime(6707): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2254) 07-31 10:28:10.311: E/AndroidRuntime(6707): 在 android.app.ActivityThread.access$600(ActivityThread.java:141) 07-31 10:28:10.311:E/AndroidRuntime(6707):在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 07-31 10:28:10.311: E/AndroidRuntime(6707): 在 android.os.Handler.dispatchMessage(Handler.java:99) 07-31 10:28:10.311:E/AndroidRuntime(6707):在 android.os.Looper.loop(Looper.java:137) 07-31 10:28:10.311: E/AndroidRuntime(6707):在 android.app.ActivityThread.main(ActivityThread.java:5069) 07-31 10:28:10.311:E/AndroidRuntime(6707):在 java.lang.reflect.Method.invokeNative(Native Method) 07-31 10:28:10.311:E/AndroidRuntime(6707):在 java.lang.reflect.Method.invoke(Method.java:511) 07-31 10:28:10.311: E/AndroidRuntime(6707):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 07-31 10:28:10.311: E/AndroidRuntime(6707): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-31 10:28:10.311:E/AndroidRuntime(6707):在 dalvik.system.NativeStart.main(本机方法)07-31 10:28:10.311: E/AndroidRuntime(6707):由以下原因引起: java.lang.ArrayIndexOutOfBoundsException:长度=7;指数=7 07-31 10:28:10.311:E/AndroidRuntime(6707):在 com.viavilab.androiddrawing.MainActivity.onCreate(MainActivity.java:50) 07-31 10:28:10.311: E/AndroidRuntime(6707): 在 android.app.Activity.performCreate(Activity.java:5104) 07-31 10:28:10.311:E/AndroidRuntime(6707):在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1092) 07-31 10:28:10.311: E/AndroidRuntime(6707): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 07-31 10:28:10.311: E/AndroidRuntime(6707): ... 11 更多
【问题讨论】:
问题解决了吗?还有一个意图变量“i”。两个具有相同名称的变量可能会导致混乱。我认为值得检查。 为什么总图片减1? 我确实像这样购买了代码,但现在开发人员没有响应。而且我在java中不是那么好:/ 【参考方案1】:com.viavilab.androiddrawing.MainActivity.onCreate(MainActivity.java:50) 07-31 10:28:10.311:
它说您的错误在方法 onCreate 的第 50 行,错误是您的索引超出了数组,请检查您的代码。
【讨论】:
你能告诉我吗,在我发布mainactivity代码的问题中,你能告诉我我应该做什么吗?? 你能告诉我正好第 50 行的代码吗?不知道***能不能显示行号 我将您的代码复制到我的编辑器,它显示第 50 行是“else if(position = 2)” List_Item item=new List_Item(description[i], title[i],Image_Arrays.icons[i]); 在我的编辑器中显示第 50 行是: List_Item item=new List_Item(description[i], title[i],Image_Arrays.icons[i]);【参考方案2】:尝试使用
for(int i=0; i<title.length -1 ;i++)
代替您的原始代码。但我需要检查array.length
和array.length -1
是如何工作的。 :)
我认为当涉及到index
和array
的length
时,array.length -1
是解决方案。但是something.length
File InputSteam
或其他类似的。
请理解我们index
和array
是这样的:
title [] = [ index 0 , index 1, index 2 , ... index ( array.lenth -1) ]
现在您的代码 index
是 7,而您的 array.length
只有 7。index 7
的值在哪里? :)
【讨论】:
问题是代码可以正常工作,但是一旦我更改了数组和图片,现在一旦我打开它,它就会崩溃。我将发布 image_arrays 活动【参考方案3】:这一行没问题,你正在从数组中获取每一项:
for(int i=0; i<title.length;i++)
问题来了:
List_Item item=new List_Item(description[i], title[i],Image_Arrays.icons[i]);
数组“Image_Arrays”比数组“title”小,这会引发异常。你可以在那里测试长度:
Log.d("ArrayLength", "title: " + String.valueOf(title.length) + " icons: "+ String.valueOf(Image_Arrays.icons.length))
【讨论】:
谢谢大家的回答,这是迄今为止最好的安卓网站。我解决了这个问题。再次感谢您以上是关于java.lang.RuntimeException:无法启动活动 ComponentInfo。 java.lang.ArrayIndexOutOfBoundsException:长度=7;指数=7的主要内容,如果未能解决你的问题,请参考以下文章