Android Studio应用程序-java.lang.IllegalStateException:无法执行android:onClick的方法-非空指针异常

问题描述 投票:0回答:1

我正在尝试将视频从该应用程序上传到Firebase,但是当我按下“发布按钮”时,出现了我之前从未遇到的错误。我进行了一些研究,发现此错误的某些人实际上只是在获取Null Pointer Exception,所以我尝试了此修复程序,但此方法不起作用。任何帮助表示赞赏。

VideoPlayActivity.java

public class VideoPlayActivity extends AppCompatActivity {

    private VideoView mVideoView;
    public static DatabaseReference mDatabase;
    public static Uri videoUri;
    public static String videoTitle;

    public static EditText videoTitleField;

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

        FirebaseDatabase database = FirebaseDatabase.getInstance();

        mDatabase = database.getInstance().getReference();

        mVideoView = findViewById(R.id.videoView);

        videoUri = Uri.parse(getIntent().getExtras().getString("videoUri"));

        mVideoView.setVideoURI(videoUri);

        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(true);
            }
        });

        mVideoView.start();
    }

    public void publish(View view) {
        videoTitleField = findViewById(R.id.videoTitle);

        videoTitle = videoTitleField.getText().toString();

                if(videoTitle.equals("")) {
                    return;
                }

                mDatabase.child("videos").child(videoTitle).setValue(videoUri, new DatabaseReference.CompletionListener() {
                    @Override
                    public void onComplete(@Nullable DatabaseError databaseError, @NonNull DatabaseReference databaseReference) {
                        if (databaseError != null) {
                            System.out.println("Data could not be saved " + databaseError.getMessage());
                        } else {
                            System.out.println("Data saved successfully.");
                        }
                    }
                });

                Intent mainPage = new Intent(VideoPlayActivity.this, MainActivity.class);
                startActivity(mainPage);
    }
}

activity_video_play.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#20FFFFFF"
    tools:context=".VideoPlayActivity">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"></VideoView>

    <Button
        android:id="@+id/publish"
        android:layout_width="match_parent"
        android:layout_height="62dp"
        android:layout_weight="1"
        android:background="#60FFFFFF"
        android:text="PUBLISH VIDEO"
        android:textSize="36sp"
        android:onClick="publish"/>

    <RelativeLayout
        android:id="@+id/coverView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="#74A6A6A6"
        android:visibility="visible"
        tools:ignore="RtlCompat">

        <EditText
            android:id="@+id/videoTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:ems="10"
            android:hint="Enter a Title"
            android:inputType="textPersonName"
            android:textSize="24sp"
            android:visibility="visible" />

    </RelativeLayout>

</RelativeLayout>

Logcat错误

2020-06-08 10:39:56.988 20236-20236/com.example.resol E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.resol, PID: 20236
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
        at android.view.View.performClick(View.java:6277)
        at android.view.View$PerformClick.run(View.java:24765)
        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:6797)
        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:772)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
        at android.view.View.performClick(View.java:6277) 
        at android.view.View$PerformClick.run(View.java:24765) 
        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:6797) 
        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:772) 
     Caused by: java.lang.StackOverflowError: stack size 8MB
        at java.lang.reflect.Method.invoke(Native Method)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.serialize(com.google.firebase:firebase-database@@19.3.0:657)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(com.google.firebase:firebase-database@@19.3.0:167)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$200(com.google.firebase:firebase-database@@19.3.0:47)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.serialize(com.google.firebase:firebase-database@@19.3.0:675)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(com.google.firebase:firebase-database@@19.3.0:167)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$200(com.google.firebase:firebase-database@@19.3.0:47)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.serialize(com.google.firebase:firebase-database@@19.3.0:675)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(com.google.firebase:firebase-database@@19.3.0:167)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$200(com.google.firebase:firebase-database@@19.3.0:47)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.serialize(com.google.firebase:firebase-database@@19.3.0:675)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(com.google.firebase:firebase-database@@19.3.0:167)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$200(com.google.firebase:firebase-database@@19.3.0:47)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.serialize(com.google.firebase:firebase-database@@19.3.0:675)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(com.google.firebase:firebase-database@@19.3.0:167)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$200(com.google.firebase:firebase-database@@19.3.0:47)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.serialize(com.google.firebase:firebase-database@@19.3.0:675)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(com.google.firebase:firebase-database@@19.3.0:167)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$200(com.google.firebase:firebase-database@@19.3.0:47)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.serialize(com.google.firebase:firebase-database@@19.3.0:675)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(com.google.firebase:firebase-database@@19.3.0:167)
java android
1个回答
0
投票

您正在保存URI类型的videoUri,它不是setValue函数的有效参数,因此会引发IllegalStateException。而是调用videoUri.toString()。

© www.soinside.com 2019 - 2024. All rights reserved.