Android Studio:尝试在空对象引用上调用虚拟方法“void android.view.View.setOnClickListener”

Posted

技术标签:

【中文标题】Android Studio:尝试在空对象引用上调用虚拟方法“void android.view.View.setOnClickListener”【英文标题】:Android Studio: Attempt to invoke virtual method 'void android.view.View.setOnClickListener' on a null object reference 【发布时间】:2019-07-24 09:22:20 【问题描述】:

我不断收到 NullPointerException。

Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

我知道这是因为 (SurfaceView)view.findViewById(R.id.surfaceview_show_rectangle) 为空,但我不明白为什么会这样以及如何解决。

我试过这个solution。但它没有用。 下面是我的 Fragment.java 和 xml 文件。

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        View view = inflater.inflate(R.layout.fragment_camera2_basic, container, false);
        surfaceview = (SurfaceView)view.findViewById(R.id.surfaceview_show_rectangle);
        return view;

    


    @Override
    public void onViewCreated(final View view, Bundle savedInstanceState) 

        view.findViewById(R.id.picture).setOnClickListener(this);
        view.findViewById(R.id.info).setOnClickListener(this);

        mTextureView = (AutoFitTextureView) view.findViewById(R.id.texture);
        //view.findViewById(R.id.surfaceview_show_rectangle).setOnClickListener(this);


        //surfaceview = (SurfaceView)view.findViewById(R.id.surfaceview_show_rectangle);
        surfaceview.setZOrderOnTop(true);  
        surfaceview.getHolder().setFormat(PixelFormat.TRANSPARENT);  
        surfaceHolder = surfaceview.getHolder(); 


        surfaceview_result = (SurfaceView)view.findViewById(R.id.surfaceview_show_result);
        surfaceview_result.setZOrderOnTop(true);  
        surfaceview_result.getHolder().setFormat(PixelFormat.TRANSPARENT);  
        surfaceResultHolder = surfaceview_result.getHolder(); 
    
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_>

    <SurfaceView
        android:id="@+id/surfaceview_show_rectangle"
        android:layout_
        android:layout_
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        />

    <SurfaceView
        android:id="@+id/surfaceview_show_result"
        android:layout_
        android:layout_
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp" />

    <com.example.android.camera2basic.AutoFitTextureView
        android:id="@+id/texture"
        android:layout_
        android:layout_
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp" />

    <FrameLayout
        android:id="@+id/control"
        android:layout_
        android:layout_
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@color/control_background">

        <Button
            android:id="@+id/picture"
            android:layout_
            android:layout_
            android:layout_gravity="center"
            android:visibility="invisible"
            android:text="@string/picture" />

        <ImageButton
            android:id="@+id/info"
            android:contentDescription="@string/description_info"
            style="@android:style/Widget.Material.Light.Button.Borderless"
            android:layout_
            android:layout_
            android:layout_gravity="center_vertical|right"
            android:padding="20dp"
            android:visibility="invisible"
            android:src="@drawable/ic_action_info" />

    </FrameLayout>

    <TextView
        android:id="@+id/textView"
        android:layout_
        android:layout_
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="69dp"
        android:layout_marginBottom="37dp"
        android:text="Camera2Dome"
        android:textColor="#fff"
        android:textSize="35sp" />

</RelativeLayout>


以下是我的错误消息。

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.camera2basic, PID: 9168
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.android.camera2basic/com.example.android.camera2basic.CameraActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.SurfaceView.setZOrderOnTop(boolean)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
        at android.app.ActivityThread.access$1000(ActivityThread.java:150)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5444)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.SurfaceView.setZOrderOnTop(boolean)' on a null object reference
        at com.example.android.camera2basic.Camera2BasicFragment.onViewCreated(Camera2BasicFragment.java:565)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1430)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3231)
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3181)
        at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:192)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:572)
        at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
        at android.app.Activity.performStart(Activity.java:6317)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2392)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
        at android.app.ActivityThread.access$1000(ActivityThread.java:150) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5444) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) 
I/Process: Sending signal. PID: 9168 SIG: 9
Application terminated.

我非常感谢任何帮助。提前致谢!!

【问题讨论】:

见***.com/a/38718205/192373。 我想你把这个表面视图布局放在一些片段中。我们也需要检查一下。所以发布更多代码。这样它会降低获取解决方案的复杂性 嗨@SIVAKUMAR.J!我更新了上面的xml文件。你能指定你需要哪部分代码吗? 【参考方案1】:

试试这个:

View view = inflater.inflate(R.layout.fragment_camera2_basic, null);

LayoutInflater lf = getActivity().getLayoutInflater();   
View view = lf.inflate(R.layout.fragment_camera2_basic, container, false);

【讨论】:

【参考方案2】:

输入你的代码

surfaceview = (SurfaceView)view.findViewById(R.id.surfaceview_show_rectangle);

在线上方

surfaceview.setZOrderOnTop(true);  

那就试试

【讨论】:

【参考方案3】:

您正在设置侦听器: view.findViewById(R.id.picture).setOnClickListener(this);,但是您的活动是否实现了OnClickListener

【讨论】:

以上是关于Android Studio:尝试在空对象引用上调用虚拟方法“void android.view.View.setOnClickListener”的主要内容,如果未能解决你的问题,请参考以下文章

尝试在空对象引用上调用虚拟方法 'android.graphics.Rect android.graphics.drawable.Drawable.getBounds()'

尝试在空对象引用上调用虚拟方法 'android.view.Window$Callback android.view.Window.getCallback()'

Android错误尝试在空对象引用上调用虚拟方法[重复]

尝试在空对象引用上调用虚拟方法 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()'

JPE:尝试在空对象引用上调用虚拟方法“android.content.res.Resources”

尝试在空对象引用上调用虚拟方法 'void android.view.View.measure(int, int)'