Java Android Studio应用程序开发 - NullPointerException。我的应用程序在加载第二个Activity时关闭

时间:2015-01-12 21:59:40

标签: java android android-intent nullpointerexception android-studio

我得到了一个致命的异常:我的Android应用程序(AA)上的主java.lang.NullPointerException。我的AA有两个活动,第一个屏幕是基本配置文件主屏幕,第二个屏幕是图片库。我还不太擅长调试,所以我无法弄清楚为什么第二个活动部队关闭(FC)。

你能指出我正确的方向吗?

主要活动

package com.gallery.brien.picturegallery;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Picture;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class Main extends Activity {



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


}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void changePicture(View view) {
    /* Open picture gallery activity */
    Intent intent = new Intent(this,PictureGallery.class);
    startActivity(intent);

     }
}

第二项活动

package com.gallery.brien.picturegallery;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;


public class PictureGallery extends Activity {

//variable array where pictures are store
Integer[] Profile = {R.drawable.brien, R.drawable.kick, R.drawable.mma, R.drawable.fun,
        R.drawable.run};

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

    //  UI of gallery display. Initializes the GridView and ItemView
    //  classes. Then wait for interaction from user.
    GridView gal = (GridView) findViewById(R.id.gridView);
    final ImageView imageView = (ImageView) findViewById(R.id.imageSelected);

    //  Calls the ImageAdapter Class.
    gal.setAdapter(new ImageAdapter(this));
    // Calls the onItemClickListener Class
    gal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override

       /*                onItemClick Method
       The onItemClick method has four arguments.
        AdapterView<?> arg0 - Records where the user touched screen
        View arg1 - Parameters of the View user touched
        int arg2  - Integer value that holds the position of the View
                    in the adapter.
        long arg3 - Determines the row id of the item that was
                     selected by the user.
        */
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            Toast.makeText(getBaseContext(), "You have selected picture " + (arg2 + 1)
                    + "Brien Calloway", Toast.LENGTH_SHORT).show();
            imageView.setImageResource(Profile[arg2]);
        }
    });


}

public class ImageAdapter extends BaseAdapter{

    private Context context;
    public ImageAdapter(Context c){
        context=c;
    }

    public ImageAdapter(PictureGallery pictureGallery) {

    }

    @Override
    public int getCount() {return Profile.length;}

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView pic = new ImageView(context);
        pic.setImageResource(Profile[arg0]);
        pic.setScaleType(ImageView.ScaleType.FIT_XY);
        pic.setLayoutParams(new GridView.LayoutParams(200,175));
        return pic;
    }
}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_picture_gallery, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

logcat的

OpenGL ES Shader Compiler Version: E031.24.00.07
    Build Date: 01/22/14 Wed
    Local Branch: base_au149_adreno_au169_patches
    Remote Branch:
    Local Patches:
    Reconstruct Branch:
01-12 16:21:20.914  24367-24367/com.gallery.brien.picturegallery D/OpenGLRenderer﹕ Enabling debug mode 0

01-12 16:21:53.704  24367-24367/com.gallery.brien.picturegallery W/ApplicationPackageManager﹕ getCSCPackageItemText()

01-12 16:21:53.734  24367-24367/com.gallery.brien.picturegallery E/MoreInfoHPW_ViewGroup﹕ Parent view is not a TextView

01-12 16:21:53.784  24367-24367/com.gallery.brien.picturegallery D/AbsListView﹕ Get MotionRecognitionManager

01-12 16:21:53.804  24367-24367/com.gallery.brien.picturegallery D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4

01-12 16:21:53.804  24367-24367/com.gallery.brien.picturegallery D/AbsListView﹕ unregisterIRListener() is called

01-12 16:21:53.814  24367-24367/com.gallery.brien.picturegallery D/AbsListView﹕ onVisibilityChanged() is called, visibility : 0

01-12 16:21:53.814  24367-24367/com.gallery.brien.picturegallery D/AbsListView﹕ unregisterIRListener() is called

01-12 16:21:53.834  24367-24367/com.gallery.brien.picturegallery D/AbsListView﹕ unregisterIRListener() is called

01-12 16:21:53.844  24367-24367/com.gallery.brien.picturegallery D/AndroidRuntime﹕ Shutting down VM

01-12 16:21:53.844  24367-24367/com.gallery.brien.picturegallery W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x42068da0)

01-12 16:21:53.864  24367-24367/com.gallery.brien.picturegallery E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.gallery.brien.picturegallery, PID: 24367
    java.lang.NullPointerException
            at android.view.ViewConfiguration.get(ViewConfiguration.java:329)
            at android.view.View.<init>(View.java:3578)
            at android.widget.ImageView.<init>(ImageView.java:118)
            at com.gallery.brien.picturegallery.PictureGallery$ImageAdapter.getView(PictureGallery.java:85)
            at android.widget.AbsListView.obtainView(AbsListView.java:2733)
            at android.widget.GridView.onMeasure(GridView.java:1049)
            at android.view.View.measure(View.java:17367)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
            at android.view.View.measure(View.java:17367)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:17367)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
            at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:382)
            at android.view.View.measure(View.java:17367)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2505)
            at android.view.View.measure(View.java:17367)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2174)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1315)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1512)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1199)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6383)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
            at android.view.Choreographer.doCallbacks(Choreographer.java:603)
            at android.view.Choreographer.doFrame(Choreographer.java:573)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5293)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

01-12 16:26:54.014  24367-24367/com.gallery.brien.picturegallery I/Process﹕ Sending signal. PID: 24367 SIG: 9

2 个答案:

答案 0 :(得分:0)

删除该行:

 public ImageAdapter(PictureGallery pictureGallery) {

 }

您正在调用此构造函数,并且您的上下文为null。

答案 1 :(得分:0)

您的ImageAdapter正在使用其第二个构造函数构造,该构造函数未设置上下文。 null上下文导致NullPointerException上下文在ImageView构造函数中导致getView,{{1}}中调用{/ 1}}。

更改构造函数以设置上下文或完全删除构造函数。