Reputation: 11
I am a beginner using Android Studio to Create an application.
On my homepage/ActivityMain I wish to put a textView that when clicked, will launch into another RelativeLayout activity.
I have attempted to do this using an intent, but the application crashes when the textbox is clicked. Please note that for all code, statements which I believe to be irrelevant were deleted (import, bundle) Below is my code for activity_main.xml:
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="256dp"
android:layout_margin="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Organelles" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/membrane"
android:text="Cell Membrane"
android:onClick="explainCellMembrane" />
Below is my code for MainActivity.java containing the code containing my intent:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void explainCellMembrane(View view) {
Intent myIntent = new Intent(MainActivity.this, MembraneActivity.class);
MainActivity.this.startActivity(myIntent);
}
}
Below is the code for the Class(MembraneActivity) launched by the intent:
package com.example.android.cellularbiology;
public class MembraneActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_membrane);
}
}
Finally is my code for the layout of my custom Java Class:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Membrane Explanation Page"/>
</LinearLayout>
In AndroidStudio there are no errors (red underline) When the app crashes it does not show error, just says that it has stopped.
How do I fix my intent? Or should another method be used?
Thanks for your help.
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.cellularbiology">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logs:
01/03 00:06:12: Launching app
No apk changes detected since last installation, skipping installation of /Users/FideronTSANG/Desktop/CellularBiology/app/build/outputs/apk/app-debug.apk
$ adb shell am force-stop com.example.android.cellularbiology
$ adb shell am start -n "com.example.android.cellularbiology/com.example.android.cellularbiology.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Waiting for process to come online
Connected to process 24150 on device samsung-sm_c9000-b9dec0e1
Application terminated.
Conclusion: Thankyou everyone for answering and helping out a noob! You completely saved my project! I hope you have a nice day :)
The intent worked after I added
<activity
android:name = ".MembraneActivity">
</activity>
To my android manifest.
I also used the improved intent provided by
public void explainCellMembrane(View view) {
startActivity(new Intent(MainActivity.this, MembraneActivity.class));
}
Upvotes: 0
Views: 131
Reputation: 120
I think, there is no problem with your Intent
. And the problem is you are missing to add your MembraneActivity
in the Manifest
file, that will go under <activity><activity/>
tag. For example,
<activity
android:name = ".MembraneActivity">
</activity>
Whenever you add a new Activity
in your app, you must have to add the Activity
name in the Manifest
file. Otherwise you app will not recognize the Activity
you are trying to start through the Intent
myIntent
.
Now after adding the MembraneActivity
in the Manifest
file, it will be like this,
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.cellularbiology">
<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=".MainActivity">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--your missing activity added here-->
<activity
android:name = ".MembraneActivity">
</activity>
</application>
</manifest>
Upvotes: 0
Reputation: 3044
You need to add the class to your manifest.
Add the following...
<activity android:name=".MembraneActivity"></activity>
This will go under the </activity>
tag.
Upvotes: 0
Reputation: 3055
First of all replace your explainCellMembrane
method with this:
public void explainCellMembrane(View view) {
startActivity(new Intent(MainActivity.this, MembraneActivity.class));
}
Second, Try Cleaning and Rebuilding your project and then share your logCat
in the question.
EDIT:
After checking your logCat
I can see the error is not in your logCat
but it is in the device so try deleting the application in your emulator or mobile device and then install it again through Android-Studio, this works for me too as it often occurs randomly.
Upvotes: 1