java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int android.view.View.getId()'

时间:2019-04-08 08:22:59

标签: java android

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int android.view.View.getId()'

点击动物时,应用程序崩溃。我了解viewById和onClick方法,但不知道该怎么看

这是我从logcat得到的错误:

 Process: com.example.ar, PID: 4416
    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getId()' on a null object reference
        at com.example.ar.MainActivity.setBackground(MainActivity.java:428)
        at com.example.ar.MainActivity.onClick(MainActivity.java:381)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

有我的MainActivity代码


    ArFragment arFragment;
    private ModelRenderable bearRenderable, catRenderable, cowRenderable,
   dogRenderable,elephantRenderable,ferretRenderable,hippopotamusRenderable,   horseRenderable,koalaRenderable,lionRenderable,reindeerRenderable,wolverineRenderable;

    ImageView bear, cat, cow, dog, elephant, ferret, hippopotamus, horse, koala, lion, reindeer, wolverine;

    View arrayView[];
    int selected = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.sceneform_ux_fragment);
        //View
        bear = (ImageView) findViewById(R.id.bear);
        cat = (ImageView) findViewById(R.id.cat);
        dog = (ImageView) findViewById(R.id.dog);
        cow = (ImageView) findViewById(R.id.cow);
        elephant = (ImageView) findViewById(R.id.elephant);
        ferret = (ImageView) findViewById(R.id.ferret);
        hippopotamus = (ImageView) findViewById(R.id.hippopotamus);
        horse = (ImageView) findViewById(R.id.horse);
        koala = (ImageView) findViewById(R.id.lion);
        reindeer = (ImageView) findViewById(R.id.reindeer);
        wolverine = (ImageView) findViewById(R.id.wolverine);

        SetArrayView();
        SetClickListener();
        setupModel();
        arFragment.setOnTapArPlaneListener(new BaseArFragment.OnTapArPlaneListener() {
            @Override
            public void onTapPlane(HitResult hitResult, Plane plane, MotionEvent motionEvent) {
                //kill
                if (selected == 1) {
                    Anchor anchor = hitResult.createAnchor();
                    AnchorNode anchorNode = new AnchorNode(anchor);
                    anchorNode.setParent(arFragment.getArSceneView().getScene());
                    createModel(anchorNode, selected);
                }
            }
        });

    }

    private void setupModel() {


        ModelRenderable.builder()
                .setSource(this, R.raw.bear)
                .build().thenAccept(renderable -> bearRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast.makeText(this, "Unnable to load bear model", Toast.LENGTH_SHORT).show();
                            return null;
                        }
                );

        ModelRenderable.builder()
                .setSource(this, R.raw.cat)
                .build().thenAccept(renderable -> catRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast.makeText(this, "Unnable to load cat model", Toast.LENGTH_SHORT).show();
                            return null;
                        }
                );

        ModelRenderable.builder()
                .setSource(this, R.raw.cow)
                .build().thenAccept(renderable -> cowRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast.makeText(this, "Unnable to load cow model", Toast.LENGTH_SHORT).show();
                            return null;
                        }
                );

        ModelRenderable.builder()
                .setSource(this, R.raw.dog)
                .build().thenAccept(renderable -> dogRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast.makeText(this, "Unnable to load dog model", Toast.LENGTH_SHORT).show();
                            return null;
                        }
                );

        ModelRenderable.builder()
                .setSource(this, R.raw.elephant)
                .build().thenAccept(renderable -> elephantRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast.makeText(this, "Unnable to load elephant model", Toast.LENGTH_SHORT).show();
                            return null;
                        }
                );

        ModelRenderable.builder()
                .setSource(this, R.raw.ferret)
                .build().thenAccept(renderable -> ferretRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast.makeText(this, "Unnable to load ferret model", Toast.LENGTH_SHORT).show();
                            return null;
                        }
                );
        ModelRenderable.builder()
                .setSource(this, R.raw.hippopotamus)
                .build().thenAccept(renderable -> hippopotamusRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast.makeText(this, "Unnable to load hippopotamus model", Toast.LENGTH_SHORT).show();
                            return null;
                        }
                );

        ModelRenderable.builder()
                .setSource(this, R.raw.horse)
                .build().thenAccept(renderable -> horseRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast.makeText(this, "Unnable to load horse model", Toast.LENGTH_SHORT).show();
                            return null;
                        }
                );

        ModelRenderable.builder()
                .setSource(this, R.raw.koala_bear)
                .build().thenAccept(renderable -> koalaRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast.makeText(this, "Unnable to load koala model", Toast.LENGTH_SHORT).show();
                            return null;
                        }
                );
        ModelRenderable.builder()
                .setSource(this, R.raw.lion)
                .build().thenAccept(renderable -> lionRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast.makeText(this, "Unnable to load lion model", Toast.LENGTH_SHORT).show();
                            return null;
                        }
                );
        ModelRenderable.builder()
                .setSource(this, R.raw.reindeer)
                .build().thenAccept(renderable -> reindeerRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast.makeText(this, "Unnable to load reindeer model", Toast.LENGTH_SHORT).show();
                            return null;
                        }
                );
        ModelRenderable.builder()
                .setSource(this, R.raw.wolverine)
                .build().thenAccept(renderable -> wolverineRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast.makeText(this, "Unnable to load wolverine model", Toast.LENGTH_SHORT).show();
                            return null;
                        }
                );
    }
    private void createModel(AnchorNode anchorNode, int selected) {
        if (selected == 1) {
            TransformableNode bear = new TransformableNode(arFragment.getTransformationSystem());
            bear.setParent(anchorNode);
            bear.setRenderable(bearRenderable);
            bear.select();
            addName(anchorNode,bear,"Bear");
        }
        if (selected == 2) {
            TransformableNode bear = new TransformableNode(arFragment.getTransformationSystem());
            bear.setParent(anchorNode);
            bear.setRenderable(catRenderable);
            bear.select();
            addName(anchorNode,bear,"Cat");
        }
        if (selected == 3) {
            TransformableNode bear = new TransformableNode(arFragment.getTransformationSystem());
            bear.setParent(anchorNode);
            bear.setRenderable(cowRenderable);
            bear.select();
            addName(anchorNode,bear,"Cow");
        }
        if (selected == 4) {
            TransformableNode bear = new TransformableNode(arFragment.getTransformationSystem());
            bear.setParent(anchorNode);
            bear.setRenderable(dogRenderable);
            bear.select();
            addName(anchorNode,bear,"Dog");
        }
        if (selected == 5) {
            TransformableNode bear = new TransformableNode(arFragment.getTransformationSystem());
            bear.setParent(anchorNode);
            bear.setRenderable(elephantRenderable);
            bear.select();
            addName(anchorNode,bear,"Elephant");
        }
        if (selected == 6) {
           TransformableNode bear = new TransformableNode(arFragment.getTransformationSystem());
            bear.setParent(anchorNode);
            bear.setRenderable(ferretRenderable);
            bear.select();
            addName(anchorNode,bear,"Ferret");
        }
        if (selected == 7) {
            TransformableNode bear = new TransformableNode(arFragment.getTransformationSystem());
            bear.setParent(anchorNode);
            bear.setRenderable(hippopotamusRenderable);
            bear.select();
            addName(anchorNode,bear,"Hippopotamus");
        }
        if (selected == 8) {
            TransformableNode bear = new TransformableNode(arFragment.getTransformationSystem());
            bear.setParent(anchorNode);
            bear.setRenderable(horseRenderable);
            bear.select();
            addName(anchorNode,bear,"Horse");
        }
        if (selected == 9) {
            TransformableNode bear = new TransformableNode(arFragment.getTransformationSystem());
            bear.setParent(anchorNode);
            bear.setRenderable(koalaRenderable);
            bear.select();
            addName(anchorNode,bear,"Koala");
        }
        if (selected == 10) {
           TransformableNode bear = new TransformableNode(arFragment.getTransformationSystem());
            bear.setParent(anchorNode);
            bear.setRenderable(lionRenderable);
            bear.select();
            addName(anchorNode,bear,"Lion");
        }
        if (selected == 11) {

            TransformableNode bear = new TransformableNode(arFragment.getTransformationSystem());
            bear.setParent(anchorNode);
            bear.setRenderable(reindeerRenderable);
            bear.select();
            addName(anchorNode,bear,"Reindeer");
        }
        if (selected == 12) {

            TransformableNode bear = new TransformableNode(arFragment.getTransformationSystem());
            bear.setParent(anchorNode);
            bear.setRenderable(wolverineRenderable);
            bear.select();
            addName(anchorNode,bear,"Wolverine");

        }
    }

    private void addName(AnchorNode anchorNode, TransformableNode model, String name) {

       ViewRenderable.builder().setView(this,R.layout.name_animal)
               .build()
               .thenAccept(viewRenderable -> {
                   TransformableNode nameView = new TransformableNode(arFragment.getTransformationSystem());
                   nameView.setLocalPosition(new Vector3(0f,model.getLocalPosition().y+0.5f,0));
                   nameView.setParent(anchorNode);
                   nameView.setRenderable(viewRenderable);
                   nameView.select();
                    //text
                   TextView txt_name = (TextView)viewRenderable.getView();
                   txt_name.setText(name);
                   //click to remove animal
                   txt_name.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View v) {
                           anchorNode.setParent(null);
                       }
                   });

               });
    }


    private void SetClickListener() {
        for (int i = 0; i < arrayView.length; i++) {
            if (arrayView[i] != null)
                arrayView[i].setOnClickListener(this);
        }
    }
    private void SetArrayView() {
        arrayView = new View[]{              bear,cat,cow,dog,elephant,ferret,hippopotamus,horse,koala,lion,reindeer,wolverine
        };
    }
    @Override
    public void onClick(View view) {
    if (view.getId()== R.id.bear) {
        selected = 1;
        setBackground(view.getId());
    }
    else if (view.getId()==R.id.cat) {
        selected = 2;
        setBackground(view.getId());
    }
    else if (view.getId()==R.id.cow) {
        selected = 3;
        setBackground(view.getId());
    }
    else if (view.getId()==R.id.dog) {
        selected = 4;
        setBackground(view.getId());
    }
    else if (view.getId()==R.id.elephant) {
        selected = 5;
        setBackground(view.getId());
    }
    else if (view.getId()==R.id.ferret) {
        selected = 6;
        setBackground(view.getId());
    }
    else if (view.getId()==R.id.hippopotamus) {
        selected = 7;
        setBackground(view.getId());
    }
    else if (view.getId()==R.id.horse) {
        selected =8;
        setBackground(view.getId());
    }
    else if (view.getId()==R.id.koala) {
        selected = 9;
        setBackground(view.getId());
   }
    else if (view.getId()==R.id.lion) {
        selected = 10;
        setBackground(view.getId());
    }
    else if (view.getId()==R.id.reindeer) {
        selected = 11;
        setBackground(view.getId());
    }
    else if (view.getId()==R.id.wolverine) {
        selected =12;
        setBackground(view.getId());
    }
    }
    private void setBackground(int id) {
    for (int i =0;i<arrayView.length;i++){
        if (arrayView[i].getId() == id)
        arrayView[i].setBackgroundColor(Color.parseColor("#80333639"));
        else
            arrayView[i].setBackgroundColor(Color.TRANSPARENT);
    }
    }}

I am new at java programming so help me please to understand where is my mistakes.

0 个答案:

没有答案