starting a new activity causing app crash - android

Please help me sorting out where I am wrong. Because when I start a new activity using a button click the app crashes.
I am unable to figure out. I used another Acitvity from the same application and it launched successfully. But this nelwy created activity isn't starting in any ways.
My codes are:
[secondscreen.java]
package org......android.activities;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org......android.R;
public class secondscreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondscreen);
TextView myAwesomeTextView = (TextView)findViewById(R.id.myAwesomeTextView);
myAwesomeTextView.setText("brown fox");
}
}
[activity_secondscreen.xml]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.secondscreen">
<TextView
android:id="#+id/myAwesomeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="secondscreen activity"
android:textSize="34sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
[starting activity code in a first activity]
buttonTraining = findViewById(R.id.buttonTraining);
buttonTraining.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"going to second activity...",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),secondscreen.class);
startActivity(intent);
}
});
[partial stacktrace]
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

Worked. After Changed
From
<activity
android:name=".activities.secondscreen"
android:exported="false"/>
to
<activity
android:name=".activities.secondscreen"
android:exported="false"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"/>

Related

Android Studio- How To Delete Recent App List Programmatically After Application Closed

when I close my application I want to remove it from the "recent app list" also in android.
I created an empty project and activity.
Here my main XML file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rebirthing.clear_recent_list_app.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:onClick="closeApp"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Here My MainActivity.java file:
`package com.example.rebirthing.clear_recent_list_app;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void closeApp(View v){
finish();
}
}
`
When I click the TextView which inside of my Xml File. It closes my App. but when I Check "Recent App List" its still there. How do I fix it?
if you want to do something with your app when you close your application.
then use this method
protected void onDestroy() {
super.onDestroy();
//do what you want to do here
}
this method is called when you close your application.
if you find this answer useful then please mark it correct.

App chrashes while changing view

I want to create a very simple app. The app should consists of 2 views. In both views are one button. By clicking the button the view should change.
Here is my folder structure
I have two activitys and two activity_layouts. As you can see OverviewActivity isn't inside the activity folder. When I place it into the folder I get this structur:
Why is the activity folder away? Can you give me a little explanation?
Ok, but in this question I use the first folder structur.
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="company.useradministration.activity.LoginActivity">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="btnOverview"
android:text="go to overview"/>
</RelativeLayout>
activity_overview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".activity.OverviewActivity">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="btnLogin"
android:text="go to login"/>
</RelativeLayout>
LoginActivity
package company.useradministration.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import company.useradministration.R;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void btnOverview(View view){
setContentView(R.layout.activity_overview);
}
}
OverviewActivity
package company.useradministration.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import company.useradministration.R;
public class OverviewActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
}
public void btnLogin(View view){
setContentView(R.layout.activity_login);
}
}
Okay. When I start the app, I see a button with the text "go to view". After pressing this button, the view changed. Now I see a button with the text "go to overview". When I press this button, the app crashes with the error:
java.lang.IllegalStateException: Could not find method btnOverview(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18439)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5085)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
UPDATE:
Here is the explained answer:
You have a activity_overview.xml with it's class OverviewActivity.
In your activity_overview.xml you set the android:onClick="btnLogin" to the button and you have:
public void btnLogin(View view){
setContentView(R.layout.activity_login);
}
in your OverviewActivity so when user clicks this button it changes the contentview successfully.
The question here is:
Why does the app crashes when you click on the button in activity_login.xml to change the contentview back to activity_overview.xml?
Simple. Because you have set the onclick method in your LoginActivity but you aren't switching to that activity but just changing the layout. So when the button to go back to overview content is clicked the app searches for the function in you OverviewActivity because the activity didn't swithed to LoginActivity. It has just changed the content and there is no function called btnOverview in your OverviewActivity. So here you have 2 solutions:
1- Create two RelativeLayouts in just one xml file and than setVisibility of each layouts on button click.
So for that you have to:
Create only one activity instead of two different and in it's xml file use this code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/overview"
android:visibility="gone">
<Button
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="go to login"
android:id="#+id/go_to_login"
android:layout_height="wrap_content" />
//Do your stuff here for overview content
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/login">
<Button
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="go to overview"
android:id="#+id/go_to_overview"
android:layout_height="wrap_content" />
//Do your stuff here for login content
</RelativeLayout>
</RelativeLayout>
This code has 2 RelativeLayouts. The first one with id:overview is of the overview content and it's visibility is gone and the second RelativeLayout with id:login has the visibility visible by default (Change the two layouts visibility as desired).
Than in your MainActivty.java (call it as you wish) you can change the visibility of these views like this:
public class MainActivity extends AppCompatActivity {
Button goToOverview, goToLogin;
RelativeLayout overview, login;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remember to change the content to match your xml file name
setContentView(R.layout.activity_main);
//Buttons
goToOverview = (Button) findViewById(R.id.go_to_overview);
goToLogin = (Button) findViewById(R.id.go_to_login);
//Layouts
overview = (RelativeLayout) findViewById(R.id.overview);
login = (RelativeLayout) findViewById(R.id.login);
goToOverview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
login.setVisibility(View.GONE);
overview.setVisibility(View.VISIBLE);
}
});
goToLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
overview.setVisibility(View.GONE);
login.setVisibility(View.VISIBLE);
}
});
}
In this way you can switch to two different layout without any problem.
Edit: You can achieve this also with this solution but the difference between the first solution and this solution is just that you will have 2 xml files instead of one.
2nd- You will have one java class file (in this example I will take OverviewActivityas example) and 2 different layout files (Which you already have: activity_login.xml and activity_overview.xml).
So in your activity_overview.xml change this line: tools:context=".activity.OverviewActivity" to tools:context=".OverviewActivity" as you said that you are using the first folder structure where the OverviewActivity file is out of the activity folder.
Than in your OverviewActivity make the following changes:
public class OverviewActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
}
public void btnLogin(View view){
setContentView(R.layout.activity_login);
}
public void btnOverview(View view){
setContentView(R.layout.activity_overview);
}
}
That's it. Please comment if you are having any problem with the code. I will be glad to help your further more.
Hope this can solve your issue.
Regards
I think this is your problem:
public void btnOverview(View view){
setContentView(R.layout.activity_overview);
}
This changes the layout for the current activity LoginActivity to activity_overview which contains the button thats looking for the btnLogin method
You are successfully changing the layout, but your staying in the same activity, so the new layout is looking for a method that exists in the other activity
Instead change the above code to this:
public void btnOverview(View view){
Intent intent = new Intent(this, OverviewActivity.class);
startActivity(intent);
}
And in OverviewActivity
public void btnLogin(View view){
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
I think you can use this as a help. I'm newbie myself to android java programming, but I believe you have to create intent to move to another activity. Additionally be sure to add your Activity to AndroidManifest.xml. That's all I think. If I'm wrong I'll be happy if someone more experienced will correct me :)

when i change my button to an icon i can't click on it to go to a new layout

this my xml file main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#raw/christmas"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/by_kostas"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="83dp"
android:text="#string/by_kostas"
android:textSize="20sp"
android:textColor="#F2F3F4"/>
<ImageView
android:id="#+id/settings"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="nameOfMethod"
android:src="#raw/settings" />
</RelativeLayout>
and my java Main
package com.kostas.mytorch;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//start our layout
super.onCreate(savedInstanceState);
setContentView(R.layout.main);{
final ImageView diskView = (ImageView) findViewById(R.id.settings);
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//my codes
}
});
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// System.out.println("image clicked...");//in my logcat
startActivity(new Intent("com.kostas.standroid.settings"));
}
});
}
}
my problem is when i click in the settings icon, my program crashes instead of what i wanted to create a new layout (settings xml is just black page),can someone be kind enough to help me out
You can attach click listener to any view by two ways:
In xml, write onClick attribute of the view and pass the method
name you have implemented in the activity.
Example: android:onClick="someMethod" and in the activity code, declare a method
public void someMethod(View view)
{
// handle click here
}
In activity, use setOnClickListener() to the view.
For now, try this:
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// System.out.println("image clicked...");//in my logcat
startActivity(new Intent(Main.this, settings.class));
}
});
Also remove this line from xml: android:onClick="nameOfMethod"
Some time there is contextual problem. Try this.
Intent intent = new Intent(CurrentActivity.this, UpcomingActivity.class);
startActivity(intent);
Also don't forget to define class in Manifest file
try to remove
android:onClick="nameOfMethod"
from the xml layout. I believe this is the method that it's called when you click the settings button and it doesn't exist so it crashes.

How to make android buttons switch different XML layouts?

I just started some android programming recently and I am creating a small project app.
I have a few different XML Layout files and want my button to go to that layout. However, when I press the button on the emulator I get this message "Unfortunately "MyApp" Has stopped".
Here is my Main Activity Class:
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Shows the Activities screen.
Button Button1=(Button)findViewById(R.id.Button1);
Button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,ActivityList.class);
startActivity(i);
}
});
}
This is my XML CODE for the Button:
android:id="#+id/Button1"
android:layout_width="300dp"
android:layout_height="55dp"
android:layout_alignLeft="#+id/Menutxt"
android:layout_below="#+id/Menutxt"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="#string/Choose_Activity"
android:textColor="#ffffffff"
android:textSize="25sp"
android:typeface="serif"
I was wondering if anyone could help me out here, been trying to do this for a few hours and cannot get past it. Thanks.
add this to the layout of the activity that you wish to open. it should go at the top: tools:context=".activity" >
e.g.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".activity" >
replace "activity" with the name of your activity
You did not show us your ActivityList.class , maybe you have created and extends Activity, but make sure that in your AndroidManifest.xml has this code:
activity android:name=".ActivityList"

Android: Cannot Start New Activity Using Intents

I am trying to launch a new activity when a button is pressed but nothing happens and I get no errors. Here is my code:
Main activity
public class CSLearn_Python_AppActivity extends Activity {
String tag = "Events";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
//get content from main.xml
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button login = (Button) findViewById(R.id.loginBtn);
login.setOnClickListener(new OnClickListener(){
public void onClick(View v){
// Intent intent = new Intent("com.Main.Verification");
// startActivity(intent);
Intent myIntent = new Intent(getBaseContext(), Verification.class);
startActivity(myIntent);
}
});
}
The new activity
import android.app.Activity;
import android.os.Bundle;
public class Verification extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.verification);
}
}
Verification XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/linearLayout1" android:layout_width="match_parent" android:baselineAligned="true" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:orientation="vertical">
<TextView android:text="#string/Verification" android:id="#+id/Verrification" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:layout_height="wrap_content" android:id="#+id/password" android:inputType="textPassword" android:layout_width="112dp">
<requestFocus></requestFocus>
</EditText>
<Button android:text="#string/LoginBtn" android:id="#+id/loginBtn" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
I added this to the android manifesto
<activity android:name=".Verification"
android:label="Verification">
<intent-filter>
<action android:name="com.Main.VERIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
If anyone can point me in the right direction it would be really appreciated.
Try the code below and register your activity in the AndroidManifest.xml file:
Intent myIntent = new Intent(CSLearn_Python_AppActivity.this, Verification.class);
startActivity(myIntent);
Is CSLearn_Python_AppActivity in the same package than Verification.
May be you can try in the Manifest with:
<activity android:name="yourpackage.Verification"
android:label="#string/verification" >
</activity>
The intent-filter in your Verification activity is not necessary.
I believe the problem lies in the getBaseContext(). Use getApplicationContext() instead (the activity's context is also an option but would cause a leak). I havent quite been able to wrap my head around the base context, but it seems to be some sort of proxy doing more or less nothing in its raw implementation.
A more through explanation of the different contexts is given here.
I would try to change it like this:
Button login = (Button) findViewById(R.id.loginBtn);
login.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Verification.class);
startActivity(myIntent);
}
});
try removing the intent filter, and everything in between, from your manifest
do you have all your imports?
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Intent myIntent = new Intent(CSLearn_Python_AppActivity.this, Verification.class);
startActivity(myIntent);
In your Android manifest file,
<activity android:name=".Verification" android:label="Verification"></activity>
You include the other activity in Manifest file:
Intent intent = new Intent(First.this, Second.class);intent.putExtra("userData",registeredUsersData);startActivity(intent);

Categories

Resources