重定向到新活动时,Android应用程序崩溃

时间:2016-10-02 13:28:37

标签: java android

我对android很新,我创建了一个基本活动的应用程序然后添加了一个按钮来指向另一个活动,它工作但是每当我点击按钮转到新的活动页面时,应用程序就会崩溃

我收到以下错误:

10/02 15:22:04: Launching app
$ adb push C:\Users\Ahmed\AndroidStudioProjects\Shiftind\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.shiftind.www.shiftind
$ adb shell pm install -r "/data/local/tmp/com.shiftind.www.shiftind"
Success


$ adb shell am start -n "com.shiftind.www.shiftind/com.shiftind.www.shiftind.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 2284 on device emulator-5554
W/System: ClassLoader referenced unknown path: /data/app/com.shiftind.www.shiftind-2/lib/x86
I/InstantRun: Instant Run Runtime started. Android package is com.shiftind.www.shiftind, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/com.shiftind.www.shiftind-2/lib/x86
W/art: Verification of android.support.v4.media.session.MediaControllerCompat android.support.v4.app.FragmentActivity.getSupportMediaController() took 420.734ms
W/art: Verification of void android.support.v4.app.FragmentActivity.requestPermissionsFromFragment(android.support.v4.app.Fragment, java.lang.String[], int) took 107.527ms
W/art: Verification of void android.support.v4.app.FragmentActivity.setEnterSharedElementCallback(android.support.v4.app.SharedElementCallback) took 102.953ms
W/art: Verification of void android.support.v7.app.AppCompatActivity.onSupportActionModeFinished(android.support.v7.view.ActionMode) took 192.620ms
W/art: Verification of void android.support.v4.app.FragmentManagerImpl.setHWLayerAnimListenerIfAlpha(android.view.View, android.view.animation.Animation) took 130.794ms
W/art: Verification of android.support.v7.app.AppCompatDelegate android.support.v7.app.AppCompatDelegate.create(android.content.Context, android.view.Window, android.support.v7.app.AppCompatCallback) took 147.296ms
W/art: Verification of void android.support.v7.app.AppCompatDelegateImplBase.<init>(android.content.Context, android.view.Window, android.support.v7.app.AppCompatCallback) took 138.730ms
W/art: Verification of void android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor() took 166.867ms
W/art: Verification of void android.support.v7.app.AppCompatDelegateImplV9.onConfigurationChanged(android.content.res.Configuration) took 215.247ms
W/art: Verification of android.support.v7.view.ActionMode android.support.v7.app.AppCompatDelegateImplV9.startSupportActionModeFromWindow(android.support.v7.view.ActionMode$Callback) took 113.775ms
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
W/gralloc_ranchu: Gralloc pipe failed

                  [ 10-02 15:22:33.523  2284: 2284 D/         ]
                  HostConnection::get() New Host Connection established 0xa72c2540, tid 2284
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
I/Choreographer: Skipped 40 frames!  The application may be doing too much work on its main thread.
D/AndroidRuntime: Shutting down VM


                  --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.shiftind.www.shiftind, PID: 2284
                  java.lang.IllegalStateException: Could not execute method for android:onClick
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                      at android.view.View.performClick(View.java:5610)
                      at android.view.View$PerformClick.run(View.java:22260)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                      at android.view.View.performClick(View.java:5610) 
                      at android.view.View$PerformClick.run(View.java:22260) 
                      at android.os.Handler.handleCallback(Handler.java:751) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
                   Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.shiftind.www.shiftind/com.shiftind.www.shiftind.registration}; have you declared this activity in your AndroidManifest.xml?
                      at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
                      at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
                      at android.app.Activity.startActivityForResult(Activity.java:4224)
                      at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
                      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
                      at android.app.Activity.startActivityForResult(Activity.java:4183)
                      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
                      at android.app.Activity.startActivity(Activity.java:4507)
                      at android.app.Activity.startActivity(Activity.java:4475)
                      at com.shiftind.www.shiftind.MainActivity.onButtonClick(MainActivity.java:19)
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                      at android.view.View.performClick(View.java:5610) 
                      at android.view.View$PerformClick.run(View.java:22260) 
                      at android.os.Handler.handleCallback(Handler.java:751) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Application terminated.

我的mainActivity.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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.shiftind.www.shiftind.MainActivity">


    <Button
        android:text="Let's Get Started"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/startbutton"
        style="@style/Widget.AppCompat.Button.Colored"
        android:onClick="onButtonClick"
        android:layout_alignParentStart="true"
        android:layout_marginStart="85dp" />
</RelativeLayout>

我的registartion.xml(第二项活动)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="registration activity"
        android:layout_width="wrap_content"
        android:layout_height="111dp"
        android:id="@+id/textView3"
        android:layout_weight="1"
        android:textStyle="normal|bold" />
</LinearLayout>

我的registration.java

package com.shiftind.www.shiftind;

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by Ahmed on 10/2/2016.
 */

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

我的主要活动.java

package com.shiftind.www.shiftind;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;

public class MainActivity extends AppCompatActivity {

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

    public void onButtonClick(View view) {
        if (view.getId() == R.id.startbutton) {
            Intent i = new Intent(MainActivity.this, registration.class);
            startActivity(i);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

在清单文件中声明第二个Activity,您应该整理出来 此外,您的按钮未声明

答案 1 :(得分:0)

<application>标记

中添加此行
 <activity android:name=".registration" ></activity>

希望有所帮助