我的以下代码显示没有错误,但未执行。
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
}
});
}
}
我的错误日志如下所示,我还检查了我的清单和MainActivity文件,但没有运气。
我的代码应该收集用户手机信号塔区域代码并将其保存在用户选择名称的数据库上,该数据库包含两个属性,一个是从用户收集的位置名称,另一个是小区广播从onReceive方法获得的塔信息,onReceive方法单独工作。
只有在我将数据库集成到我的代码中时才会出现此问题
谢谢。
答案 0 :(得分:1)
SQLiteDatabase cd = openOrCreateDatabase("MLIdata", MODE_WORLD_WRITEABLE, null);
尝试用
替换此行SQLiteDatabase cd = openOrCreateDatabase("MLIdata", MODE_WORLD_WRITEABLE);
答案 1 :(得分:0)
我在上面的代码中的错误,
最终正确的代码是,
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";
}}
}
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)
{
SQLiteDatabase cd = openOrCreateDatabase("mydata", MODE_WORLD_READABLE, null);
cd.execSQL("CREATE TABLE IF NOT EXISTS MLITable (CblocationCode INT(10), CblocationName VARCHAR);");
cd.execSQL("INSERT INTO MLITable VALUES ('"+str+ "','"+locname+ "');");
Toast.makeText(getBaseContext(), "value successfully entered.", Toast.LENGTH_LONG).show();
cd.close();
}
else
{
Toast.makeText(getBaseContext(), "Please enter the location name, for example 'Home'.", Toast.LENGTH_LONG).show();
}
}
});
viewdb.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0) {
// TODO Auto-generated method stub
// here comes the code for viewing the database
}
});
}
}