2016-07-31 147 views
0
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()); 
    } 
} 

}java.lang.RuntimeException:无法启动活动ComponentInfo。 java.lang.ArrayIndexOutOfBoundsException:length = 7;指数= 7

公共类MainActivity延伸活动{

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); 
    } 
} 

10月7日至31日:28:10.311:E/AndroidRuntime(6707):致命异常:主 07 -31 10:28:10.311:E/AndroidRuntime(6707): java.lang.RuntimeException:无法启动活动 ComponentInfo {com.viavilab.androiddrawing/com.viavilab.androiddrawing.MainActivity}: java.lang.ArrayIndexOutOfBounds例外:长度= 7; index = 7 07-31 10:28:10.311:E/AndroidRuntime(6707):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2204) 07-31 10:28:10.311:E/AndroidRuntime 6707):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2254) 07-31 10:28:10.311:E/AndroidRuntime(6707):at android.app.ActivityThread.access $ 600(ActivityThread.java :141)07-31 10:28:10.311:E/AndroidRuntime(6707):at 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)十月7日至31日:28:10.311: E/AndroidRuntime(6707):在 android.app.ActivityThread.main(ActivityThread.java:5069)07- 31 10:28:10.311:E/AndroidRuntime(6707):at java.lang.reflect.Method.invokeNative(Native Method)07-31 10:28:10.311:E/AndroidRuntime(6707):at java .lang.reflect.Method.invoke(Method.java:511)07-31 10:28:10.311: E/AndroidRuntime(6707):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit。 java:793) 07-31 10:28:10.311:E/AndroidRuntime(6707):at 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:length = 7; index = 7 07-31 10:28:10.311:E/AndroidRuntime(6707):at 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(仪表。 java:1092) 07-31 10:28:10.311:E/AndroidRuntime(6707):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 07-31 10:28:10.311:E/AndroidRuntime (6707):... 11多个

+0

的问题解决了吗?还有一个变量“我”的意图。两个同名的变量可能导致混乱。值得检查,我想。 –

+0

为什么总图像减1? –

+0

我确实购买了这样的代码,但现在开发人员没有回应。我不Java中的好:/ – samfun

回答

0
com.viavilab.androiddrawing.MainActivity.onCreate(MainActivity.java:50) 07-31 10:28:10.311: 

它说你的错误是在方法的第50行的onCreate和错误是你的指数是从数组中,检查你的代码,请。

+0

你能告诉我请,我在贴在MainActivity代码的问题,您能不能告诉我该怎么做完全,请? – samfun

+0

你能告诉我第50行中的代码吗?我不知道是否计算器可以显示行数 – berlloon

+0

我复制你的代码,我的编辑器,它显示50行是“否则,如果(位置= 2)” – berlloon

0

代替原来的代码中使用

for(int i=0; i<title.length -1 ;i++) 

尝试。但我需要检查array.lengtharray.length -1是如何工作的。 :)

我认为当涉及到indexlengtharray,array.length -1是解决方案。但something.lengthFile InputSteam或其他人。

并请理解我们indexarray这样的:

title [] = [ index 0 , index 1, index 2 , ... index (array.lenth -1) ] 

现在你的代码的index是7和你array.length只有7.如果是index 7价值? :)

+0

问题是代码正在工作,但是一旦我改变了数组和图片,现在一旦我打开它,它就会崩溃。我将发布image_arrays活动 – samfun

0

这条线路是好的,你是从阵列获得每一个项目:

for(int i=0; i<title.length;i++) { 

,问题就出现在这里:

List_Item item=new List_Item(description[i], title[i],Image_Arrays.icons[i]); 

阵列 “Image_Arrays” 越小则数组 “称号”那就抛出异常。您可以测试有长度:

Log.d("ArrayLength", "title: " + String.valueOf(title.length) + " icons: "+ String.valueOf(Image_Arrays.icons.length)) 
+1

谢谢大家的答案,这是迄今为止最好的android网站。我解决了这个问题。再次感谢你 – samfun

相关问题