NullPointerException when starting an activity through intent - java

I have following code, I am truing to start an activity given certain condition is true. But over startActivity(controllerActivity); I am getting java.lang.NullPointerException
The controllerActivity variable at the stage contains a valid value of Intent { act=android.intent.action.Main }
Thread thr_authenticateUser = new Thread(new Runnable() {
#Override
public void run() {
…
Boolean _authenticationStatus = Boolean.valueOf(authenticationReplyValue);
if (_authenticationStatus) {
Intent controllerActivity = new Intent("android.intent.action.Main");
startActivity(controllerActivity); //NullPointerException
}
}
});
thr_authenticateUser.start();
Over further findings I found out that the exact place where it is failing is inside Main activitY’s `onCreate()
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.main_view); // HERE: java.lang.NullPointerException
However the view name main_view is correct and it exists.
Manifest:
<activity
android:name=".Main"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:windowSoftInputMode="adjustPan" >
<intent-filter>
<action android:name="android.intent.action.Main" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Compelte stack trace:
java.lang.NullPointerException
at android.app.Activity.startActivityForResult(Activity.java:3370)
at android.app.Activity.startActivityForResult(Activity.java:3331)
at android.app.Activity.startActivity(Activity.java:3566)
at android.app.Activity.startActivity(Activity.java:3534)
at com.citypulse.citypulse.User$1.run(User.java:57)
at java.lang.Thread.run(Thread.java:856)

Get the context to start your activity
getBaseContext().startActivity(controlledActivity);
or
YourActivity.this.startActivity(controlledActivity);

Related

SplashScreen doesn't show main activity but it automatically closes the app after showing the splashscreen

<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="App Name"
android:roundIcon="#drawable/logo"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true"
android:theme="#style/maintheme"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning"
tools:replace="android:icon, android:allowBackup"
>
this activity is splash screen activity below
<activity
android:name=""
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
this activity is Folder activity activity below
<activity
android:name= ""
android:theme= ""
android:configChanges="orientation|keyboardHidden|screenSize"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:exported="true">
</activity>
<activity android:name="" android:theme=""/>
<activity android:name="" android:theme="" android:configChanges="orientation|keyboardHidden|screenSize"/>
</application>
the problem is when the app run it show the splash screen and then it
show the app keep closing,it doesn't start the main activity.
can i make the android manifest to make the splash screen show first and then the start main activity
You cannot use your manifest for this, you need to use an Intent.
Code Example that you need in your SplashScreen Activity:
Intent intent = new Intent(SplashScreen.this, YOURNEXTCLASS.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
You can switch activities only from the java files.
Implement this in your OnCreate method.
SplashActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
startActivity(intent);
finish();
}
}, 2000); ////wait 2s before doing the action
}
AndroidManifest.xml
<activity
android:name=".ui.activities.SplashActivity"
android:exported="true"
android:theme="#style/SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.activities.MainActivity"
android:exported="true"/>
the simple method to do is use this in splashscreenactivity.java
Intent intent=new Intent(SplashScreen.this, YourClass.class);
startActivity(intent);
finish();
like this
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, 3000);
}
}
Make Sure Your compileSdkVersion 31 and targetSdkVersion 30.

I don't want the app to run the MainActivity while I pressed home in SplashScreen just like WhatsApp

I have put onPause in the SplashScreen activity to override the MainActivity but it won't work.
and I've searched the internet but still not find how to do it. it always start MainActivity even if I've pushed the home button
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Thread splashScreenThread = new Thread() {
#Override
public void run() {
try {
sleep(2000);
super.run();
} catch (InterruptedException e) {
Log.e(this.getClass().getSimpleName(), "showSplashScreen: " + e.getMessage());
} finally {
Intent moveToHomeActivityIntent = new Intent(SplashScreenActivity.this, HomeActivity.class);
startActivity(moveToHomeActivityIntent);
finish();
}
}
};
splashScreenThread.start();
}
#Override
protected void onPause(){
super.onPause();
finish();
}
Please try with this will working
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(), HomeActivity.class));
finish();
}
}, 2000);
Just go to your Manifest.xml and default your activity to SplashScreenActivity
Change This
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SplashScreenActivity" />
to This
<activity android:name=".SplashScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
I've found out the answer! It's so simple and easy to implement. just follow the instruction on this link
Thanks #Zun for providing the reference

Issue when starting Jitsi video call activity

I am able to run Jitsi video calling android sdk successfully when I add the main video calling activity as the launcher activity of the application, video is getting connected smooth and no worries . However when I changed to the code to calling the same activity from another activity it throws an activity not found exception .
Here is my manifest file
<activity
android:name=".activity.JitsiVideoCallActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize"
android:label="#string/app_name"
android:resizeableActivity="true"
android:supportsPictureInPicture="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="beta.hipchat.me"
android:scheme="https" />
<data
android:host="beta.meet.jit.si"
android:scheme="https" />
<data
android:host="chaos.hipchat.me"
android:scheme="https" />
<data
android:host="enso.me"
android:scheme="https" />
<data
android:host="hipchat.me"
android:scheme="https" />
<data
android:host="meet.jit.si"
android:scheme="https" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="org.jitsi.meet" />
</intent-filter>
</activity>
This is my activity which is supposed to do the video call
public class JitsiVideoCallActivity extends AppCompatActivity {
private JitsiMeetView view;
private static final String ADD_PEOPLE_CONTROLLER_QUERY = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new JitsiMeetView(this);
Bundle config = new Bundle();
config.putBoolean("startWithAudioMuted", false);
config.putBoolean("startWithVideoMuted", false);
Bundle urlObject = new Bundle();
urlObject.putBundle("config", config);
urlObject.putString("url", "https://meet.jit.si/wizcounsel");
view.loadURLObject(urlObject);
setContentView(view);
}
This is how I launch the intent
#OnClick(R.id.call)
void call() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
askForAudioPermission();
} else
startActivity(new Intent(this, JitsiMeetActivity.class),);
}
I have added JAVA 8 compatibility in my app level gradle file and dependencies on both the gradle files
What I have tried
changing launch mode to singletask App crashes
Making the video call activity the launcher App works
Extend AppCombactActivity and / or JitsiMee Activity App crashes
This is my crash log
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.star.star*, PID: 26197
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.star.star/org.jitsi.meet.sdk.JitsiMeetActivity}; have you declared this activity in your AndroidManifest.xml?
If any more info is needed , let me know, thanks in advance, Kindly help
initialize it in oncreate method.In any activity.
void connectCall() {
URL serverURL;
try {
serverURL = new URL("https://meet.jit.si");
} catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("Invalid server URL!");
}
JitsiMeetConferenceOptions defaultOptions
= new JitsiMeetConferenceOptions.Builder()
.setServerURL(serverURL)
.build();
JitsiMeet.setDefaultConferenceOptions(defaultOptions);
}
now call this method where you want. Example onclick listener
public void onVideoCall(String text) {
if (text.length() > 0) {
// Build options object for joining the conference. The SDK will merge the default
// one we set earlier and this one when joining.
JitsiMeetConferenceOptions options
= new JitsiMeetConferenceOptions.Builder()
.setRoom(text)
.setWelcomePageEnabled(false)
.build();
// Launch the new activity with the given options. The launch() method takes care
// of creating the required Intent and passing the options.
JitsiMeetActivity.launch(activity, options);
}
I think you forgot to register your activity in AndroidManifest.xml. You should register your activity in <application> tag as below.
<activity
android:name=".YourActivity"/>

Unable to start an activity on button click

I have a button on main activity which onclick is supposed to start a activity
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View v){
if(v.getId()==R.id.lbutton)
{
Intent i = new Intent(MainActivity.this,Display.class);
startActivity(i);
}
}
}
lbutton is the id of button
Display.java
public class Display extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondsc);
}
}
secondsc.xml is a layout file which contains content for the new activity
Manifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Display">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
remove intent-filter to DisplayActivity in your manifest file:
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
And register your clickListener button
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.lbutton).setOnClickLIstener(new OnClickLIstener (){
void onClick (View v) {
onButtonClick(v);
}
});
}
You need to add an event listener to the button. You can either implement the View.OnClickListener interface and implement the onClick method (good if you have multiple buttons that need listeners), or use an anonymous class:
btn.setOnClickListener(new View.OnClickListener() {
onClick (View v) {
}
}
Are you sure that the OnButtonClick is being executed? I recommend doing it this way:
Button lButton = (Button) findViewById(R.id.lButton);
lButton.setOnClickListener(new OnClickListener() {
onClick(View view) {
Intent i = new Intent(MainActivity.this,Display.class);
startActivity(i);
}
}
-Daniel

Launcher activity not found

So I am redoing my app so that there will be an animation on the start screen of the app. The only problem is it seems you cannot start an app with a view class. At least I'm not really sure if you can. Here is my code. With this code i get a launcher activity not found in the console?
public class SplashLaunch extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splashlaunch);
final Main d = new Main(this);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
setContentView(d);
}
}, 5000);
}
}
Here is the manifest file. I have a feeling that I'm going to need to change this.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Tripps.thesimplegame"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SplashLaunch"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SPLASHLAUNCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".YouFailed"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.YOUFAILED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Likely somewhere in your launcher activity you have an onCreate method:
public class YourActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.foobar);
}
}
Just change the setContentView to your custom view:
public class YourActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
MyCustomView view = new MyCustomView(this);
setContentView(view);
}
}
The Activity that you want to start when the app first starts MUST have the following Intent filter:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Categories