Android studio error: java.lang.RuntimeException: Unable to start activity ComponentInfo - java

I was trying this code, it can build successfully but it crashes immediately.
The errors shows:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.logintest/com.example.logintest.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.google.android.gms.common.SignInButton.findViewById(int)' on a null object reference.
And it said it is caused by :
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.google.android.gms.common.SignInButton.findViewById(int)' on a null object reference
I tried for a long time but still don't know how to fix it, any can help please, thanks!
The LoginActivity.java code are as follow:
public class LoginActivity extends AppCompatActivity {
public static final int GOOGLE_SIGN_IN_CODE = 10005;
SignInButton signIn;
GoogleSignInOptions gso;
GoogleSignInClient signInClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
signIn.findViewById(R.id.signIn);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
signInClient = GoogleSignIn.getClient(this,gso);
GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this);
if(signInAccount != null) {
startActivity(new Intent(this,MainActivity.class));
}
signIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent sign = signInClient.getSignInIntent();
startActivityForResult(sign, GOOGLE_SIGN_IN_CODE);
}
});
}
}
and the error code:
2021-05-28 21:37:33.617 10526-10526/com.example.logintest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.logintest, PID: 10526
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.logintest/com.example.logintest.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.google.android.gms.common.SignInButton.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.google.android.gms.common.SignInButton.findViewById(int)' on a null object reference
at com.example.logintest.LoginActivity.onCreate(LoginActivity.java:29)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6944) 
part of activity_login.xml:
<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"
tools:context=".LoginActivity">
<com.google.android.gms.common.SignInButton
android:id="#+id/signIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="200dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="300dp"
android:layout_marginEnd="200dp"
android:layout_marginRight="200dp"
android:layout_marginBottom="300dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Related

Android Studio Ads Throw Errors [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 1 year ago.
I have been trying to implement ads into my android studio project, but whenever I try and run my app it crashes before showing anything likely due to the OnStart function. The code is:
private AdView mAdView = findViewById(R.id.adView1);
public String Input;
public search search = new search();
#Override
protected void onCreate(Bundle savedInstanceState)
{
getSupportActionBar().hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
The app runs when I remove the private AdView mAdView line from the start but no ads are shown.
My XML is:
<com.google.android.gms.ads.AdView
android:id="#+id/adView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="53dp"
android:layout_marginTop="420dp"
app:adSize="BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></com.google.android.gms.ads.AdView>
The error code that is thrown when the app is run is:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.firstever, PID: 10185
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.firstever/com.example.firstever.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
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:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:173)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
at android.content.Context.obtainStyledAttributes(Context.java:744)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
at com.example.firstever.MainActivity.<init>(MainActivity.java:24)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
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:2066) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:223) 
at android.app.ActivityThread.main(ActivityThread.java:7656) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
check out your manifest if you add this code
...<application ... > ...
< meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ad_user_id_here_from_admob" />
...</application>...

NullPointerException at onClick method

I am making an app on Android Studio and I ran into the NullPointerException in my onClick method. How do I fix it?
The debugger of my app gives me this:
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)
The code (the onClick() method) that is wrong:
#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;
}
}
And this is my XML file for that class, and it is a constraint layout. This is what I use for my onClick method and I don't know what's wrong with it:
<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" />
you are not initializing the button in your method.Please first do this in your onCreate() method or if you are using fragment then in onCreateView():
ImageView record_btn= findViewById(R.id.record_btn);
Please initialize the view inside onCreate()
Here is the working code snippet
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;
}
}}

attempt to invoke virtual method on null object reference while using Recycler View

I am beginner app dev trying to create chat app. The app starts smoothly but when I want to display all the registered users in a Recycler View app crashes . It doesnt show any error still the app crashes and when commented out the error just goes to the next line. Any help would be much appreciated.
Logcat
2020-11-23 10:38:39.697 4907-4907/com.example.decide_o E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.decide_o, PID: 4907
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.decide_o/com.example.decide_o.AllUsers}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
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 androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at com.example.decide_o.AllUsers.onCreate(AllUsers.java:38)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
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) 
All Users
public class AllUsers extends AppCompatActivity {
private RecyclerView nuserlist;
private DatabaseReference nUserDatabase;
private LinearLayoutManager nlayoutmanager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nUserDatabase=FirebaseDatabase.getInstance().getReference().child("Users");
nlayoutmanager=new LinearLayoutManager(this);
nuserlist = (RecyclerView) findViewById(R.id.userlist);
nuserlist.setHasFixedSize(true);
nuserlist.setLayoutManager(nlayoutmanager);
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Users,UsersViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.single_users,
UsersViewHolder.class,
nUserDatabase
) {
#Override
protected void populateViewHolder(UsersViewHolder usersViewHolder, Users users, int i) {
usersViewHolder.setName(users.getName());
}
};
nuserlist.setAdapter(firebaseRecyclerAdapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder{
View nView;
public UsersViewHolder(#NonNull View itemView) {
super(itemView);
nView=itemView;
}
public void setName(String name) {
TextView nameview=(TextView)nView.findViewById(R.id.single_username);
nameview.setText(name);
}
}
}
xml
<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="match_parent"
android:layout_height="match_parent"
tools:context=".AllUsers">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/userlist"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
the issue here is you haven't set your content view.
Add this line in you onCreate method :-
setContentView(R.layout.your_activity_xml);
after your super.oncreate is called.
If you don't assign your content view then it won't be identified that from where to fetch your recyclerView :- nuserlist = (RecyclerView) findViewById(R.id.userlist);.

videoview.setVideoUri shows null pointer exception

So I have a VideoView but when I try to play video (which is in my res/raw folder)it gives me null pointer exception. I tried using video.setVideoUri(uri) and also video.setVideoPath(path) but it still shows null pointer exception.
Activity:
public class Player extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
VideoView videoView=findViewById(R.id.videoView);
String path="android.resource://"+getPackageName()+"/"+R.raw.playthis;
Uri uri=Uri.parse(path);
videoView.setVideoPath(uri.getPath());
}
public void goBackVideo(View view){
finish();
}
}
xml for VideoView:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/VideoPlayerFrameLayout" android:background="#drawable/player_video_pause">
<VideoView android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/videoView"/>
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:src="#drawable/player_icon_play" android:onClick="setPlayORpause"
android:id="#+id/PauseButton" android:background="#fff"/>
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="left|center" android:layout_marginLeft="160dp"
android:src="#drawable/player_icon_prev" android:id="#+id/PrevButton" android:background="#fff"/>
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="right|center" android:layout_marginRight="160dp"
android:src="#drawable/player_icon_next" android:id="#+id/NextButton" android:background="#fff"/>
<ImageButton
android:layout_width="46dp"
android:layout_height="34dp"
android:id="#+id/backPressButton"
android:rotation="180" android:backgroundTint="#fff"
android:background="#null"
android:src="#drawable/settings_icon_back"
android:layout_gravity="left"
android:onClick="goBackVideo" android:drawingCacheQuality="high"/>
</FrameLayout>
Logcat:
2020-10-29 06:58:50.965 13640-13640/com.testapp.test3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.testapp.test3, PID: 13640
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testapp.test3/com.testapp.test3.VideoPlayer}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoPath(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2887)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2965)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1669)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6702)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoPath(java.lang.String)' on a null object reference
at com.testapp.test3.VideoPlayer.onCreate(VideoPlayer.java:26)
at android.app.Activity.performCreate(Activity.java:7115)
at android.app.Activity.performCreate(Activity.java:7106)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2840)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2965) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1669) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:192) 
at android.app.ActivityThread.main(ActivityThread.java:6702) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826) 
Do I need to set up a content provider before I can play any video?
How do I fix this?
EDIT:
Now i get another error
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
final VideoView videoView=findViewById(R.id.videoView);
Uri uri=Uri.parse("content://" +"sdcard/"+Environment.DIRECTORY_DOWNLOADS+"/"+"playthis.mp4");
MediaController mediaController=new MediaController(this);
videoView.setMediaController(mediaController);
mediaController.setAnchorView(videoView);
videoView.setVideoURI(uri);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
videoView.seekTo(0);
videoView.start();
}
});
}
logcat:
2020-10-29 07:51:39.371 27420-27420/com.testapp.test3 E/ActivityThread: Failed to find provider info for sdcard
2020-10-29 07:51:39.371 27420-27420/com.testapp.test3 W/MediaPlayer: Couldn't open content://sdcard/Download/playthis.mp4: java.io.FileNotFoundException: No content provider: content://sdcard/Download/playthis.mp4
2020-10-29 07:51:39.375 27420-27420/com.testapp.test3 E/MediaPlayerNative: Unable to create media player
2020-10-29 07:51:39.381 27420-27420/com.testapp.test3 W/VideoView: Unable to open content: content://sdcard/Download/playthis.mp4
java.io.IOException: setDataSource failed.: status=0x80000000
at android.media.MediaPlayer.nativeSetDataSource(Native Method)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1181)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1169)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1087)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1108)
at android.widget.VideoView.openVideo(VideoView.java:399)
at android.widget.VideoView.-wrap0(Unknown Source:0)
at android.widget.VideoView$7.surfaceCreated(VideoView.java:678)
at android.view.SurfaceView.updateSurface(SurfaceView.java:710)
at android.view.SurfaceView$2.onPreDraw(SurfaceView.java:145)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:977)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2427)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1446)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6954)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:925)
at android.view.Choreographer.doCallbacks(Choreographer.java:737)
at android.view.Choreographer.doFrame(Choreographer.java:669)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:911)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6702)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
You should add a contentView on your activity onCreate So change your onCreate Like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout_name) // Add this line,
VideoView videoView=findViewById(R.id.videoView);
String path="android.resource://"+getPackageName()+"/"+R.raw.playthis;
Uri uri=Uri.parse(path);
videoView.setVideoPath(uri.getPath());
}
After that you should check null video path, like
// If path is not null then set it to your videoView.
if(uri.getPath() != null) {
videoView.setVideoPath(uri.getPath());
}

App crashes on startup after adding ImageView [duplicate]

This question already has answers here:
Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
(2 answers)
Closed 4 years ago.
I am running a basic app with one ImageView. It runs until I add the following
ImageView gohan = findViewById(R.id.gohanView);
These are the only things that I've done:
Created project
Created empty activity
Added ImageView to activity
Imported image to drawable folder
Attached image to src of ImageView
RUN (Everything works at this point)
I then add the code above to my MainActivity class and the app will not run anymore.
Java
package com.example.david.dragonballz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView gohan = findViewById(R.id.gohanView);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".MainActivity">
<ImageView
android:id="#+id/gohanView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="69dp"
android:layout_marginEnd="114dp"
android:layout_marginLeft="114dp"
android:layout_marginRight="114dp"
android:layout_marginStart="114dp"
android:layout_marginTop="69dp"
android:src="#drawable/gohan"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Logcat
04-18 13:02:51.416 15392-15392/com.example.david.dragonballz E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.david.dragonballz, PID: 15392
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.david.dragonballz/com.example.david.dragonballz.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2839)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:56)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.david.dragonballz.MainActivity.<init>(MainActivity.java:9)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1180)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2829)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6938) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
Here is example of WORKING code from the Android classes I'm taking. Why does this work for him and used to work for me?
EXAMPLE
Never find id of any views before onCreate() method because the xml layout is loaded on onCreate().
setContentView(R.layout.activity_main);
find id in onCreate()
gohan = (ImageView) findViewById(R.id.gohanView);
Declare this
public ImageView gohan;
Add this line in oncreate()
gohan = (ImageView)findViewById(R.id.gohanView);
after this line
setContentView(R.layout.activity_main);
Finally your onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gohan = (ImageView)findViewById(R.id.gohanView);
gohan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something
gohan.animate().alpha(0).setDuration(2000);
}
});
}
ALSO NOW ACCESS THIS IMAGEVIEW ANYWHERE

Categories