gpt4 book ai didi

java.lang.NullPointerException : Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference

转载 作者:太空宇宙 更新时间:2023-11-04 12:41:25 25 4
gpt4 key购买 nike

我正在尝试使用 SQLite 运行应用程序,但每当我单击按钮输入一些数据时,就会出现此消息

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference at com.example.carlos.assigmentcarlos.Register$1.onClick(Register.java:39)

这是我的代码的一部分

package com.example.carlos.assigmentcarlos;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {
Button Login, Register, Delete, Update;
int status = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Login = (Button) findViewById(R.id.Login);
Register = (Button) findViewById(R.id.Reg);
Delete = (Button) findViewById(R.id.Delete);
Update = (Button) findViewById(R.id.Update);
Login.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
status = 1;
Bundle b = new Bundle();
b.putInt("status", status);

Intent i = new Intent(MainActivity.this, Register.class);
i.putExtras(b);
startActivity(i);

}
});

Register.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Register.class);
startActivity(i);
}
});
Update.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
status = 2;
Bundle b = new Bundle();
b.putInt("status", status);
Intent i = new Intent(MainActivity.this, Register.class);
i.putExtras(b);
startActivity(i);
}
});
Delete.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
status = 3;
Bundle b = new Bundle();
b.putInt("status", status);
Intent i = new Intent(MainActivity.this, Register.class);
i.putExtras(b);
startActivity(i);

}
});
}
}

注册类

package com.example.carlos.assigmentcarlos;

/**
* Created by Carlos on 22/04/2016.
*/


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity {
Button Login;
EditText USERNAME,USERPASS;
String username,userpass;
Context CTX = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
Login = (Button) findViewById(R.id.b_login);
USERNAME = (EditText) findViewById(R.id.user_name);
USERPASS = (EditText) findViewById(R.id.user_pass);
Login.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {


Bundle b = getIntent().getExtras();
int status = b.getInt("status");
if(status == 1)
{
Toast.makeText(getBaseContext(), "Please wait...", Toast.LENGTH_LONG).show();
username = USERNAME.getText().toString();
userpass = USERPASS.getText().toString();
DatabaseOperations DOP = new DatabaseOperations(CTX);
Cursor CR = DOP.getInformation(DOP);
CR.moveToFirst();
boolean loginstatus = false;
String NAME = "";
do
{
if(username.equals(CR.getString(0))&& (userpass.equals(CR.getString(1))))
{
loginstatus = true;
NAME = CR.getString(0);
}

}while(CR.moveToNext());
if(loginstatus)
{
Toast.makeText(getBaseContext(), "Login Success----\n Welcome "+NAME, Toast.LENGTH_LONG).show();
finish();
}
else
{
Toast.makeText(getBaseContext(), "Login Failed---- ", Toast.LENGTH_LONG).show();
finish();
}



}
else if(status == 2)
{
Toast.makeText(getBaseContext(), "Please wait...", Toast.LENGTH_LONG).show();
username = USERNAME.getText().toString();
userpass = USERPASS.getText().toString();
DatabaseOperations DOP = new DatabaseOperations(CTX);
Cursor CR = DOP.getInformation(DOP);
CR.moveToFirst();
boolean loginstatus = false;
String NAME = "";
do
{
if(username.equals(CR.getString(0))&& (userpass.equals(CR.getString(1))))
{
loginstatus = true;
NAME = CR.getString(0);
}

}while(CR.moveToNext());
if(loginstatus)
{
Toast.makeText(getBaseContext(), "Login Success----\n Welcome "+NAME, Toast.LENGTH_LONG).show();

Intent i = new Intent("update_filter");
Bundle BN = new Bundle();
BN.putString("user_name",NAME );
BN.putString("user_pass",userpass );
i.putExtras(BN);
startActivity(i);
finish();
}
else
{
Toast.makeText(getBaseContext(), "Login Failed---- ", Toast.LENGTH_LONG).show();
finish();
}






}
else if(status == 3)
{
Toast.makeText(getBaseContext(), "Please wait...", Toast.LENGTH_LONG).show();
username = USERNAME.getText().toString();
userpass = USERPASS.getText().toString();
DatabaseOperations DOP = new DatabaseOperations(CTX);
Cursor CR = DOP.getInformation(DOP);
CR.moveToFirst();
boolean loginstatus = false;
String NAME = "";
do
{
if(username.equals(CR.getString(0))&& (userpass.equals(CR.getString(1))))
{
loginstatus = true;
NAME = CR.getString(0);
}

}while(CR.moveToNext());
if(loginstatus)
{
Toast.makeText(getBaseContext(), "Login Success----\n Welcome "+NAME, Toast.LENGTH_LONG).show();
Intent i = new Intent("delete_filter");
Bundle B = new Bundle();
B.putString("user_name",NAME );
i.putExtras(B);
startActivity(i);

finish();
}
else
{
Toast.makeText(getBaseContext(), "Login Failed---- ", Toast.LENGTH_LONG).show();
finish();
}




Intent i = new Intent("delete_filter");
startActivity(i);
}

}
});
}

}

数据库操作

package com.example.carlos.assigmentcarlos;

/**
* Created by Carlos on 22/04/2016.
*/


import com.example.carlos.assigmentcarlos.tableData.TableInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.SyncStateContract.Columns;
import android.util.Log;

public class DatabaseOperations extends SQLiteOpenHelper {
public static final int database_version = 1;
public String CREATE_QUERY = "CREATE TABLE "+TableInfo.TABLE_NAME+"("+TableInfo.USER_NAME+" TEXT,"+TableInfo.USER_PASS+" TEXT);";

public DatabaseOperations(Context context) {
super(context, TableInfo.DATABASE_NAME, null, database_version);
Log.d("Database operations", "Database created");

}

@Override
public void onCreate(SQLiteDatabase sdb) {

sdb.execSQL(CREATE_QUERY);
Log.d("Database operations", "Table created");

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}

public void putInformation(DatabaseOperations dop,String name,String pass)

{
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableInfo.USER_NAME, name);
cv.put(TableInfo.USER_PASS, pass);
long k = SQ.insert(TableInfo.TABLE_NAME, null, cv);
Log.d("Database operations", "One raw inserted");



}

public Cursor getInformation(DatabaseOperations dop)
{
SQLiteDatabase SQ = dop.getReadableDatabase();
String[] coloumns = {TableInfo.USER_NAME,TableInfo.USER_PASS};
Cursor CR = SQ.query(TableInfo.TABLE_NAME,coloumns, null, null, null, null, null);
return CR;


}

public Cursor getUserPass(DatabaseOperations DOP, String user)
{
SQLiteDatabase SQ = DOP.getReadableDatabase();
String selection = TableInfo.USER_NAME +" LIKE ?";
String coloumns[] = {TableInfo.USER_PASS};
String args[] = {user};
Cursor CR = SQ.query(TableInfo.TABLE_NAME, coloumns, selection, args, null, null, null);
return CR;

}

public void deleteUser(DatabaseOperations DOP, String user, String pass)
{
String selection = TableInfo.USER_NAME+ " LIKE ? AND "+TableInfo.USER_PASS +" LIKE ?";
//String coloumns[] = {TableInfo.USER_PASS};
String args[] = {user,pass};
SQLiteDatabase SQ = DOP.getWritableDatabase();
SQ.delete(TableInfo.TABLE_NAME, selection, args);

}

public void updateUserInfo(DatabaseOperations DOP, String user_name, String user_pass, String new_user_name )
{
SQLiteDatabase SQ = DOP.getWritableDatabase();
String selection = TableInfo.USER_NAME+ " LIKE ? AND "+TableInfo.USER_PASS +" LIKE ?";
String args[] = {user_name,user_pass};
ContentValues values = new ContentValues();
values.put(TableInfo.USER_NAME, new_user_name);
SQ.update(TableInfo.TABLE_NAME, values, selection, args);


}


}

最佳答案

在此代码块中,您没有传递 Bundle

Register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Register.class);
startActivity(i);
}
});

您需要将Bundle添加到上述 block

Bundle b = new Bundle();
b.putInt("status", status);
Intent i = new Intent(MainActivity.this, Register.class);
i.putExtras(b);
startActivity(i);

或者考虑没有BundleIntent

int status;
try{
Bundle b = getIntent().getExtras();
status = b.getInt("status");
} catch(Exception ex){
status = -1; //Or some error status //
}

关于java.lang.NullPointerException : Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36799018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com