0

我的应用程序包含带有文本的图像查看器,我尝试将图像保存到 SD,但是当我按下按钮时保存它会强制关闭。任何解决该问题的帮助将不胜感激。

图像寻呼机

  public class ImagePager extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra, stringArray );

    ViewPager myPager = (ViewPager) findViewById(R.id.myimagepager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);}

private int imageArra[] = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e};


private String[] stringArray = new String[] { "Image a", "Image b","Image c","Image d","Image e"}; 
}

图像页面适配器

  public class ImagePagerAdapter extends PagerAdapter {

Activity activity;
int imageArray[];
String[] stringArray;

public ImagePagerAdapter(Activity act, int[] imgArra, String[] stringArra) {
    imageArray = imgArra;
    activity = act;
    stringArray = stringArra;}

public int getCount() {
    return imageArray.length;}

public Object instantiateItem(View collection, final int position) {
    LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.custom_pager, null);   

    final ImageView im=(ImageView) layout.findViewById(R.id.myimage);               
    im.setImageResource(imageArray[position]);

    TextView txt=(TextView) layout.findViewById(R.id.image_text);
    txt.setText(stringArray[position]);
    ((ViewPager) collection).addView(layout, 0);

    Button button=(Button)layout.findViewById(R.id.button_save);

    button.setOnClickListener(new OnClickListener() {

  private Bitmap bm;
  private String PREFS_NAME;
  private Context mContext;
 public void onClick(View arg0) {
String root = Environment.getExternalStorageDirectory().toString();
   File myDir = new File(root + "/saved_images");
   if(!myDir.exists()){
       myDir.mkdirs();
       }
   bm = BitmapFactory.decodeResource( mContext.getResources(), imageArray[position]);
   im.setImageBitmap(bm);
     SharedPreferences savedNumber = mContext.getSharedPreferences(PREFS_NAME, 0); 
    int lastSavedNumber = savedNumber.getInt("lastsavednumber",0); 
    lastSavedNumber++; 
    String fname = "Image-"+lastSavedNumber+".png"; 
    File file = new File (myDir, fname); 
    if (file.exists ()) {file.delete (); 
    } 
    try { 
    FileOutputStream out = new FileOutputStream(file); 
    bm.compress(Bitmap.CompressFormat.PNG, 100, out);//Your Bitmap from the resouce 
    out.flush(); 
    out.close(); 
    }
     catch (Exception e) { 
    e.printStackTrace(); 
    }       
    SharedPreferences saveNumber = mContext.getApplicationContext().getSharedPreferences(PREFS_NAME, 0); 
    SharedPreferences.Editor editorset = saveNumber.edit(); 
    editorset.putInt("lastsavednumber",lastSavedNumber); 
    editorset.commit();
      Toast.makeText(mContext, "Image Saved", Toast.LENGTH_SHORT). show();}});

       return layout;   }

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
    return null;
}
}

日志猫:

   java.lang.NullPointerException
at com.test.demo.ImagePagerAdapter$1.onClick(ImagePagerAdapter.java:61)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

您永远不会将 mContext 设置为任何值,默认值为 null。将其分配给活动。

于 2013-07-19T01:02:05.570 回答