选择图像后Android崩溃

时间:2014-11-20 21:42:46

标签: android

在我的Android应用程序中,我允许用户从库中选择一个图像。但是在选择图像后,应用程序崩溃了这个logcat

11-20 16:37:49.332: E/AndroidRuntime(18242): FATAL EXCEPTION: main
11-20 16:37:49.332: E/AndroidRuntime(18242): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.arin/activity.NavigationScreen}: java.lang.NullPointerException
11-20 16:37:49.332: E/AndroidRuntime(18242):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at android.app.ActivityThread.access$700(ActivityThread.java:143)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at android.os.Looper.loop(Looper.java:137)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at android.app.ActivityThread.main(ActivityThread.java:4950)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at java.lang.reflect.Method.invokeNative(Native Method)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at java.lang.reflect.Method.invoke(Method.java:511)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at dalvik.system.NativeStart.main(Native Method)
11-20 16:37:49.332: E/AndroidRuntime(18242): Caused by: java.lang.NullPointerException
11-20 16:37:49.332: E/AndroidRuntime(18242):    at activity.NavigationScreen.setUpCarousel(NavigationScreen.java:222)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at activity.NavigationScreen.onCreate(NavigationScreen.java:60)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at android.app.Activity.performCreate(Activity.java:5177)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
11-20 16:37:49.332: E/AndroidRuntime(18242):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
11-20 16:37:49.332: E/AndroidRuntime(18242):    ... 11 more

这就是我打开意图的方式:

private void recognition() {
    if (subRootCategory != null) {
        Intent pickIntent = new Intent();
        pickIntent.setType("image/*");
        pickIntent.setAction(Intent.ACTION_GET_CONTENT);
        Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Intent chooserIntent = Intent.createChooser(pickIntent, getString(R.string.select_or_take));
        chooserIntent.putExtra
        (
          Intent.EXTRA_INITIAL_INTENTS, 
          new Intent[] { takePhotoIntent }
        );
        startActivityForResult(chooserIntent, MyIntent.RECOGNITION);
    } else {
        Popup.ShowErrorMessage(this, R.string.select_one);
    }
}

然后是结果活动代码

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        // this is for uploading an image
        if (requestCode == MyIntent.SELECT_PICTURE) { 
            Bitmap bitmap = MyImage.GetBitmapFromPath(this, data.getData(), Fish.UPLOAD_WIDTH, Fish.UPLOAD_HEIGHT);

            // store locally if offline mode
            if (ArinContext.ifOfflineMode()) {
                FishImage.getCommentAndSaveTemp(this, bitmap, CategoryImage.tablename, selectedCategory);
            } else {
                new Async_up_image(this, bitmap, selectedCategory);
            }
        } 

        // this is for picking an image for image recognition
        else if (requestCode == MyIntent.RECOGNITION) { 
            Intent myIntent = new Intent(this, SpeciesScreen.class);
            myIntent.putExtra("type", 2);
            myIntent.putExtra("path", Common.getRealPathFromURI(this, data.getData()));
            startActivity(myIntent);
        }
    }
}

有谁知道这里有什么不对吗?

由于


修改

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

    // activates the action bar
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    // make sure arin pics don't show up in gallery apps
    setUpNoMedia();

    // set up the carousel (fish tree)
    setUpCarousel();

    // shows any message like if you are banned
    showMessage();
}

...

private void setUpCarousel() {
    LocalData localdata = ArinContext.getLocaldata();
    String category_data = localdata.getCategory_data();
    String species_data = localdata.getSpecies_data();

    // show 0 matches initially
    updateCount();

    // generate the treeview
    TreeMaker treemaker = new TreeMaker();
    root = treemaker.getCategoryRoot(this, category_data, species_data);

    if (root != null) {
        downloadImages(root);
    } else {
        Popup.ShowErrorMessage(this, R.string.corrupted);
    }
}

第222行是String category_data = localdata.getCategory_data();函数中的setUpCarousel

0 个答案:

没有答案