findViewById(R.id.listview_category)返回null的问题

本文介绍了一种解决在Android应用开发中ListView显示数据时出现null值的问题。通过更改查找视图的方式,使用popupWindow并正确地初始化ListView,可以避免空指针异常,确保应用程序稳定运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

listview中显示数据时老是报null,通过查询发现
cid_listview=(ListView) findViewById(R.id.listview_category);

返回null。

解决办法

View popupWindow=layoutInflator.inflate(R.layout.cid_popup, null);
		cid_listview=(ListView) popupWindow.findViewById(R.id.listview_category);
所以在用
findViewById方法时要指定view
实验四的界面是图书查询界面Ss.java类代码: import android.content.Intent; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class Ss extends AppCompatActivity { private EditText editId; private EditText editName; private EditText editAuthor; private static final int item1 = Menu.FIRST; private static final int item2 = Menu.FIRST + 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ss); // 初始化 EditText editId = findViewById(R.id.edit_id); editName = findViewById(R.id.edit_name); editAuthor = findViewById(R.id.edit_author);//通过ID绑定视图对象 LinearLayout ll = findViewById(R.id.table_layout); registerForContextMenu(ll); // 1清空 - 属性事件响应(XML 按钮需添加 android:onClick="clearByAttribute") // 2清空 - 内部匿名类事件响应 findViewById(R.id.ff_id1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clearFields(); } }); // 3清空 - 监听器接口实现类事件响应 findViewById(R.id.ff_id2).setOnClickListener(new MyClickListener()); // 4清空 - 外部监听器接口实现类事件响应 findViewById(R.id.ff_id3).setOnClickListener(new ExternalClickListener(this)); Button btnBack = findViewById(R.id.btn_back); btnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Ss.this, Sy.class); startActivity(intent); finish(); } }); } // 1清空对应的属性事件响应方法 public void clearByAttribute(View view) { clearFields(); }//当用户点击按钮就会被执行 // 清空文本框的公共方法 public void clearFields() { if (editId != null) editId.setText(""); if (editName != null) editName.setText(""); if (editAuthor != null) editAuthor.setText(""); } // 内部监听器接口实现类(3清空) private class MyClickListener implements View.OnClickListener { @Override public void onClick(View v) { clearFields(); } } // 外部监听器接口实现类(4清空) @Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo info) { menu.add(0, 1, 0, "删除"); menu.add(0, 2, 0, "修改"); super.onCreateContextMenu(menu, view, info); } @Override public boolean onContextItemSelected(MenuItem item) { if (item.getItemId() == 1) { Toast.makeText(this, "您点击了删除", Toast.LENGTH_LONG).show(); } else if (item.getItemId() == 2) { Toast.makeText(this, "您点击了修改", Toast.LENGTH_LONG).show(); } return true; } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, item1, 0, "关于"); menu.add(0, item2, 0, "退出"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case item1: Toast.makeText(Ss.this, "姓名:骆树涛。学号:2404224132", Toast.LENGTH_SHORT).show(); break; case item2: this.finish(); break; } return true; } } 图书查询界面的ss.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" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图书编号" android:textSize="20dp"/> <EditText android:id="@+id/edit_id" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图书名称" android:textSize="20dp"/> <EditText android:id="@+id/edit_name" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图书作者" android:textSize="20dp"/> <EditText android:id="@+id/edit_author" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="中文" android:textSize="20dp"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="外文" android:textSize="20dp"/> </RadioGroup> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询" android:textSize="20dp"/> <Button android:id="@+id/btn_clar" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="清空" android:textSize="20dp" android:onClick="clearByAttribute"/> <Button android:id="@+id/btn_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" android:textSize="20dp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/ff_id1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清空" android:textSize="20dp"/> <Button android:id="@+id/ff_id2" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="清空" android:textSize="20dp" /> <Button android:id="@+id/ff_id3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清空" android:textSize="20dp"/> </LinearLayout> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="*"> <TableRow> <TextView android:text="图书编号" android:textSize="15dp"/> <TextView android:text="图书名称" android:textSize="15dp"/> <TextView android:text="图书作者" android:textSize="15dp"/> <TextView android:text="馆藏地点" android:textSize="15dp"/> </TableRow> <TableRow android:id="@+id/table_layout"> <TextView android:text="1001" android:textSize="15dp"/> <TextView android:text="Android" android:textSize="15dp"/> <TextView android:text="张三" android:textSize="15dp"/> <TextView android:text="5-423" android:textSize="15dp"/> </TableRow> <TableRow> <TextView android:text="查询结果1条" android:textSize="15dp" android:layout_span="4" android:layout_gravity="center"/> </TableRow> </TableLayout> </LinearLayout> 实验五的有首页界面、图书新增界面、图书详情界面。代码如下 首页Sy.JAVA类代码 import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class Sy extends AppCompatActivity { private static final int item1 = Menu.FIRST; private static final int item2 = Menu.FIRST + 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sy); Button btnAdd = findViewById(R.id.btn_add); Button btnSearch = findViewById(R.id.btn_search); btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Sy.this, Xz.class); startActivity(intent); } }); btnSearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Sy.this, Ss.class); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, item1, 0, "关于"); menu.add(0, item2, 0, "退出"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case item1: Toast.makeText(Sy.this, "姓名:骆树涛。学号:2404224132", Toast.LENGTH_SHORT).show(); break; case item2: finishAffinity(); break; } return true; } } 首页的sy.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" android:orientation="vertical"> <Button android:id="@+id/btn_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="新增" android:textSize="20dp"/> <Button android:id="@+id/btn_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询" android:textSize="20dp"/> </LinearLayout> 图书新增的Xz.Java类代码: import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import androidx.appcompat.app.AppCompatActivity; public class Xz extends AppCompatActivity { private EditText editBookId; private EditText editBookName; private EditText editBookAuthor; private EditText editBookLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.xz); editBookId = findViewById(R.id.edit_book_id); editBookName = findViewById(R.id.edit_book_name); editBookAuthor = findViewById(R.id.edit_book_author); editBookLocation = findViewById(R.id.edit_book_location); Button btnAddBook = findViewById(R.id.btn_add_book); btnAddBook.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String bookId = editBookId.getText().toString(); String bookName = editBookName.getText().toString(); String bookAuthor = editBookAuthor.getText().toString(); String bookLocation = editBookLocation.getText().toString(); Intent intent = new Intent(Xz.this, Xq.class); intent.putExtra("bookId", bookId); intent.putExtra("bookName", bookName); intent.putExtra("bookAuthor", bookAuthor); intent.putExtra("bookLocation", bookLocation); startActivity(intent); } }); } } 图书新增的xz.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" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图书编号" android:textSize="20dp"/> <EditText android:id="@+id/edit_book_id" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图书名称" android:textSize="20dp"/> <EditText android:id="@+id/edit_book_name" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图书作者" android:textSize="20dp"/> <EditText android:id="@+id/edit_book_author" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="馆藏地点" android:textSize="20dp"/> <EditText android:id="@+id/edit_book_location" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <Button android:id="@+id/btn_add_book" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="新增" android:textSize="20dp" android:layout_gravity="center"/> </LinearLayout> 图书详情的Xq.java类代码: import android.content.Intent; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class Xq extends AppCompatActivity { private static final int ITEM_HOME = Menu.FIRST; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.xq); TextView tvBookId = findViewById(R.id.tv_book_id); TextView tvBookName = findViewById(R.id.tv_book_name); TextView tvBookAuthor = findViewById(R.id.tv_book_author); TextView tvBookLocation = findViewById(R.id.tv_book_location); Intent intent = getIntent(); String bookId = intent.getStringExtra("bookId"); String bookName = intent.getStringExtra("bookName"); String bookAuthor = intent.getStringExtra("bookAuthor"); String bookLocation = intent.getStringExtra("bookLocation"); tvBookId.setText("图书编号:" + bookId); tvBookName.setText("图书名称:" + bookName); tvBookAuthor.setText("图书作者:" + bookAuthor); tvBookLocation.setText("馆藏地点:" + bookLocation); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, ITEM_HOME, 0, "首页"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case ITEM_HOME: Intent intent = new Intent(Xq.this, Sy.class); startActivity(intent); finish(); return true; default: return super.onOptionsItemSelected(item); } } } 图书详情的xq.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" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/tv_book_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp"/> <TextView android:id="@+id/tv_book_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp"/> <TextView android:id="@+id/tv_book_author" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp"/> <TextView android:id="@+id/tv_book_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp"/> </LinearLayout>在实验4的基础上,新增3个界面,分别是图书管理系统的“首页”、“图书新增”和“图书详情”界面。具体要求如下: (1)首页显示两个按钮,分别是“新增”和“查询”。点击“新增”按钮,跳转到图书新增界面;点击“查询”按钮,调整到图书查询界面(即实验4所完成的界面)。 (2)图书新增界面,设置图书编号、图书名称、图书作者和馆藏地点四个信息的输入框,以及一个“新增”按钮。用户点击新增按钮后,将用户输入的4个信息传递到图书详情页面,并显示图书详情页面。 (3)图书详情页面用于显示用户输入的图书编号、图书名称、图书作者和馆藏地点。 (4)整个应用程序的入口固定设置为“首页”对应的Activity。 (5)完善“图书查询界面”中的"返回"按钮,按钮的作用是回到首页。 (6)将实验3中,图书查询页面的OptionsMenu搬迁到首页程序中。根据题目来规划写个流程图
最新发布
06-28
AndroidManifest.xml:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.diaryapp"> <uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA" /> <application android:allowBackup="true" android:hardwareAccelerated="true" android:largeHeap="true" android:requestLegacyExternalStorage="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.HelloWorld"> <activity android:name=".MainActivity" android:label="@string/title_activity_main" android:exported="false" /> <activity android:name=".DiaryActivity" android:label="@string/title_activity_diary" android:exported="false" /> <activity android:name=".RegisterActivity" android:label="@string/title_activity_register" android:exported="false" /> <activity android:name=".LoginActivity" android:label="@string/title_activity_login" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> DiaryAdapter: package com.example.diaryapp.adapters; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.example.diaryapp.utils.GlideEngine; import com.example.diaryapp.DiaryActivity; import com.example.diaryapp.R; import com.example.diaryapp.models.Diary; import java.util.List; public class DiaryAdapter extends RecyclerView.Adapter<DiaryAdapter.DiaryViewHolder> { private Context context; private List<Diary> diaryList; private int userId; public DiaryAdapter(Context context,List<Diary> diaryList,int userId){ this.context=context; this.diaryList=diaryList; this.userId=userId; // 通过构造函数传递 userId } // 当数据更新时调用此方法 @SuppressLint("NotifyDataSetChanged") public void updateDiaries(List<Diary> newDiaries){ this.diaryList.clear(); this.diaryList.addAll(newDiaries); notifyDataSetChanged(); } @NonNull @Override public DiaryAdapter.DiaryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view= LayoutInflater.from(context).inflate(R.layout.item_diary,parent,false); return new DiaryViewHolder(view); } // 绑定视图持有者方法 @Override public void onBindViewHolder(@NonNull DiaryAdapter.DiaryViewHolder holder, int position) { Diary diary=diaryList.get(position); holder.tvTitle.setText(diary.getTitle()); holder.tvDate.setText(diary.getDate()); holder.tvContent.setText(diary.getContent()); holder.tvWordCount.setText("字数:" + diary.getContent().length()); if (diary.getImagePath() != null && !diary.getImagePath().isEmpty()) { GlideEngine engine = GlideEngine.createGlideEngine(); engine.loadImage(context,diary.getImagePath(),holder.ivImage); } else { holder.ivImage.setBackgroundColor(context.getResources().getColor(android.R.color.darker_gray)); } holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(context, DiaryActivity.class); intent.putExtra("diaryId",diary.getId()); intent.putExtra("userId",userId); // 确保传递正确的 userId context.startActivities(new Intent[]{intent}); } }); } @Override public int getItemCount() { return diaryList.size(); } public static class DiaryViewHolder extends RecyclerView.ViewHolder { private TextView tvTitle,tvDate,tvContent,tvWordCount; private ImageView ivImage; public DiaryViewHolder(@NonNull View itemView) { super(itemView); tvTitle=itemView.findViewById(R.id.tvTitle); tvDate=itemView.findViewById(R.id.tvDate); tvContent=itemView.findViewById(R.id.tvContent); ivImage=itemView.findViewById(R.id.ivImage); tvWordCount=itemView.findViewById(R.id.tvWordCount); } } } Diary:package com.example.diaryapp.models; public class Diary { private int id; private String title; private String content; private String date; private String imagePath; public Diary(){} public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getImagePath() { return imagePath; } public void setImagePath(String imagePath) { this.imagePath = imagePath; } } DatabaseHelper:package com.example.diaryapp.utils; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import com.example.diaryapp.models.Diary; import java.util.ArrayList; import java.util.List; public class DatabaseHelper extends SQLiteOpenHelper { //数据库信息 private static final String DATABASE_NAME = "DiaryApp.db"; private static final int DATABASE_VERSION = 1; //用户表信息 private static final String TABLE_USER = "user"; private static final String COLUMN_USER_ID = "id"; private static final String COLUMN_USER_USERNAME = "username"; private static final String COLUMN_USER_PASSWORD = "password"; //日记表信息 private static final String TABLE_DIARY = "diary"; private static final String COLUMN_DIARY_ID = "id"; private static final String COLUMN_DIARY_USER_ID = "user_id"; private static final String COLUMN_DIARY_TITLE = "title"; private static final String COLUMN_DIARY_CONTENT = "content"; private static final String COLUMN_DIARY_DATE = "date"; private static final String COLUMN_DIARY_IMAGE_PATH = "image_path"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME,null,DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { //创建用户表 String createUserTable = "CREATE TABLE " + TABLE_USER + " (" + COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_USER_USERNAME + " TEXT UNIQUE, " + COLUMN_USER_PASSWORD + " TEXT)"; db.execSQL(createUserTable); //创建日记表 String createDiaryTable = "CREATE TABLE " + TABLE_DIARY + " (" + COLUMN_DIARY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_DIARY_USER_ID + " INTEGER, " + COLUMN_DIARY_TITLE + " TEXT, " + COLUMN_DIARY_CONTENT + " TEXT, " + COLUMN_DIARY_DATE + " TEXT, " + COLUMN_DIARY_IMAGE_PATH + " TEXT)"; db.execSQL(createDiaryTable); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //升级数据库时的操作 db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER); db.execSQL("DROP TABLE IF EXISTS " + TABLE_DIARY); onCreate(db); } //用户相关操作 public long addUser(String username,String password){ SQLiteDatabase db = null; try { db=this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_USER_USERNAME, username); values.put(COLUMN_USER_PASSWORD,password); return db.insert(TABLE_USER,null,values); }catch (Exception e){ e.printStackTrace(); return -1; }finally { if (db!=null){ db.close(); } } } public boolean checkUser(String username, String password){ SQLiteDatabase db = null; Cursor cursor = null; boolean exists = false; try { db=this.getReadableDatabase(); cursor = db.rawQuery( "SELECT * FROM " + TABLE_USER + " WHERE " + COLUMN_USER_USERNAME + "=? AND " + COLUMN_USER_PASSWORD + "=?", new String[]{username,password} ); exists = cursor.getCount() > 0; }catch (Exception e){ e.printStackTrace(); }finally { if (cursor != null){ cursor.close(); } if (db!=null){ db.close(); } } return exists; // Cursor cursor = db.rawQuery( // "SELECT * FROM " + TABLE_USER + " WHERE " + // COLUMN_USER_USERNAME + "=? AND " + COLUMN_USER_PASSWORD + "=?", // new String[]{username,password} // ); // boolean exists = cursor.getCount() > 0; // Log.d("DatabaseHelper","checkUser: username=" + username + ", exists=" + exists); // cursor.close(); // return exists; } public int getUserId(String username){ SQLiteDatabase db = null; Cursor cursor=null; int userId=-1; try { db = this.getReadableDatabase(); cursor = db.rawQuery( "SELECT " + COLUMN_USER_ID + " FROM " + TABLE_USER + " WHERE " + COLUMN_USER_USERNAME + "=?", new String[]{username} ); if (cursor.moveToFirst()) { // 如果cursor不为空且移动到第一条记录,获取用户ID userId = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_USER_ID)); } }catch (Exception e){ e.printStackTrace(); }finally { if (cursor!=null){ cursor.close(); } if (db!=null){ db.close(); } } return userId; } //日记相关操作 public long addDiary(int userId, Diary diary){ SQLiteDatabase db = getWritableDatabase(); try { db.beginTransaction(); ContentValues values = new ContentValues(); values.put(COLUMN_DIARY_USER_ID, userId); values.put(COLUMN_DIARY_TITLE,diary.getTitle()); values.put(COLUMN_DIARY_CONTENT,diary.getContent()); values.put(COLUMN_DIARY_DATE,diary.getDate()); values.put(COLUMN_DIARY_IMAGE_PATH,diary.getImagePath()); long result = db.insert(TABLE_DIARY,null,values); db.setTransactionSuccessful(); return result; }catch (Exception e){ e.printStackTrace(); return -1; }finally { db.endTransaction(); // if (db != null){ // db.endTransaction(); // db.close(); // } } } public List<Diary> getDiariesByUserId(int userId){ List<Diary> diaries = new ArrayList<>(); SQLiteDatabase db = getReadableDatabase(); Cursor cursor = null; try { db = this.getReadableDatabase(); cursor = db.rawQuery( "SELECT * FROM " + TABLE_DIARY + " WHERE " + COLUMN_DIARY_USER_ID + "=? ORDER BY " + COLUMN_DIARY_DATE + " DESC", new String[]{String.valueOf(userId)} ); while (cursor.moveToNext()) { Diary diary = new Diary(); diary.setId(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_DIARY_ID))); diary.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DIARY_TITLE))); diary.setContent(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DIARY_CONTENT))); diary.setDate(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DIARY_DATE))); diary.setImagePath(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DIARY_IMAGE_PATH))); diaries.add(diary); } }catch (Exception e){ e.printStackTrace(); }finally { if (cursor != null){ cursor.close(); } // if (db != null){ // db.close(); // } } return diaries; } public Diary getDiaryById(int diaryId){ SQLiteDatabase db = getReadableDatabase(); Cursor cursor = null; try { db = this.getReadableDatabase(); cursor = db.rawQuery( "SELECT * FROM " + TABLE_DIARY + " WHERE " + COLUMN_DIARY_ID + "=?", new String[]{String.valueOf(diaryId)} ); if (cursor.moveToFirst()) { Diary diary = new Diary(); diary.setId(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_DIARY_ID))); diary.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DIARY_TITLE))); diary.setContent(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DIARY_CONTENT))); diary.setDate(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DIARY_DATE))); diary.setImagePath(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DIARY_IMAGE_PATH))); return diary; } }catch (Exception e){ e.printStackTrace(); }finally { if (cursor != null){ cursor.close(); } // if (db != null){ //// db.close(); // 确保关闭数据库连接 // } } // cursor.close(); return null; } public long updateDiary(Diary diary){ SQLiteDatabase db = getWritableDatabase(); try { ContentValues values = new ContentValues(); values.put(COLUMN_DIARY_TITLE,diary.getTitle()); values.put(COLUMN_DIARY_CONTENT,diary.getContent()); values.put(COLUMN_DIARY_DATE,diary.getDate()); values.put(COLUMN_DIARY_IMAGE_PATH,diary.getImagePath()); return db.update(TABLE_DIARY, values, COLUMN_DIARY_ID + "=?", new String[]{String.valueOf(diary.getId())}); // db.close(); // 确保关闭数据库连接 // Log.d("DatabaseHelper","updateDiary: result=" + result); //调试日志 // return result; }catch (Exception e){ e.printStackTrace(); return -1; }finally { // if (db != null){ // db.close(); // } } } public void deleteDiary(int diaryId) { SQLiteDatabase db = getWritableDatabase(); try { db.beginTransaction(); db.delete(TABLE_DIARY, COLUMN_DIARY_ID + "=?", new String[]{ String.valueOf(diaryId) }); db.setTransactionSuccessful(); }catch (Exception e){ e.printStackTrace(); }finally { db.endTransaction(); } // finally { // if (db != null){ // db.close(); // } } // int result=db.delete(TABLE_DIARY, // COLUMN_DIARY_ID + "=?", // new String[]{String.valueOf(diaryId)}); // db.close(); // 确保关闭数据库连接 // Log.d("DatabaseHelper","deleteDiary: result=" + result);//调试日志 // } } GlideEngine:package com.example.diaryapp.utils; import android.content.Context; import android.widget.ImageView; import com.bumptech.glide.Glide; import com.bumptech.glide.load.resource.bitmap.CenterCrop; import com.bumptech.glide.load.resource.bitmap.RoundedCorners; import com.example.diaryapp.R; import com.luck.picture.lib.engine.ImageEngine; import com.luck.picture.lib.utils.ActivityCompatHelper; public class GlideEngine implements ImageEngine { @Override public void loadImage(Context context, String url, ImageView imageView) { if (!ActivityCompatHelper.assertValidRequest(context)) { return; } try { Glide.with(context) .load(url) .into(imageView); } catch (Exception e) { e.printStackTrace(); imageView.setBackgroundColor(context.getResources().getColor(android.R.color.darker_gray)); } } @Override public void loadImage(Context context, ImageView imageView, String url, int maxWidth, int maxHeight) { if (!ActivityCompatHelper.assertValidRequest(context)) { return; } Glide.with(context) .load(url) .override(maxWidth, maxHeight) .into(imageView); } @Override public void loadAlbumCover(Context context, String url, ImageView imageView) { if (!ActivityCompatHelper.assertValidRequest(context)) { return; } Glide.with(context) .asBitmap() .load(url) .override(180, 180) .sizeMultiplier(0.5f) .transform(new CenterCrop(), new RoundedCorners(8)) .placeholder(R.drawable.ic_launcher_background) .into(imageView); } @Override public void loadGridImage(Context context, String url, ImageView imageView) { if (!ActivityCompatHelper.assertValidRequest(context)) { return; } Glide.with(context) .load(url) .override(200, 200) .centerCrop() .placeholder(R.drawable.ic_launcher_background) .into(imageView); } @Override public void pauseRequests(Context context) { Glide.with(context).pauseRequests(); } @Override public void resumeRequests(Context context) { Glide.with(context).resumeRequests(); } private GlideEngine() { } private static final class InstanceHolder { static final GlideEngine instance = new GlideEngine(); } public static GlideEngine createGlideEngine() { return InstanceHolder.instance; } }DiaryActivity:package com.example.diaryapp; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import com.example.diaryapp.models.Diary; import com.example.diaryapp.utils.DatabaseHelper; import com.example.diaryapp.utils.GlideEngine; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; public class DiaryActivity extends AppCompatActivity { // 声明界面组件和相关变量 private EditText etTitle,etContent; private TextView tvDate; private ImageView ivImage; private Button btnSave,btnDelete,btnAddImage; private DatabaseHelper dbHelper; private int userId; private int diaryId = -1; private String imagePath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_diary);// 设置布局文件 // 获取传递过来的用户ID和日记ID userId = getIntent().getIntExtra("userId",-1); diaryId = getIntent().getIntExtra("diaryId",-1); if (userId == -1){ // 检查用户ID是否有效 // 用户ID无效,可能是登录流程的问题 Toast.makeText(this, "用户ID无效", Toast.LENGTH_SHORT).show(); finish(); return; } // 初始化界面组件 initView(); // 如果是编辑日记,加载日记内容 if (diaryId != -1){ Diary diary=dbHelper.getDiaryById(diaryId); if (diary != null){ etTitle.setText(diary.getTitle()); // 设置日记标题 etContent.setText(diary.getContent()); // 设置日记内容 tvDate.setText(diary.getDate()); // 设置日记日期 imagePath=diary.getImagePath(); // 获取图片路径 if (imagePath != null && !imagePath.isEmpty()) { try { // 使用Glide加载图片 GlideEngine engine = GlideEngine.createGlideEngine(); engine.loadImage(this, imagePath, ivImage); } catch (Exception e) { e.printStackTrace(); ivImage.setBackgroundColor(getResources().getColor(android.R.color.darker_gray)); // 设置默认背景 } } // 显示删除按钮 btnDelete.setVisibility(View.VISIBLE); }else { // 日记ID无效,可能是数据库查询的问题 Toast.makeText(this, "日记ID无效", Toast.LENGTH_SHORT).show(); finish(); } }else { // 设置当前日期 tvDate.setText(getCurrentDate()); } // 添加图片按钮监听 btnAddImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //selectImage(); // 改:if (!view.isEnabled() || !view.isClickable()){ // return; // } showImageOptions(); // 显示添加图片选项 } }); // 保存按钮监听 btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 改:if (!view.isEnabled() || !view.isClickable()){ // return; // } saveDiary(); // 保存日记 } }); // 删除按钮监听 btnDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 改:if (!view.isEnabled() || !view.isClickable()){ // return; // } deleteDiary(); // 删除日记 } }); } // private void selectImage() { // // 创建选择图片的Intent // Intent pickIntent=new Intent(Intent.ACTION_GET_CONTENT); // pickIntent.setType("image/*"); // // // 创建拍照的Intent // Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // if (captureIntent.resolveActivity(getPackageManager()) != null) { // startActivityForResult(captureIntent,2); // } // //intent.setAction(Intent.ACTION_GET_CONTENT); // startActivityForResult(pickIntent,1); // } private void showImageOptions() { // 创建弹出菜单,提供两个选项:从图库选择和拍照 String[] options = {"从图库选择","拍照"}; new AlertDialog.Builder(this) .setTitle("添加图片") .setItems(options,(dialog,which) -> { if (which == 0){ //从图库选择 Intent pickIntent = new Intent(Intent.ACTION_GET_CONTENT); pickIntent.setType("image/*"); startActivityForResult(pickIntent,1); }else if (which == 1){ //拍照 Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (captureIntent.resolveActivity(getPackageManager()) != null){ startActivityForResult(captureIntent,2); }else { Toast.makeText(DiaryActivity.this, "无法调用摄像头", Toast.LENGTH_SHORT).show(); } } }) .show(); } private void saveDiary() { String title=etTitle.getText().toString().trim(); String content=etContent.getText().toString().trim(); String date=tvDate.getText().toString(); if (title.isEmpty() || content.isEmpty()){ // 检查输入是否为空 return; } Diary diary=new Diary(); diary.setTitle(title); diary.setContent(content); diary.setDate(date); diary.setImagePath(imagePath); // 确保图片路径不为空 if (diaryId != -1){ diary.setId(diaryId); dbHelper.updateDiary(diary); // 更新日记 }else { dbHelper.addDiary(userId,diary); // 添加新日记 } //返回主界面 Intent resultIntent = new Intent(); setResult(RESULT_OK,resultIntent); finish(); } private void deleteDiary() { // //删除日记 // dbHelper.deleteDiary(diaryId); new AlertDialog.Builder(this) .setTitle("确认删除") .setMessage("确定要删除这篇日记吗?") .setPositiveButton("删除", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 使用独立的 DatabaseHelper 实例执行删除操作 DatabaseHelper helper=new DatabaseHelper(DiaryActivity.this); helper.deleteDiary(diaryId); // dbHelper.deleteDiary(diaryId); // 删除日记 // 设置结果码为RESULT_OK,表示操作成功,返回主界面MainActivity Intent resultIntent = new Intent(); setResult(RESULT_OK,resultIntent); finish(); } }) .setNegativeButton("取消",null) .show(); } @Override protected void onDestroy() { super.onDestroy(); // 释放可能持有的资源 if (btnSave!=null){ btnSave.setOnClickListener(null); } if (btnDelete!=null){ btnDelete.setOnClickListener(null); } if (btnAddImage!=null){ btnAddImage.setOnClickListener(null); } } @Override protected void onPause() { super.onPause(); // 暂停时释放资源 if (isFinishing()) { // 清理可能的临时资源 } } @Override protected void onResume() { super.onResume(); // 恢复时重新初始化资源 } private String getCurrentDate(){ // 返回当前日期的字符串表示 return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); } protected void onActivityResult(int requestCode,int resultCode,Intent data){ super.onActivityResult(requestCode,resultCode,data); if (isFinishing() || isDestroyed()){ return; } // 确保调用super.onActivityResult if (requestCode == 1 && resultCode == RESULT_OK && data != null){ // 处理从图库选择的图片 Uri selectedImage = data.getData(); if (selectedImage != null) { // 检查selectedImage是否为null try { // 将图片保存到应用私有目录 Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage); String filename = "diary_image_" + System.currentTimeMillis() + ".jpg"; File file = new File(getExternalFilesDir(null), filename); // File fileDir = getExternalFilesDir(null); // if (fileDir != null && !fileDir.exists()){ // fileDir.mkdir(); // } // File file = new File(fileDir,filename); FileOutputStream fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); imagePath = file.getAbsolutePath(); // 改:ivImage.setImageBitmap(bitmap); // 显示图片 // 使用Glide加载图片 GlideEngine engine = GlideEngine.createGlideEngine(); engine.loadImage(this, imagePath, ivImage); } catch (IOException e) { e.printStackTrace(); //改: ivImage.setBackgroundColor(getResources().getColor(android.R.color.darker_gray)); } } // Glide.with(this).load(imagePath).into(ivImage); // }catch (IOException e){ // e.printStackTrace(); // } }else if (requestCode == 2 && resultCode == RESULT_OK && data != null){ //处理拍照结果 Bundle extras = data.getExtras(); Bitmap imageBitmap = (Bitmap) extras.get("data"); if (extras != null) { // 检查extras是否为null // Bitmap imageBitmap = (Bitmap) extras.get("data"); if (imageBitmap != null) {// 检查photo是否为null try { String filename = "diary_image_" + System.currentTimeMillis() + ".jpg"; File file = new File(getExternalFilesDir(null), filename); FileOutputStream fos = new FileOutputStream(file); imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); imagePath = file.getAbsolutePath(); // 改:ivImage.setImageBitmap(imageBitmap); // 显示图片 // 使用Glide加载图片 GlideEngine engine = GlideEngine.createGlideEngine(); engine.loadImage(this, imagePath, ivImage); } catch (IOException e) { e.printStackTrace(); ivImage.setBackgroundColor(getResources().getColor(android.R.color.darker_gray)); } } } } } private void initView() { // 初始化界面组件 etTitle=findViewById(R.id.etTitle); etContent=findViewById(R.id.etContent); tvDate=findViewById(R.id.tvDate); ivImage=findViewById(R.id.ivImage); btnAddImage=findViewById(R.id.btnAddImage); btnSave=findViewById(R.id.btnSave); btnDelete=findViewById(R.id.btnDelete); dbHelper=new DatabaseHelper(this); // 初始化数据库助手 // userId=getIntent().getIntExtra("userId",-1); // diaryId=getIntent().getIntExtra("diaryId",-1); } } LoginActivity:package com.example.diaryapp; import android.content.Intent; import android.content.SharedPreferences; 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 androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.example.diaryapp.utils.DatabaseHelper; public class LoginActivity extends AppCompatActivity { private EditText etUsername,etPassword; private CheckBox cbRemember; private DatabaseHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_login); // 初始化界面组件 initView(); // 获取SharedPreferences中的保存信息 SharedPreferences prefs=getSharedPreferences("DiaryApp",MODE_PRIVATE); String username=prefs.getString("username",""); String password=prefs.getString("password",""); // 如果用户名存在但密码不存在,删除用户名 if (username.isEmpty() || password.isEmpty()){ prefs.edit().remove("username").remove("password").apply(); } boolean remember=prefs.getBoolean("remember",false); etUsername.setText(username); etPassword.setText(password); cbRemember.setChecked(remember); // 登录按钮监听 Button btnLogin=findViewById(R.id.btnLogin); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { login(); } }); // 注册按钮监听 Button btnRegister=findViewById(R.id.btnRegister); btnRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(LoginActivity.this, RegisterActivity.class); startActivity(intent); } }); } private void login() { String username=etUsername.getText().toString().trim(); String password=etPassword.getText().toString().trim(); boolean remember=cbRemember.isChecked(); // 验证输入 if (username.isEmpty() || password.isEmpty()){ Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show(); return; } // 保存登录信息到SharedPreferences SharedPreferences prefs=getSharedPreferences("DiaryApp",MODE_PRIVATE); SharedPreferences.Editor editor=prefs.edit(); // 如果记住密码被勾选,则保存密码;否则清空密码 if (remember){ editor.putString("username",username); editor.putString("password",password); }else { // editor.remove("username"); editor.remove("password"); // 确保未勾选时清空密码 } editor.putBoolean("remember",remember); editor.apply(); // 验证用户 if (dbHelper.checkUser(username,password)){ Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show(); Intent intent=new Intent(LoginActivity.this,MainActivity.class); intent.putExtra("username",username); startActivity(intent); finish(); // 关闭登录界面,避免返回时重复登录 }else { Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show(); } } private void initView() { // 初始化界面组件 etUsername=findViewById(R.id.etUsername); etPassword=findViewById(R.id.etPassword); cbRemember=findViewById(R.id.cbRemember); dbHelper=new DatabaseHelper(this); } }MainActivity:package com.example.diaryapp; import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.example.diaryapp.adapters.DiaryAdapter; import com.example.diaryapp.models.Diary; import com.example.diaryapp.utils.DatabaseHelper; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView rvDiaryList; private DiaryAdapter diaryAdapter; private List<Diary> diaryList; private DatabaseHelper dbHelper; private String username; private int userId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); dbHelper=new DatabaseHelper(this); // 获取传递过来的用户名 username=getIntent().getStringExtra("username"); if (username == null){ // 用户名为空,可能是登录流程的问题 Toast.makeText(this, "用户名为空", Toast.LENGTH_SHORT).show(); finish(); return; } userId=dbHelper.getUserId(username); if (userId == -1){ // 用户ID无效,可能是数据库查询的问题 Toast.makeText(this, "用户ID无效", Toast.LENGTH_SHORT).show(); finish(); return; } // 初始化界面组件 initView(); // 加载日记 loadDiaries(); // 添加日记按钮监听 Button btnAddDiary=findViewById(R.id.btnAddDiary); // 在添加日记的按钮点击事件中 btnAddDiary.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MainActivity.this,DiaryActivity.class); intent.putExtra("userId",userId); startActivityForResult(intent,1); } }); } protected void onActivityResult(int requestCode,int resultCode,Intent data){ super.onActivityResult(requestCode,resultCode,data); if (requestCode == 1 && resultCode == RESULT_OK){ // List<Diary> diaries = dbHelper.getDiariesByUserId(userId); // diaryAdapter.updateDiaries(diaries); //更新适配器数据 loadDiaries(); // 刷新日记列表 } } @SuppressLint("NotifyDataSetChanged") private void loadDiaries() { diaryList.clear(); // 清空当前列表 diaryList.addAll(dbHelper.getDiariesByUserId(userId)); // 从数据库重新获取数据 diaryAdapter.notifyDataSetChanged(); // 通知适配器数据已更改 } private void initView() { rvDiaryList=findViewById(R.id.rvDiaryList); rvDiaryList.setLayoutManager(new LinearLayoutManager(this)); diaryList=new ArrayList<>(); // 确保传递 userId 给 DiaryAdapter diaryAdapter=new DiaryAdapter(this,diaryList,userId); rvDiaryList.setAdapter(diaryAdapter); } }RegisterActivity:package com.example.diaryapp; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.example.diaryapp.utils.DatabaseHelper; public class RegisterActivity extends AppCompatActivity { private EditText etUsername,etPassword,etConfirmPassword; private Button btnRegister; private DatabaseHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_register); // 初始化界面组件 initView(); // 注册按钮监听 btnRegister=findViewById(R.id.btnRegister); btnRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { registerUser(); } }); // 返回登录按钮监听 Button btnBack=findViewById(R.id.btnBack); btnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(RegisterActivity.this, LoginActivity.class); startActivity(intent); } }); } private void registerUser() { String username=etUsername.getText().toString().trim(); String password=etPassword.getText().toString().trim(); String confirmPassword=etConfirmPassword.getText().toString().trim(); // 验证输入 if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()){ Toast.makeText(this, "所有字段不能为空", Toast.LENGTH_SHORT).show(); return; } if (!password.equals(confirmPassword)){ Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show(); return; } // 检查用户名是否已被注册 if (dbHelper.checkUser(username,password)){ Toast.makeText(this, "该用户名已被注册", Toast.LENGTH_SHORT).show(); return; } // 注册新用户 long result=dbHelper.addUser(username,password); if (result > 0){ Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show(); Intent intent=new Intent(RegisterActivity.this,LoginActivity.class); startActivity(intent); finish(); }else { Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show(); } } private void initView() { etUsername=findViewById(R.id.etUsername); etPassword=findViewById(R.id.etPassword); etConfirmPassword=findViewById(R.id.etConfirmPassword); dbHelper=new DatabaseHelper(this); } }
06-06
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值