gpt4 book ai didi

java - 致命异常,java lang nullpointerException

转载 作者:行者123 更新时间:2023-12-01 15:04:15 25 4
gpt4 key购买 nike

我的以下代码没有显示错误,但无法执行。

package my.project.mil;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Cbdata extends MainActivity {

public String str;

public void onReceive(Context context, Intent intent) {
//---get the CB message passed in---
Bundle bundle = intent.getExtras();
SmsCbMessage[] msgs = null;
str = "";
if (bundle != null) {
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsCbMessage[pdus.length];
for (int i=0; i<msgs.length; i++) {
msgs[i] = SmsCbMessage.createFromPdu((byte[])pdus[i]);
str += "CB " + msgs[i].getGeographicalScope() + msgs[i].getMessageCode() + msgs[i].getMessageIdentifier() + msgs[i].getUpdateNumber();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}}
}

SQLiteDatabase cd = openOrCreateDatabase("MLIdb", MODE_WORLD_READABLE, null);



Button submit;
Button viewdb;
EditText name;

@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.seventh);

viewdb = (Button) findViewById(R.id.viewdb);
submit = (Button) findViewById(R.id.submit);
name = (EditText) findViewById(R.id.name);

submit.setOnClickListener(new View.OnClickListener()
{

public void onClick(View arg0) {

String locname = name.getText().toString();
if (locname.length()>0)
{

cd.execSQL("CREATE TABLE IF NOT EXITSTS MLITable (CblocationCode INT(10), CblocationName VARCHAR);");
cd.execSQL("INSERT INTO MLITable VALUES (' ',' ');" +str +locname);
Toast.makeText(getBaseContext(), "value successfully entered.", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getBaseContext(), "Please enter the location name, for example 'Home'.", Toast.LENGTH_LONG).show();
}

}
});

cd.close();

viewdb.setOnClickListener(new View.OnClickListener()
{

public void onClick(View arg0) {
// TODO Auto-generated method stub
// here comes the code for viewing the database


}
});
}

}

我的错误日志如下所示,我还检查了我的 list 和 MainActivity 文件,但没有运气。

我的代码应该收集用户的蜂窝塔区域代码并将其保存在用户选择名称的数据库中,该数据库由两个属性组成,一是从用户收集的位置名称,二是小区广播从onReceive方法获取的塔信息,单独的onReceive方法工作正常。

仅当我将数据库集成到代码中时才会出现此问题。
My logcat

谢谢。

最佳答案

SQLiteDatabase cd = openOrCreateDatabase("MLIdata", MODE_WORLD_WRITEABLE, null);

尝试将此行替换为

SQLiteDatabase cd = openOrCreateDatabase("MLIdata", MODE_WORLD_WRITEABLE);

关于java - 致命异常,java lang nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13193630/

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