Android Studio Two Buttons to Two Pages - java

Good morning,
Quick coding inquiry I'd like to put forth.
I've gotten an assignment to produce an application on Android Studio that has two buttons that should each load new pages when clicked.
The chapter from our books explains how to do this with one button, but I assume there might be a different set up with two buttons?
Anyway, this is only the second assignment in our class so I'm not very far in understanding how all of this works; that and the class is an Independent Study course so I'm teaching myself. Not always the easiest thing.
Anyway, here's what I do have:
MainActivity.java
package com.example.thelatestmusicscene;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.buttonOne);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MusicNewsOne.class));
}
});
Button button2 = (Button) findViewById(R.id.buttonTwo);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MusicNewsTwo.class));
}
});
}
}
I believe this is the only file that's relevant to my problem but I can post the others if you need to see them too.
The first button functions perfectly but when I try to use the second button the app stops working. I've seen a few explanations online, but the few I've found about loading other pages gives me a bunch of errors in my code.
Note: This is how we were shown to do it from the book, but I'm all for alternative methods.
Everything else for this application is done, it's just this linking to the second java page I'm faltering on. If you know a better way to lay out the code I'm all ears or if you could link me to a resource that could teach me, I'd be grateful.
OK, this is going to be the confusing part. When I downloaded Android Studio for the class I could never get any of the emulators to run. This was a problem I also posted, which as of today, has still not received an answer. If you have a solution to that or want to see those details of what I’ve tried, go here:
https://superuser.com/questions/1394568/android-studio-and-haxm-installation
Ultimately, I could write code, but I couldn’t test it. However, I found a way to run the apps on my own smartphone to test if they’re functioning. Letting Android Studio build the APK file and then letting my phone run the app.
Now…as for errors. There are none. Android Studio isn’t coming back with any. As far as it's concerned, everything is fine. When I load the application on my phone and I click the second button, the application closes, and I get “Unfortunately, nameOfApp has stopped.”
And that’s where I find myself.
Here’s the XML (I assume you mean the AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thelatestmusicscene">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MusicNewsTwo">
</activity>
<activity android:name=".MusicNewsOne" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I've got to get some sleep now but I'll try retrieving a crash report from my phone today if I can.

Brother your MainActivity.java file have correct code.
Kindly check button name in xml and java file, and check your manifest file either MusicNewsTwo added as activity.
And be sure you have both java and xml file in correct place.

Few notes
You may need to provide the crash's log/stacktrace/message (for us to know what's the solution for it)
You may also need to provide us the layout or the .xml file
What could possibly went wrong
One of the common problems encountered:
Assuming you have only copied it from your class, you might have encountered the error saying that your MusicNewsTwo.class is not declared in the AndroidManifest.xml, do this:
What could be the possible solution
<application
android:label="Example APp"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="package.path.to.MusicNewsOne">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Add this activity if missing -->
<activity
android:name="package.path.to.MusicNewsTwo"/>
</application>
I will update my answer so as long as you take into consideration the Few notes section of my answer, otherwise if I got your answer write already with this one, comment out.

Found the problem within my phone itself. Turns out it was a permission error for downloading 3rd party apps; my code was actually perfect. Thanks for all the suggestions though.

I think the IDs of the views in the layout : "activity_main" are different than the ones you are using in the MainActivity.java file

Bro there are two ways to do this, start a new Activity like you are doing, and other is By Using one activity you can post the fragments on the same activity as much as you want i.e 1 ,2 3 and so on.here is the link for fragments, but you are very much biggner ill suggest you to start with the actitvity and learn about the Activity life cycle. you can find here . moreover tell me what the error you are getting, To find the error see a the bottom of android studio the option (Logcat) click on it . and see the error and past it in the comment.

Related

How to make the Android app load into logo then back to main activity?

I am new to App Development and I've been studying Kotlin for only a month now (1 hour everyday). I got the grasp of the functions but I still haven't gotten around using them for the purposes I have in mind.
Using Android Studio, I am trying to make the App load into a logo upon opening the app(Just like Facebook, Reddit), the logo is animated (which is not a problem for me). I have a couple of ways to achieve this but I wanna see what's the most efficient way to do it.
Create a new SplashActivity and change your starting activity in you manifest file like this:
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
then in your SplashActivity's OnCreate, start your MainActivity
startActivity(Intent(this, MainActivity::class.java))
You can read more about this here.
You can create one Activity let's say SplashActivity which will have your logo and animations you want. Then from that Activity you can start your MainActivity. If you need to process some data inside your SplashActivity you do that then you start MainActivity. Otherwise, if you don't have any data to process you can simply start MainActivity with some delay, like this:
Handler().postDelayed({
val i = Intent(this, MainActivity::class.java)
startActivity(i)
}, 5000)
This will start your MainActivity after 5000 milliseconds which is 5 seconds. Now try to write some code and if you run into a problem try to find a solution or ask again if you can't. There is a lot of tutorials on the Internet you can find on how to do this.

In android getting white screen before activity when first time app start on phone

Here is my source code of launcher activity.
I search a lot on stack overflow but nothing getting good answer
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_lang);
init();
}
private void init(){
spnLanguage = (Spinner)findViewById(R.id.spnLanguage);
btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
}
Android apps do take some amount of time to start up, especially on a cold start. There is a delay there that you may not be able to avoid
https://www.bignerdranch.com/blog/splash-screens-the-right-way/
In the AndroidManifest file of your starting Activity, mention a transparent Theme
<activity
android:name="Your activity name"
android:theme="#android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
In my case delay was bit more. After searching a lot I found that it is because of android studio auto run feature. After disabling the auto run issue is solved.
I had a similar problem some time ago.
The activity is displayed to the user when it reaches the onStart() method. Your init() seems to take some time so until it finishes, you will only see the empty activity.
You could either trigger all heavy background work (that is not needed for the initial GUI setup) in a later method or first call another activity acting as splash screen if you have to keep all content of init() where it is.
I had also same issue please find below the question as well as the resolution:
White screen is displayed first time on gradle build.

Android start activity in different java package

I'm building an Android application that needs to have customer specific code. Customer specific code needs to be separated from the actual Android product our company supplies.
To do this I tried to create 2 packages:
com.company.product.activity
com.company.product.customcode.activity
Both packages contain ExampleActivity.
I have written a factory that uses reflection to determine of a custom component exists on top of the product class. This works fine.
Using the following code to start the ExampleActivity of product works:
Intent intent = new Intent(this, com.company.product.activity.ExampleActivity.class);
startActivity(intent);
Using the following code to start the ExampleActivity of customcode fails:
Intent intent = new Intent(this, com.company.product.customcode.activity.ExampleActivity.class);
startActivity(intent);
Error:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.product.customername/com.company.product.customcode.activity.ExampleActivity}: java.lang.IllegalArgumentException: AppIndex: The URI host must match the package name and follow the format (android-app://<package_name>/<scheme>/[host_path]). Provided URI: android-app://com.company.product.customcode.activity/http/host/path
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
I also tried this code, but then Android gives a Toast that it cannot find the Activity:
Intent intent = new Intent();
intent.setClassName("com.company.product.customcode.activity", "com.company.product.customcode.activity.ExampleActivity");
startActivity(intent);
Manifest:
<activity android:name="com.company.product.activity.ExampleActivity"
android:label="#string/app_name"
android:noHistory="false"
/>
//Custom implementation of the ExampleActivity
<activity android:name="com.company.product.customcode.activity.ExampleActivity"
android:label="#string/app_name"
android:noHistory="false"
/>
Does anybody have any ideas or tips how to achieve the maingoal: To split custom code from the productcode where activity names might be equal.
I am not sure what you missed here. I have tried this and it worked.
I have created a class MainActivity under package com.sample.so_sample.activities
and another MainActivity under package com.sample.so_sample1.test.activities
My manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.so_sample">
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.sample.so_sample1.test.activities.MainActivity">
</activity>
</application>
</manifest>
and my call to navigate in com.sample.so_sample.activities.MainActivity is
Intent i = new Intent(this, com.sample.so_sample1.test.activities.MainActivity.class);
startActivity(i);
There is nothing wrong in the code.I had tried the same,it seems no issue.
Differen package names is the right way, it has to work fine, seem like problem is out of this code.
Your code can be in any java packages, you need just specify fully qulified name of activity in manifest. Android package name it's just a unique app id string.
instead of:
Intent intent = new Intent();
intent.setClassName("com.company.product.customcode.activity", "com.company.product.customcode.activity.ExampleActivity");
startActivity(intent);
try:
Intent intent = new Intent();
intent.setClassName("YOUR PACKAGE NAME IN MANIFEST", "com.company.product.customcode.activity.ExampleActivity");
startActivity(intent);
I've solved this question.
It seemed by further investigation that the Activity crashed because of some auto-generated code that was added while copy-pasting....
It seemed like a crash before the Activity was loaded, but this apparently was not the case.
Thank you all for you're answers, they were very useful!

Exception: ActivityNotFoundException - different name works flawless

I'm building an AndroidApp which uses a java-class Question.java which extends android.app.Activity.
The Activity is stated in the androidmanifest.xml as
<activity
android:name=".gui.android.Question"
android:label="#string/question_activity" >
</activity>
While using this version I get an ActivityNotFoundException: Unable to find explicit activity class everytime I try to start this activity.
However, after hours of not getting any further i tried to make a new Java-Class QuestionFix.java plus a new Activity
<activity
android:name=".gui.android.QuestionFix"
android:label="#string/question_activity" >
</activity>
The code of Question.java and QuestionFix.java and calls are 100% equal except the startActivity-calls.
Using the QuestionFix.java-Class causes no problem at all and everything works as expected.
I tried deleting Question.javaand then replacing it QuestionFix.java but the error persisted. Using a FQN in the androidmanifest.xml didn't help.
Is someone able to tell me how I can fix this?

Setting up an Android Activity within Titanium

I am attempting to enable Bluetooth on an Android device. I have read to Android documentation and have a pretty good idea of what the process is. I am, however, rather stuck at actually firing off an Activity using the manifest file. This is what I've done so far...
I've developed an Android Module with a couple of classes:
BluetoothModule // extends KrollModule
BluetoothSetup // extends Activity
In BluetoothSetup, the onCreate method looks like:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
connectBluetooth();
}
Also in BluetoothSetup, the connectBluetooth() method looks like:
protected void connectBluetooth(){
// if statements to check if bluetooth is enabled/avail removed
Intent intentBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intentBluetooth, 0);
}
}
Finally, in the module's timodule.xml I've added:
<activity android:label="#string/app_name" android:name="com.eyesore.bluetooth2.BluetoothSetup">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
The module compiles just fine, but does not actually do anything. I fear that I've missed a fundamental step here, but I'm just not sure what it is. Any advice would be greatly appreciated!
Figured this out. Wound up creating a custom per instructions here:
https://wiki.appcelerator.org/display/guides/Maintaining+a+Custom+AndroidManifest.xml
I removed the extra code Titanium drops in the manifest file and it seems to be working.

Categories