简体   繁体   中英

App stops when launching a new activity

I've been trying to launch a new activity in my app and, although it does for a moment, inmediately after opening the new screen, the app stops. I've already added the new actvity to the manifest. This is the method I use to call to the new activity, whuich worked perfectly when it only had the toast in the clickListener:

public void Bs()
    {
        View.OnClickListener listSet = new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();
                Intent intent= new Intent(MainActivity.this, Set.class);
                startActivity(intent);
            }
        };
        b= (ImageButton) findViewById(R.id.imageButton7);
        b.setOnClickListener(listSet);
    }

This is the class that is being called:

public class Set extends Activity 
{
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
    }
}

And this is the layout class setting that only has a textview (I've tried several layouts and nne of them work, so I guess this is no the problem but I add it anyway in case it helps):

?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:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="0dp"
        android:text="@string/toolbar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

These are the errors I have in the logcat:

 --------- beginning of crash
07-23 07:10:04.022 3267-2868/? A/google-breakpad: -----BEGIN BREAKPAD MICRODUMP-----
07-23 07:10:04.022 3267-2868/? A/google-breakpad: V WebView:51.0.2704.90

(...)

    07-23 07:10:11.548 2202-2891/? E/SystemUpdateService: Failed to call RecoverySystem.cancelScheduledUpdate
                                                          java.lang.reflect.InvocationTargetException
                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                              at xae.c(:com.google.android.gms:134)
                                                              at afuw.d(:com.google.android.gms:195)
                                                              at afuw.p(:com.google.android.gms:2178)
                                                              at afuw.a(:com.google.android.gms:448)
                                                              at afuw.doInBackground(:com.google.android.gms:50475)
                                                              at android.os.AsyncTask$2.call(AsyncTask.java:304)
                                                              at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                              at java.lang.Thread.run(Thread.java:761)
                                                           Caused by: java.io.IOException: cancel scheduled update failed
                                                              at android.os.RecoverySystem.cancelScheduledUpdate(RecoverySystem.java:555)
                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                              at xae.c(:com.google.android.gms:134) 
                                                              at afuw.d(:com.google.android.gms:195) 
                                                              at afuw.p(:com.google.android.gms:2178) 
                                                              at afuw.a(:com.google.android.gms:448) 
                                                              at afuw.doInBackground(:com.google.android.gms:50475) 
                                                              at android.os.AsyncTask$2.call(AsyncTask.java:304) 
                                                              at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                                                              at java.lang.Thread.run(Thread.java:761) 
    07-23 07:10:12.453 2202-3333/? W/art: Verification of com.google.android.gms.common.data.DataHolder[] com.google.android.gms.games.broker.AppContentAgent.loadCardStream$3489344c(com.google.android.gms.games.broker.GamesClientContext, com.google.android.gms.games.broker.AppContentContext, long) took 841.459ms
    07-23 07:10:12.592 2202-2891/? E/SystemUpdateTask: exception trying to cancel scheduled update
                                                       java.io.IOException: Failed to invoke RecoverySystem.cancelScheduledUpdate
                                                           at xae.c(:com.google.android.gms:140)
                                                           at afuw.d(:com.google.android.gms:195)
                                                           at afuw.p(:com.google.android.gms:2178)
                                                           at afuw.a(:com.google.android.gms:448)
                                                           at afuw.doInBackground(:com.google.android.gms:50475)
                                                           at android.os.AsyncTask$2.call(AsyncTask.java:304)
                                                           at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                           at java.lang.Thread.run(Thread.java:761)


                                                       [ 07-23 07:10:12.629  2443: 2488 D/         ]
                                                       HostConnection::get() New Host Connection established 0x895a1900, tid 2488
(...)
07-23 07:10:44.198 1534-2908/? E/RecoverySystemService: Timed out connecting to uncrypt socket
07-23 07:10:44.198 1534-2908/? E/RecoverySystemService: Failed to connect to uncrypt socket
07-23 07:10:44.201 2202-3376/? E/SystemUpdateService: Failed to call RecoverySystem.cancelScheduledUpdate
                                                      java.lang.reflect.InvocationTargetException
                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                          at xae.c(:com.google.android.gms:134)
                                                          at afuw.d(:com.google.android.gms:195)
                                                          at afuw.p(:com.google.android.gms:2178)
                                                          at afuw.a(:com.google.android.gms:448)
                                                          at afuw.doInBackground(:com.google.android.gms:50475)
                                                          at android.os.AsyncTask$2.call(AsyncTask.java:304)
                                                          at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                          at java.lang.Thread.run(Thread.java:761)
                                                       Caused by: java.io.IOException: cancel scheduled update failed
                                                          at android.os.RecoverySystem.cancelScheduledUpdate(RecoverySystem.java:555)
                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                          at xae.c(:com.google.android.gms:134) 
                                                          at afuw.d(:com.google.android.gms:195) 
                                                          at afuw.p(:com.google.android.gms:2178) 
                                                          at afuw.a(:com.google.android.gms:448) 
                                                          at afuw.doInBackground(:com.google.android.gms:50475) 
                                                          at android.os.AsyncTask$2.call(AsyncTask.java:304) 
                                                          at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                                                          at java.lang.Thread.run(Thread.java:761) 
07-23 07:10:44.201 2202-3376/? E/SystemUpdateTask: exception trying to cancel scheduled update
                                                   java.io.IOException: Failed to invoke RecoverySystem.cancelScheduledUpdate
                                                       at xae.c(:com.google.android.gms:140)
                                                       at afuw.d(:com.google.android.gms:195)
                                                       at afuw.p(:com.google.android.gms:2178)
                                                       at afuw.a(:com.google.android.gms:448)
                                                       at afuw.doInBackground(:com.google.android.gms:50475)
                                                       at android.os.AsyncTask$2.call(AsyncTask.java:304)
                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                       at java.lang.Thread.run(Thread.java:761)

Am I doing something wrong when calling the new activity? Or is it the class I'm calling the one isn't fine?

EDIT This is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xx.yy">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:debuggable="false"
        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="com.xx.yy.MainActivity"
                android:configChanges= "orientation|screenSize"
                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="com.xx.yy.Set"
            android:screenOrientation="portrait">
        </activity>
    </application>
</manifest>

I have been doing some more attempts and I think the problem was I had fullScreen on the first activity but not on the second one. Therefore, adding these two lines to the Set class solved the problem:

public class Set extends Activity
{
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.settings);
    }
}

Can you share your MANIFEST file here

I tried the similar kind of code you were written, Check the working code below.

MainActivity.java

package com.service.rajeshm.activitytest;

import android.content.Intent;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View.OnClickListener listSet = new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();
                Intent intent= new Intent(MainActivity.this, Set.class);
                startActivity(intent);
            }
        };
        ImageButton b= (ImageButton) findViewById(R.id.imageButton1);
        b.setOnClickListener(listSet);
    }
}

activity_main.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:id="@+id/activity_main"
    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="com.service.rajeshm.activitytest.MainActivity">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginTop="17dp"
        android:id="@+id/imageButton1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

Set.java

package com.service.rajeshm.activitytest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Set extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
    }
}

activity_set.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:id="@+id/activity_set"
    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="com.service.rajeshm.activitytest.Set">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@android:drawable/btn_star_big_on"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="64dp"
        android:layout_marginStart="64dp"
        android:layout_marginTop="48dp"
        android:id="@+id/imageView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="32dp"
        android:layout_marginEnd="32dp" />
</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.service.rajeshm.activitytest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>
        <activity android:name=".Set"></activity>
    </application>

</manifest>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM