0
私はFirebase通知を使用しているアプリケーションをビルドしています。通知を受け取ったときに、その通知をローカルストレージ(SQLite)に保存する必要があります。しかし、私はそのデータベースからそれを読んで、それは私にnullポインタを送信します。私のミスはどこですか?ここでなぜ通知を受け取ったときにSQLiteのヌルポインタがありますか?
は私のDataBaseHelperクラスである:ここで
public class DataBaseHelper extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "bazaFire.db";
private static final String TABLE_NAME = "Messages";
private static final String COLUMN_NAME="message_body";
private SQLiteDatabase myDB;
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME
+ "(id INTEGER PRIMARY KEY AUTOINCREMENT, message_body TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void insertMsg(String msg) {
SQLiteDatabase db = getWritableDatabase();
Log.d("getWritable", "Okay");
ContentValues cv = new ContentValues();
cv.put(COLUMN_NAME, msg);
Log.d("Stavljena", msg);
db.insert(TABLE_NAME, null, cv);
db.close();
}
public Cursor query(String table, String[] columns, String selectionArgs, String groupBy, String having, String orderBy){
return myDB.query("Messages", null, null, null, null, null, null);
}
はmyFirebaseMessagingServiceです:
public class myFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG="MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("onMessageReceived", "Pozvana funkcija onMessageReceived");
Log.d(TAG, "From " + remoteMessage.getFrom());
Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
Log.d(TAG, "Location " + remoteMessage.getNotification().getClickAction());
Log.d(TAG, "Value " + remoteMessage.getData().get("click_action"));
sendNotification(remoteMessage);
Log.d("Function called", "sendNotification");
String message=remoteMessage.getNotification().getBody();
Log.d("baza", message);
DataBaseHelper db=new DataBaseHelper(this);
db.insertMsg(message);
}
private void sendNotification(RemoteMessage remoteMessage) {
Intent intent = new Intent(myFirebaseMessagingService.this, MainActivity.class);
intent.putExtra("click_action", "goToFragment1");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notification=new NotificationCompat.Builder(this)
.setSmallIcon(logo)
.setContentText(remoteMessage.getNotification().getBody())
.setContentTitle("Title")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification.build());
}
そして、ここでは私のMainActivityです:
public class MainActivity extends AppCompatActivity {
Button dugme, dugme2, dugme3;
Cursor c;
DataBaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("onCreate", "ONCREATE");
final Intent intent=getIntent();
setContentView(R.layout.activity_main);
String msg = getIntent().getStringExtra("click_action");
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (msg != null) {
if (msg.equals("goToFragment1")) {
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.replace(R.id.myFragment, fragment1);
Log.d("FragmentTransaction", "Fragment je promenjen u onCreate!");
fragmentTransaction.commit();
Log.d("Create", "Kraj onCreatea");
}
}
dugme = (Button) findViewById(R.id.dugme1);
dugme2 = (Button) findViewById(R.id.subscribe);
dugme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment fragment = null;
if (view == dugme) {
fragment = new Fragment1();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.myFragment, fragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
transaction.commit();
}
});
dugme2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseMessaging.getInstance().subscribeToTopic("android");
Log.d("Log", "Uspesno ste se pretplatili");
}
});
try{
c=db.query("Messages", null, null, null, null, null);
if(c.moveToFirst()){
do{
Toast.makeText(MainActivity.this, "id: " + c.getString(0) + "IME: " + c.getString(1), Toast.LENGTH_LONG).show();
}
while(c.moveToNext());{
}
}}catch (Exception e){
throw new Error("Greska!");
}
そして、これは私のlogcatです:
06-15 14:36:26.354 29325-29325/com.example.dev3.notifikacijefragmenti E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.Error: Greska!
at com.example.dev3.notifikacijefragmenti.MainActivity.onCreate(MainActivity.java:82)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.access$700(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
誰でも助けてくれますか?
を元の例外スタックトレースを投稿することができますか? –
この質問はまだ有効ですか?以下の答えはあなたの問題を解決していませんでしたか? – earthw0rmjim
まあ、その答えは私を助けなかったが、私は問題を解決:) – Atenica