检查默认情况下是否打开活动或来自Intent

时间:2015-03-20 13:38:28

标签: android android-intent

您好我是Android开发的新手。我试图检查我的活动是否默认打开,或者它是否来自一个意图,以及是否从中获取额外内容。

以下是我正在使用的代码:

public class MainActivity extends Activity  {

    protected int firstImageIndex;
    protected String albumName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String comingFrom;
        Intent i = getIntent();
        if (i != null){
            comingFrom = i.getExtras().getString("comingFrom");
            if (comingFrom.equals("gridView")) {
                albumName = (String) i.getExtras().get("albumName");
                firstImageIndex = i.getExtras().getInt("firstImageIndex");
                ImageView iv = (ImageView) findViewById(R.id.imageView);
            }
        }
        else{
            albumName = "All Photos";
            firstImageIndex = 0;
        }

    }



    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

    public void openGalleryBucketList(View view) {
        Intent openGalleryIntent = new  Intent(this, GalleryBucketList.class);

        openGalleryIntent.putExtra("sender","First");
        startActivity(openGalleryIntent);
    }

    public void goToLastPhoto(View view) {
        Intent goToLastImageIntent = new Intent(this, LastImageActivity.class);
        goToLastImageIntent.putExtra("firstImageIndex",firstImageIndex);
        goToLastImageIntent.putExtra("albumName", albumName);
    }
}

我遇到的问题是应用程序在行if(i != null)中的空指针异常崩溃

任何有关为何以及如何解决此问题的帮助将不胜感激! 感谢

编辑:这是读数(也许我没有正确阅读: enter image description here

enter image description here

0 个答案:

没有答案