尝试在空对象引用上调用虚拟方法'void android.view.View.setOnClickListener(android.view.View $ OnClickListener)'

时间:2019-04-01 14:12:14

标签: java android

尝试学习适用于AR的googleSceneform,每次遇到问题我的应用都会崩溃?

我在logcat和应用程序中遇到的错误也没有在模拟器和电话上发出。

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ar/com.example.ar.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        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)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.ar.MainActivity.SetClickListener(MainActivity.java:213)
        at com.example.ar.MainActivity.onCreate(MainActivity.java:63)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 

这是我的MainActivty.java应用程序立即崩溃,甚至没有打开

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ArFragment arFragment;
    private ModelRenderable bearRenderable;

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

    View arrayView[];
    ViewRenderable name_animal;
    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);

        SetArrayView();
        SetClickListener();
        setupModel();
        arFragment.setOnTapArPlaneListener(new BaseArFragment.OnTapArPlaneListener() {
            @Override
            public void onTapPlane(HitResult hitResult, Plane plane, MotionEvent motionEvent) {          
                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;
                        }
                );
    private void createModel(AnchorNode anchorNode, int selected) {
        if (selected == 1) {
            TransformableNode bear = new TransformableNode(arFragment.getTransformationSystem());
            bear.setParent(anchorNode);
            bear.setRenderable(bearRenderable);
            bear.select();
        }
    }
    private void SetClickListener() {
        for (int i=0;i<arrayView.length;i++)
            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 v) {

    }
}

我是一个初学者,所以我只是想找出我的错误。我认为这与View和ClickListener有关

3 个答案:

答案 0 :(得分:0)

要防止出现空指针异常,可以在SetClickListener方法中执行此操作:

 private void SetClickListener() {
        for (int i=0;i<arrayView.length;i++)
        {
           if( arrayView[i] != null ) 
            arrayView[i].setOnClickListener(this);
        }    
    }

答案 1 :(得分:0)

在您的代码中,只有task.run()task.call()不为空,因此您可以绑定其他图像视图,也可以在非空对象上应用单击侦听器,例如

bear

答案 2 :(得分:0)

在您的onCreate方法中,您只初始化了两个ImageView组件,bear和cat。其余的是牛,狗,大象... null。 要进行管理,您应该找到所有其他ImageView组件。

cow = (ImageView) findViewById(R.id.cow);
dog = (ImageView) findViewById(R.id.dog);

或测试null中的setOnClickListener

 private void SetClickListener() {
        for (int i=0;i<arrayView.length;i++)
          if (arrayView[i] != null)
            arrayView[i].setOnClickListener(this);
    }