onclick方法处的nullpointerexception

ct3nt3jp  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(321)

我在androidstudio上制作一个应用程序,在onclick方法中遇到了nullpointerexception。我该怎么修?
我的应用程序的调试器告诉我:

java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
        at android.view.View.performClick(View.java:6256)
        at android.view.View$PerformClick.run(View.java:24701)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
        at android.view.View.performClick(View.java:6256) 
        at android.view.View$PerformClick.run(View.java:24701) 
        at android.os.Handler.handleCallback(Handler.java:789) 
        at android.os.Handler.dispatchMessage(Handler.java:98) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference
        at com.example.venttome.ventingPage.onClick(ventingPage.java:66)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409) 
        at android.view.View.performClick(View.java:6256) 
        at android.view.View$PerformClick.run(View.java:24701) 
        at android.os.Handler.handleCallback(Handler.java:789) 
        at android.os.Handler.dispatchMessage(Handler.java:98) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

错误的代码(onclick()方法):

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.record_btn:
            if (isRecording){
               //stop recording
                recordButton.setImageDrawable(getResources().getDrawable(R.drawable.record_btn_stopped, null));
                isRecording = false;
            } else {
                //start recording
                recordButton.setImageDrawable(getResources().getDrawable(R.drawable.record_btn_recording, null));
                isRecording = true;
            }
            break;
    }
}

这是那个类的xml文件,它是一个约束布局。这就是我使用的onclick方法,我不知道有什么问题

<ImageView
        android:id="@+id/record_btn"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginStart="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginBottom="28dp"
        android:background="@android:color/white"
        android:clickable="true"
        android:cropToPadding="true"
        android:onClick="onClick"
        android:src="@drawable/record_btn_stopped"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toLeftOf="@id/final_button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/inputFeelings"
        app:layout_constraintVertical_bias="1.0"
        android:focusable="true"
        tools:ignore="ContentDescription" />

请帮帮我。

vbkedwbf

vbkedwbf1#

请初始化oncreate()中的视图这里是工作代码段

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

ImageView recordButton;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recordButton = findViewById(R.id.record_btn);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.record_btn:
            Toast.makeText(this, "Called", Toast.LENGTH_SHORT).show();
            recordButton.setImageResource(R.drawable.ic_launcher_foreground);
            break;

    }
}}
evrscar2

evrscar22#

您没有在方法中初始化按钮。请先在方法中初始化 onCreate() 方法,或者如果您使用的是fragment,那么在 onCreateView() :

ImageView record_btn= findViewById(R.id.record_btn);

相关问题