私はこの未知の問題を抱えています。問題を解決できないのは、Android で sqlite から値を取得すると、2 つのクラスで同じことを実行しても問題なく動作するということですが、メソッドが機能しないと、nullポインター例外が発生します。メソッドにパラメーターを渡すと、メソッドを除くすべての場所で機能し、エラーが発生してアプリが閉じます。
私のデータベースはここで動作します
public class Activity5 extends Activity {
public static String tableler;
final Context cont = this;
static String a1,a2,a3,a4,a5,a6,a7,a8;
static String out,out2,out3,out4,out5,out6,out7,out8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity5);
final EditText edt = (EditText)findViewById(R.id.replayedit);
Button btnsave = (Button) findViewById(R.id.btnSaveChange);
Button btncancel = (Button) findViewById(R.id.btnCancel);
SQLiteDatabase db = openOrCreateDatabase("ComDB", MODE_PRIVATE, null);
Cursor c = db.rawQuery("SELECT REPLAYS FROM Rtm WHERE ID = "+Activity3.replays, null);
c.moveToFirst();
out = c.getString(c.getColumnIndex("REPLAYS"));
db.close();
c.close();
if(Activity3.replays.contains("1"))
{
edt.setText("great"+out);
}if(Activity3.replays.contains("2"))
{
edt.setText("great"+out);
}if(Activity3.replays.contains("3"))
{
edt.setText("great"+out);
}if(Activity3.replays.contains("4"))
{
edt.setText("great"+out);
}if(Activity3.replays.contains("5"))
{
edt.setText("great"+out);
}if(Activity3.replays.contains("6"))
{
edt.setText("great"+out);
}if(Activity3.replays.contains("7"))
{
edt.setText("great"+out);
}if(Activity3.replays.contains("8"))
{
edt.setText("great"+out);
}
// this.vieww();
// this.edit();
btnsave.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String updates = edt.getText().toString();
updater(Activity3.replays, updates);
Toast.makeText(cont, "Your Implementation complete",Toast.LENGTH_LONG ).show();
}
});
btncancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
public void updater(String sid,String vals)
{
String tableNb = "REPLAYS";
String tablela = "Rtm";
String ID = "ID";
SQLiteDatabase db = openOrCreateDatabase("ComDB", MODE_PRIVATE, null);
ContentValues cv = new ContentValues();
cv.put(tableNb, vals);
db.update(tablela, cv, ID+"="+sid, null);
// Cursor c = db.rawQuery("SELECT REPLAYS FROM Rtm WHE", selectionArgs)
db.close();
}
}
ここでエラーが発生し、アプリを閉じます
public class SendGlass extends Activity{
static String replay;
public void sms (String phone,String Sms )
{
SmsManager smsManager = SmsManager.getDefault();
String phoneNo = phone;
String sms = Sms;
String api = "You Entered one";
String api2 = "You Entered two";
String api3 = "Nothing great";
String rep = sms.replace(phoneNo+" :", "");
SQLiteDatabase db = openOrCreateDatabase("ComDB", MODE_PRIVATE, null);
Cursor c = db.rawQuery("SELECT REPLAYS FROM Rtm WHERE ID = 1", null);
while(c.moveToNext())
{
replay = c.getString(c.getColumnIndex("REPLAYS"));
if(rep.equals("1"))
{
// String replay = c.getString(c.getColumnIndex("REPLAYS"));
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}if(rep.equals("2"))
{
// String replay = c.getString(c.getColumnIndex("REPLAYS"));
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}if(rep.equals("3"))
{
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}if(rep.equals("4"))
{
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}if(rep.equals("5"))
{
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}if(rep.equals("6"))
{
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}if(rep.equals("7"))
{
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}if(rep.equals("8"))
{
smsManager.sendTextMessage(phoneNo, null, replay, null, null);
}
}
}
sendGlassのパラメーターを取得するBroadCastクラス
public class BroadCastRecivers extends BroadcastReceiver{
static String from;
static String body;
@Override
public void onReceive(Context cont, Intent intent) {
// TODO Auto-generated method
// SmsManager smsManager = SmsManager.getDefault();
// TODO Auto-generated method s
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
from = str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
body = str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(cont, str, Toast.LENGTH_LONG).show();
// ret = str;
// int tos = Integer.valueOf(body);
// obj.Decision(from, tos);
// sms(from, body);
// String phoneNo = phone;
// String sms = Sms;
// SmsManager smsManager = SmsManager.getDefault();
// smsManager.sendTextMessage(phoneNo, null, sms, null, null);
/** Activity5 obj = new Activity5();
String rep = body.replace("from"+" :", "");
if(rep.equals("1"))
{
Activity3.replays = rep;
obj.select(from, rep);
}if(rep.equals("2"))
{
Activity3.replays = rep;
obj.select(from, rep);
}if(rep.equals("3"))
{
Activity3.replays = rep;
obj.select(from, rep);
}if(rep.equals("4"))
{
Activity3.replays = rep;
obj.select(from, rep);
}if(rep.equals("5"))
{
Activity3.replays = rep;
obj.select(from, rep);
}if(rep.equals("6"))
{
Activity3.replays = rep;
obj.select(from, rep);
}if(rep.equals("7"))
{
Activity3.replays = rep;
obj.select(from, rep);
}if(rep.equals("8"))
{
Activity3.replays = rep;
obj.select(from, rep);
}else
{
rep = "Sorry man";
obj.select(from, rep);
}
**/
from = "somehing";
body = "exception";
SendGlass obj = new SendGlass();
obj.sms(from, body);
} }
私のログキャット。
12-05 23:37:38.507: D/AndroidRuntime(267): Shutting down VM
12-05 23:37:38.507: W/dalvikvm(267): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-05 23:37:38.597: E/AndroidRuntime(267): FATAL EXCEPTION: main
12-05 23:37:38.597: E/AndroidRuntime(267): java.lang.RuntimeException: Unable to start receiver automator.mator.BroadCastRecivers: java.lang.NullPointerException
12-05 23:37:38.597: E/AndroidRuntime(267): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
12-05 23:37:38.597: E/AndroidRuntime(267): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
12-05 23:37:38.597: E/AndroidRuntime(267): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
12-05 23:37:38.597: E/AndroidRuntime(267): at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 23:37:38.597: E/AndroidRuntime(267): at android.os.Looper.loop(Looper.java:123)
12-05 23:37:38.597: E/AndroidRuntime(267): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-05 23:37:38.597: E/AndroidRuntime(267): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 23:37:38.597: E/AndroidRuntime(267): at java.lang.reflect.Method.invoke(Method.java:521)
12-05 23:37:38.597: E/AndroidRuntime(267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-05 23:37:38.597: E/AndroidRuntime(267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-05 23:37:38.597: E/AndroidRuntime(267): at dalvik.system.NativeStart.main(Native Method)
12-05 23:37:38.597: E/AndroidRuntime(267): Caused by: java.lang.NullPointerException
12-05 23:37:38.597: E/AndroidRuntime(267): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
12-05 23:37:38.597: E/AndroidRuntime(267): at automator.mator.SendGlass.<init>(SendGlass.java:12)
12-05 23:37:38.597: E/AndroidRuntime(267): at automator.mator.BroadCastRecivers.onReceive(BroadCastRecivers.java:100)
12-05 23:37:38.597: E/AndroidRuntime(267): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
12-05 23:37:38.597: E/AndroidRuntime(267): ... 10 more
12-05 23:37:43.027: I/Process(267): Sending signal. PID: 267 SIG: 9
ハードコードされた文字列を使用すると、データベースから値を取得するときにのみエラーが発生します。