MinatoMinami 2021-12-08 18:37 采纳率: 75%
浏览 52
已结题

第一个页面登录跳转后一直黑屏不显示消息

问题遇到的现象和发生背景

如题,界面一登录成功跳转后变为图1的情况。

img

img

问题相关代码,请勿粘贴截图
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class MainActivit extends AppCompatActivity {
    private Button Login;
    private EditText User,Password;

    private CheckBox cbIsRememberPass;
    private SharedPreferences sharedPreferences;

    private List<Bean> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//        setContentView(R.layout.activity_login);



        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        initViews();
        sharedPreferences=getSharedPreferences("rememberpassword",Context.MODE_PRIVATE);
        boolean isRemember=sharedPreferences.getBoolean("rememberpassword",false);

        if(isRemember){
            String user=sharedPreferences.getString("user","");
            String password=sharedPreferences.getString("password","");
            User.setText(user);
            Password.setText(password);
            cbIsRememberPass.setChecked(true);


        }

        User = findViewById(R.id.user);
        Password = findViewById(R.id.password);
        Login = findViewById(R.id.login);



        Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String user = User.getText().toString();
                String password = Password.getText().toString();
                if (user.length() == 0) {
                    Toast.makeText(MainActivit.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                } else if (password.length() == 0) {
                    Toast.makeText(MainActivit.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                } else if ((Password.getText().toString().trim()).equals(User.getText().toString().trim())) {
                    SharedPreferences.Editor editor=sharedPreferences.edit();
                    if(cbIsRememberPass.isChecked()){
                        editor.putBoolean("rememberpassword",true);
                        editor.putString("user",user);
                        editor.putString("password",password);
                        Intent intent = new Intent();
                        intent.setClass(MainActivit.this, MyAdapter.class);
                        startActivity(intent);
                    }else {
                        editor.clear();

                    } editor.commit();
                    finish();
                    Toast.makeText(MainActivit.this, "登录成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.setClass(MainActivit.this, MainActivity.class);
                    startActivity(intent);
                    MainActivit.this.finish();
                } else {
                    Toast.makeText(MainActivit.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();

                }

            }
        });

    }

    private void initViews() {
        User=(EditText)findViewById(R.id.user);
        Password=(EditText)findViewById(R.id.password);
        cbIsRememberPass=(CheckBox) findViewById(R.id.cbIsRememberPass);
    }

    public void callPhone (View view){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:07713246041"));
        startActivity(intent);
    }
}

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Bean> data = new ArrayList<>();

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

        for(int i=0;i<1000;i++){
            Bean bean =new Bean();
            bean.setName("11"+i);
            data.add(bean);
        }
        RecyclerView recyclerView = findViewById(R.id.rv);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);


        MyAdapter myAdapter=new MyAdapter(data,this);
        recyclerView.setAdapter(myAdapter);
    }
}

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

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivit">

    <TextView
        android:id="@+id/GXCME"
        android:layout_width="303dp"
        android:layout_height="38dp"
        android:text=""
        android:textAlignment="center"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.038" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="184dp"
        android:layout_height="114dp"
        android:layout_marginTop="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.484"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/GXCME"
        app:srcCompat="@drawable/gxcme" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="74dp"
        android:layout_height="23dp"
        android:layout_marginStart="36dp"
        android:layout_marginTop="40dp"
        android:text="用户名:"
        android:textSize="17sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="75dp"
        android:layout_height="20dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:text="密码:"
        android:textSize="17sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.4"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="@+id/user" />

    <EditText
        android:id="@+id/user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.406"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="callPhone"
        android:text="服务电话:"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/password"
        app:layout_constraintVertical_bias="0.95" />

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="84dp"
        android:layout_marginTop="20dp"
        android:text="登录"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

    <Button
        android:id="@+id/rege"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="64dp"
        android:layout_marginTop="20dp"
        android:text="注册"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/login"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="333dp"
        android:layout_height="67dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.642"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cbIsRememberPass"
        app:layout_constraintVertical_bias="0.267">

        <RadioGroup
            android:id="@+id/radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/radioButton4"
                android:layout_width="330dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:focusable="false"
                android:text="学生" />

            <RadioButton
                android:id="@+id/radioButton5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="教师" />


        </RadioGroup>


    </LinearLayout>

    <CheckBox
        android:id="@+id/cbIsRememberPass"
        android:layout_width="137dp"
        android:layout_height="29dp"
        android:layout_marginTop="24dp"
        android:text="记住账号密码"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/password" />

</androidx.constraintlayout.widget.ConstraintLayout>

  • 写回答

1条回答 默认 最新

  • JioWu 2021-12-09 14:53
    关注

    首先MyAdapter不是Activity 不能作显式跳转 其次你的Recyclerview需要再对应的布局中有部署 不然会为空 导致进入到MainAcitivty2的时候 Recyclerview去设置布局管理器这一行代码时报空指针

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月9日
  • 已采纳回答 12月9日
  • 创建了问题 12月8日

悬赏问题

  • ¥15 单片机配网,继电器开关,广播
  • ¥60 Qcustomplot绘制实时动态曲线
  • ¥20 运用matlab画x-y图
  • ¥15 用idea运行项目,运行tomcat报错:断言失败
  • ¥15 Sqlserver查询链接服务器数据问题
  • ¥15 Bibtex4Word 引用中文文献
  • ¥20 用opencv c/c++ 转换成灰度图,然后做一下直方图均衡,输出mp4文件
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题
  • ¥30 gradle环境下javafx项目如何使用druid连接池