-1

I want android studio activity use youtube API play click button close app but error to NullpotinerException... So I was doing

  1. Check Google API key
  2. Check the variable name
  3. Check manifest permission
  4. Check .Jar fileenter image description here

Is there any other problem?

this activity code

  public class ThirdActivity extends YouTubeBaseActivity {
    YouTubePlayerView youtubeView;
    Button button;
    YouTubePlayer.OnInitializedListener listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.youtubeButton);
        youtubeView = (YouTubePlayerView) findViewById(R.id.youtubeView);
        listener = new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                youTubePlayer.loadVideo("Ktazno7rgZg&t");
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
            }
        };
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                youtubeView.initialize("is my apikey i was check", listener);
            }
        });
    }
}

this xml code

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
        android:layout_height="match_parent">
    
    
        <com.google.android.youtube.player.YouTubePlayerView
            android:id="@+id/youtubeView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:ignore="MissingConstraints"/>
    
        <Button
            android:id="@+id/youtubeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content
            android:text="Play"
            android:textColor="#000000"
            tools:ignore="MissingConstraints" />
    </androidx.constraintlayout.widget.ConstraintLayout>

Error massage

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myproject, PID: 30726 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myproject/com.example.myproject.ThirdActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3792) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3968) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8512) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.myproject.ThirdActivity.onCreate(ThirdActivity.java:34) at android.app.Activity.performCreate(Activity.java:8198) at android.app.Activity.performCreate(Activity.java:8182) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3765) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3968)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:246)  at android.app.ActivityThread.main(ActivityThread.java:8512)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)  I/Process: Sending signal. PID: 30726 SIG: 9

1 Answers1

-1

I think you might now be using the correct XML file in your code, it's assumption, but most likely it is the case. Your ThirdActivity is using

setContentView(R.layout.activity_main);

I think you forgot to use the correct XML file in above code, and this activity_main is a different XML that is meant for MainActivity or something.

And the Button, YouTubePlayerView in the XML you shared is not been used in your ThirdActivity.

Note: it's very easy to have these kinds of issue if we are copy-pasting few codes between for different activity, and we don't realise the issue at compile time because Android studio doesn't give compile time error for this when you are using other layout XML but in findViewById using the different id of different layout altogether, you get to know about it in runtime by getting error like this.

Dinkar Kumar
  • 2,175
  • 2
  • 12
  • 22
  • 1
    I read your note, always remember that – babyDeveloper May 18 '21 at 06:40
  • glad that i could help you figure out the issue and also able to provide you with some pointer which will help you in future if something like this happens again – Dinkar Kumar May 18 '21 at 06:41
  • You really need to stop answering duplicates. You're causing more work for people actively doing moderation – Zoe May 18 '21 at 08:23
  • @Zoe I did request to follow the null pointer post if you see I commented in question, but the person didn't find the solution, that is the reason I answered, I can try to search for more questions and link them rather than answering related with that, will make sure not cause more work for fellow people by doing this. thanks for pointing out and will make sure this doesn't happen – Dinkar Kumar May 18 '21 at 08:30