有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

更改屏幕后java应用程序崩溃

我会说得很贴切,切中要害。我刚刚开始学习在Android Studio中开发应用程序,我已经想出了以下代码:

package com.example.peter.helloworld;

import 安卓.content.Intent;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.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"?>
<安卓.support.constraint.ConstraintLayout 
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.peter.helloworld.MainActivity">

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

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

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

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

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

    <Button
        安卓:id="@+id/button"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_below="@+id/textView5"
        安卓:layout_centerHorizontal="true"
        安卓:layout_marginTop="57dp"
        安卓: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: 安卓::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
                                                                            安卓.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 安卓.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1933)
                                                                                at 安卓.app.Instrumentation.execStartActivity(Instrumentation.java:1616)
                                                                                at 安卓.app.Activity.startActivityForResult(Activity.java:4488)
                                                                                at 安卓.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
                                                                                at 安卓.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
                                                                                at 安卓.app.Activity.startActivityForResult(Activity.java:4446)
                                                                                at 安卓.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
                                                                                at 安卓.app.Activity.startActivity(Activity.java:4807)
                                                                                at 安卓.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 安卓.view.View.performClick(View.java:6294)
                                                                                at 安卓.view.View$PerformClick.run(View.java:24770)
                                                                                at 安卓.os.Handler.handleCallback(Handler.java:790)
                                                                                at 安卓.os.Handler.dispatchMessage(Handler.java:99)
                                                                                at 安卓.os.Looper.loop(Looper.java:164)
                                                                                at 安卓.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:807)

共 (4) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

    检查你的图片屏幕。AndroidManifest中的java。xml

    如果不存在,请按如下所示:

    <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>
    

    试试这个,让我知道这是否对你有用

  3. # 3 楼答案

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

  4. # 4 楼答案

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