屏幕更改后应用程序崩溃

时间:2017-11-25 04:09:06

标签: java android

我会保持这种甜美和重点。我刚开始学习在Android Studio中开发应用程序,我已经提出了这个代码:

package com.example.peter.helloworld;

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

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {

HashMap<String,String> cred = new HashMap<String,String>();
EditText u, p;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = findViewById(R.id.button);
    button.setOnClickListener(this);
    cred.put("username", "password");

}


public void onClick(View v){
    this.u = findViewById(R.id.username);
    this.p = findViewById(R.id.password);
    login(this.u.getText().toString(), this.p.getText().toString());
}

public void login(String username, String password){
    if (cred.keySet().contains(username)){
        if (cred.get((String) username).equals(password)){
            Intent change = new Intent(this, picscreen.class);
            startActivity(change);
        }
    }else{
        return;
    }
}

}

(在xml布局文件中)

<?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.peter.helloworld.MainActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_dark"
    android:backgroundTint="@android:color/holo_red_dark"
    android:layoutMode="opticalBounds"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:ems="10"
        android:inputType="textPassword"
        tools:layout_editor_absoluteX="85dp"
        tools:layout_editor_absoluteY="233dp" />

    <EditText
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/password"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/password"
        android:layout_marginTop="130dp"
        android:ems="10"
        android:inputType="textPersonName"
        tools:layout_toLeftOf="password" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/username"
        android:layout_centerHorizontal="true"
        android:text="Username"
        tools:layout_editor_absoluteX="161dp"
        tools:layout_editor_absoluteY="199dp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@id/textView3"
        android:layout_below="@id/password"
        android:layout_alignLeft="@id/textView3"
        android:text="Password" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView5"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="57dp"
        android:text="Login" />

</RelativeLayout>

目标是创建登录屏幕。

登录成功后应用程序崩溃。我想知道为什么。如果您需要其他信息,请与我们联系。

编辑:LOGCAT(对不起,我是新手,不知道诊断需要错误日志,干杯!

11-25 11:24:51.033 3431-3431/com.example.peter.helloworld I/zygote: Not late-enabling -Xcheck:jni (already on)
11-25 11:24:51.404 3431-3431/com.example.peter.helloworld W/zygote: Unexpected CPU variant for X86 using defaults: x86
11-25 11:24:51.785 3431-3431/com.example.peter.helloworld W/ActivityThread: Application com.example.peter.helloworld is waiting for the debugger on port 8100...
11-25 11:24:51.804 3431-3431/com.example.peter.helloworld I/System.out: Sending WAIT chunk
11-25 11:24:52.750 3431-3446/com.example.peter.helloworld I/zygote: Debugger is active
11-25 11:24:52.822 3431-3431/com.example.peter.helloworld I/System.out: Debugger has connected
11-25 11:24:52.822 3431-3431/com.example.peter.helloworld I/System.out: waiting for debugger to settle...
11-25 11:24:53.025 3431-3431/com.example.peter.helloworld I/System.out: waiting for debugger to settle...
11-25 11:24:53.227 3431-3431/com.example.peter.helloworld I/System.out: waiting for debugger to settle...
11-25 11:24:53.428 3431-3431/com.example.peter.helloworld I/System.out: waiting for debugger to settle...
11-25 11:24:53.630 3431-3431/com.example.peter.helloworld I/System.out: waiting for debugger to settle...
11-25 11:24:53.838 3431-3431/com.example.peter.helloworld I/System.out: waiting for debugger to settle...
11-25 11:24:54.040 3431-3431/com.example.peter.helloworld I/System.out: waiting for debugger to settle...
11-25 11:24:54.242 3431-3431/com.example.peter.helloworld I/System.out: waiting for debugger to settle...
11-25 11:24:54.449 3431-3431/com.example.peter.helloworld I/System.out: waiting for debugger to settle...
11-25 11:24:54.650 3431-3431/com.example.peter.helloworld I/System.out: waiting for debugger to settle...
11-25 11:24:54.851 3431-3431/com.example.peter.helloworld I/System.out: waiting for debugger to settle...
11-25 11:24:55.053 3431-3431/com.example.peter.helloworld I/System.out: waiting for debugger to settle...
11-25 11:24:55.255 3431-3431/com.example.peter.helloworld I/System.out: waiting for debugger to settle...
11-25 11:24:55.456 3431-3431/com.example.peter.helloworld I/System.out: debugger has settled (1458)
11-25 11:24:55.673 3431-3431/com.example.peter.helloworld I/InstantRun: starting instant run server: is main process
11-25 11:24:56.416 3431-3524/com.example.peter.helloworld D/OpenGLRenderer: HWUI GL Pipeline
11-25 11:24:56.591 3431-3524/com.example.peter.helloworld I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
11-25 11:24:56.591 3431-3524/com.example.peter.helloworld I/OpenGLRenderer: Initialized EGL, version 1.4
11-25 11:24:56.591 3431-3524/com.example.peter.helloworld D/OpenGLRenderer: Swap behavior 1
11-25 11:24:56.599 3431-3524/com.example.peter.helloworld D/EGL_emulation: eglCreateContext: 0xa7341600: maj 2 min 0 rcv 2
11-25 11:24:56.618 3431-3524/com.example.peter.helloworld D/EGL_emulation: eglMakeCurrent: 0xa7341600: ver 2 0 (tinfo 0xa9ca1f70)
11-25 11:24:56.813 3431-3524/com.example.peter.helloworld D/EGL_emulation: eglMakeCurrent: 0xa7341600: ver 2 0 (tinfo 0xa9ca1f70)
11-25 11:24:57.373 3431-3431/com.example.peter.helloworld V/View: dispatchProvideAutofillStructure(): not laid out, ignoring 0 children of 1073741833
11-25 11:24:57.392 3431-3431/com.example.peter.helloworld I/AssistStructure: Flattened final assist data: 2912 bytes, containing 1 windows, 11 views
11-25 11:25:00.021 3431-3444/com.example.peter.helloworld I/zygote: Do partial code cache collection, code=28KB, data=30KB
11-25 11:25:00.022 3431-3444/com.example.peter.helloworld I/zygote: After code cache collection, code=28KB, data=30KB
11-25 11:25:00.022 3431-3444/com.example.peter.helloworld I/zygote: Increasing code cache capacity to 128KB
11-25 11:25:00.650 3431-3444/com.example.peter.helloworld I/zygote: Do partial code cache collection, code=58KB, data=58KB
11-25 11:25:00.650 3431-3444/com.example.peter.helloworld I/zygote: After code cache collection, code=58KB, data=58KB
11-25 11:25:00.650 3431-3444/com.example.peter.helloworld I/zygote: Increasing code cache capacity to 256KB
11-25 11:25:02.967 3431-3444/com.example.peter.helloworld I/zygote: Do full code cache collection, code=123KB, data=99KB
11-25 11:25:03.029 3431-3444/com.example.peter.helloworld I/zygote: After code cache collection, code=116KB, data=71KB
11-25 11:25:03.505 3431-3431/com.example.peter.helloworld E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
11-25 11:25:03.505 3431-3431/com.example.peter.helloworld E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
11-25 11:25:05.305 3431-3444/com.example.peter.helloworld I/zygote: Do partial code cache collection, code=123KB, data=98KB
11-25 11:25:05.307 3431-3444/com.example.peter.helloworld I/zygote: After code cache collection, code=123KB, data=98KB
11-25 11:25:05.307 3431-3444/com.example.peter.helloworld I/zygote: Increasing code cache capacity to 512KB
11-25 11:25:12.140 3431-3444/com.example.peter.helloworld I/zygote: Do full code cache collection, code=248KB, data=169KB
11-25 11:25:12.140 3431-3444/com.example.peter.helloworld I/zygote: After code cache collection, code=245KB, data=145KB
11-25 11:25:20.114 3431-3431/com.example.peter.helloworld D/AndroidRuntime: Shutting down VM
11-25 11:25:20.131 3431-3431/com.example.peter.helloworld E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.peter.helloworld, PID: 3431
                                                                            android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.peter.helloworld/com.example.peter.helloworld.picscreen}; have you declared this activity in your AndroidManifest.xml?
                                                                                at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1933)
                                                                                at android.app.Instrumentation.execStartActivity(Instrumentation.java:1616)
                                                                                at android.app.Activity.startActivityForResult(Activity.java:4488)
                                                                                at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
                                                                                at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
                                                                                at android.app.Activity.startActivityForResult(Activity.java:4446)
                                                                                at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
                                                                                at android.app.Activity.startActivity(Activity.java:4807)
                                                                                at android.app.Activity.startActivity(Activity.java:4775)
                                                                                at com.example.peter.helloworld.MainActivity.login(MainActivity.java:39)
                                                                                at com.example.peter.helloworld.MainActivity.onClick(MainActivity.java:32)
                                                                                at android.view.View.performClick(View.java:6294)
                                                                                at android.view.View$PerformClick.run(View.java:24770)
                                                                                at android.os.Handler.handleCallback(Handler.java:790)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                at android.os.Looper.loop(Looper.java:164)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

4 个答案:

答案 0 :(得分:1)

您的代码工作正常但请检查清单文件中的“picscreen”条目

答案 1 :(得分:0)

您是否在清单文件中声明了“picscreen”。这可能是应用程序崩溃的根本原因。

答案 2 :(得分:0)

您是否在清单文件中声明了picsscreen活动?

答案 3 :(得分:0)

检查AndroidManifest.xml中的picscreen.java

如果它不存在,那就把它放在下面:

<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>
        <activity android:name=".picscreen" />

    </application>

试试这个并告诉我这是否适合您。