Other activity on the start - java

I have 2 activity in my app - first and second. Currenly my default is first.
<activity
android:name=".FirstActivity"
android:label="randomlabel"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
Now i want to change default activity so i change like this:
<activity
android:name=".FirstActivity"
android:label="randomlabel"
>
</activity>
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity
and app now didn't work, what is bad here ?
error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.russian.app/ru.russian.app.FirstActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Edit2:
FirstActivity
public class FirstActivity extends AppCompatActivity {
Button button10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
button10.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
goToSecond();
}
});
}
private void goToSecond() {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}

It looks like from your stacktrace that you forgot to bind the button in FirstActivity. You need to bind it by calling
button10 = (Button) findViewById(R.id.my_button_id);
Replace my_button_id with the id that you set for the button in your activity_first layout.

Try this:
<activity
android:name=".FirstActivity"
android:label="randomlabel">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity

it looks like you are trying to set a onClick() listener in your activity on a view that hasn't been initialised correctly.
you need to initialise the button from the inflated view.
add
button10 = (Button) findViewById(R.id.button10);

Related

Airbnb deeplinkdispatch can't setup to work

I am starter at Android, I found this library for deep links, and it's just not working.
After the example beneath, I didn't receive the deep link.
I annotate my activity as in the GitHub example
#DeepLink("http://example.com/{id}")
public class SplashActivity extends AppCompatActivity {
I setup manifest.xml
<activity
android:name="com.example.SplashActivity"
android:exported="true"
android:label="#string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="example.com" />
<data android:scheme="http"/>
</intent-filter>
</activity>
And I setup the rest on onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
if (getIntent().getBooleanExtra(DeepLink.IS_DEEP_LINK, false)) {
Bundle parameters = getIntent().getExtras();
Log.d("TAG", "Deeplink params: " + parameters);
String idString = parameters.getString("id");
Log.d("id", idString);
link = idString;
}
I trigger the deep link via adb
adb shell am start -W -a android.intent.action.VIEW -d "http://example.com/123"

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.

Android Studio- Why is this image button closing my app?

Theres a image button that should bring my to a different activity but it closes my app. Any one have any ideas as to why?
ImageButton imagebtn2 = (ImageButton) findViewById(R.id.imagebtn2);
imagebtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(Scrape.this, MainActivity.class);
startActivity(intent);
}
});
This is my code, the MainActivity is the same code just different names. Below is my manifest.
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Scrape"
android:label="#string/app_name1"
android:theme="#style/AppTheme.NoActionBar" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</activity>
This is my xml.
<ImageButton
android:id="#+id/imagebtn1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginLeft="15dp"
android:src="#drawable/got" />
And the second image buttons xml
If its on line with setOnClickListener then its NullPointerException because the IDs are different and you will get null in imageBtn2.
If you have debugger, the exception would help a lot .. thats why the exceptions are there!

Android Splash Screen to FragmentActivity

Is it possible to open a FragmentActivity after Splash screen?
I want to open the FragmentActivity / Fragment after the Splash screen but I get following Error:
Unable to find explicit activity class {com.**********.MainActivity};
have you declared this activity in your AndroidManifest.xml?
My splash screen:
public class SplashScreen extends Activity {
private final int SPLASH_DISPLAY_LENGTH = 3000;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent mainIntent = new Intent(SplashScreen.this,Juli.class);
SplashScreen.this.startActivity(mainIntent);
SplashScreen.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
Manifest:
<activity
android:name="com.test.tab.SplashScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In Manifest add:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- Splash screen -->
<activity
android:name="com.test.tab.SplashScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name="com.test.tab.Juli"
android:label="#string/app_name" >
</activity>
</application>

New Intent not Starting on button click

On the app I am making, on the home menu screen that you get to after the splash screen, when you click one of the buttons, nothing happens. I don't know what the problem is?
Here is the src file, it implements View.OnClickListener:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bPlay:
Intent ourIntentPlay = new Intent(PartyActivity.this, Play.class);
startActivity(ourIntentPlay);
break;
case R.id.bFacts:
Intent ourIntentFacts = new Intent(PartyActivity.this, Facts.class);
startActivity(ourIntentFacts);
break;
case R.id.bInfo:
Intent ourIntentInfo = new Intent(PartyActivity.this, Info.class);
startActivity(ourIntentInfo);
break;
case R.id.bHelp:
Intent ourIntentHelp = new Intent(PartyActivity.this, Help.class);
startActivity(ourIntentHelp);
break;
}
}
And here is the manifest, inside the application tags:
<activity
android:name=".Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PartyActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="w.m.PARTYACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Info"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="w.m.INFO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Play"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="w.m.PLAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Help"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="w.m.HELP" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Thanks
Are you assigning a listener to your buttons? Do this in your onCreate() method:
findViewById(R.id.bPlay).setOnClickListener(clickListener);
If your activity implements OnClickListener, then clickListener will be this. Do this for all of your buttons.

Categories