After learning with youtube how to change layout, how to put Buttons, EditText, TextViews in the MainActivity, I have tried the simplest way of starting the new activity. when I run the activity I see the button with the text "open activity 2". I can click it. and ... nothing happens. I should be able of viewing the layout of activity_second with the TextView = "Activity 2". Instead of that I am able of click on the button all the time but not able to see "Activity 2" In this website I have learnt that the problem could be the intent, so I changed: Intent intent = new Intent(this, SecondActivity.class) by Intent intent = new Intent (MainActivity.this, SecondActivity.class) I even thought of using getBaseContext instead, but not done yet. I believe the problem could be that we need in the SecondActivity.java callling somehow the TextView Theoretically, calling the layout activity_second.xml should be enough, because it will appear the android:text="Activity 2" Another suspicious is that I am not using Android Studio and not using AppCompatActivity but copying my code from pages that use them. Instead I am using only MainActivity extends Activity and maybe I am overseen something like casting the widget or something similar. My question is do you see some type. I have overcome already some "cannot find symbol" like typos in my id which the compiler does not find or forgetting to import some widget or putting a comma instead of a dot. I am using emacs as an editor and compiling the code using this generous blog. I have installed openjdk, javac, and basic android tools I have simplified the code at maximum, using as a base https://developer.android.com/training/basics/firstapp/starting-activity and a youtube of channel code in flow. Here is my code, where is the typo? p.S: it is my first post and the code seems not to be correctly formatted, hopefully you are able to read it.
MainActivity.java
package net.otro.abrir1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.util.Log;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
openActivity2();
}
});
}
/** Called when the user taps the Send button */
public void openActivity2(){
// Do something in response to button
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
SecondActivity.java
package net.otro.abrir1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.view.View;
import android.widget.EditText;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="open activity 2"
android:id="@+id/button"/>
</RelativeLayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview"
android:text="Activity 2"/>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="net.otro.abrir1"
versionCode="1"
versionName="0.1">
<uses-sdk android:minSdkVersion="16"/>
<application android:label="Abrir1">
<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=".SecondActivity"
android:parentActivityName=".MainActivity">
<!--android:label="@string/activity2_name" -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest>
The question was: Where is the typo. The answer is there is no typo, but I was trying to compile with this command:
javac -source 1.7 -target 1.7 -bootclasspath "${JAVA_HOME}"/jre/lib/rt.j$ \
-classpath "${PLATFORM}/android.jar" -d build/obj \
build/gen/com/probando/intento1/R.java \
"${PROJ}"/src/com/probando/intento1/MainActivity.java
As it is to be
javac -source 1.7 -target 1.7 -bootclasspath "${JAVA_HOME}"/jre/lib/rt.j$ \
-classpath "${PLATFORM}/android.jar" -d build/obj \
build/gen/com/probando/intento1/R.java \
"${PROJ}"/src/com/probando/intento1/*.java
I was expecting that the compilation of MainActivity.java will be enough to call SecondActivity.java but it is not. It has to be first compiled into "R" R.java is a file with the same information that your file MainActivity.java and SecondActivity. java, but instead of variables, you can see memory addresses.
Comment1: The title reflected my suspicous that the AppCompatActivity could be the root-cause, but it is not. AppCompatActivity is only a setting included in higher versions of Android. In order to use it you need to specify it in your import section in the heading of your java file Comment2: I recommend this site: https://www.hanshq.net/command-line-android.html to avoid the use of Android Studio. With only 3 files and a couple of lines of code you have your first app. Later on, you only need to modify it. Comment3: the guide above is for 64 bit and version 25.0.0. If you happen to have 32 bit and version 19.0.0, it might help to make a small change: For 64 bit:
$ "${BUILD_TOOLS}/zipalign" -f -p 4 \
build/Hello.unsigned.apk build/Hello.aligned.apk
For 32 bit:
$ "${BUILD_TOOLS}/zipalign" -f 4 \
build/Hello.unsigned.apk build/Hello.aligned.apk
For some reason Android stopped to support 32 bit in higher versions.
You need to delete the flag -p, which is not recognized for some reason.
Comment4: My thanks to Hans, the author of the website above, for the wonderful guide for those who for, any reason, do not want to use Android Studio In my case Android Studio was running in a 4 RAM windows machine so slow, that I did not dare to install it in my Centos 7 with 2,5 RAM.
The command line is really an option to Android Studio for those who cannot afford it.