私はアンドロイドの新機能です。レルムからデータを取得する際に問題が発生しました。私はretrofitを使用し、Realm(Successful for Sqlite)を使用してデータを保存したいが、データベースからデータを取得しているうちに、私は投稿したエラーがある。レルムのデータを取得する際にエラーが発生しました
N E/AndroidRuntime:致命的な例外:メイン プロセス:np.com.rabindraacharya.retrofitintern、PID:17091
package np.com.rabindraacharya.retrofitintern.ui;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import java.util.List;
import np.com.rabindraacharya.retrofitintern.Controller.RestManager;
import np.com.rabindraacharya.retrofitintern.R;
import np.com.rabindraacharya.retrofitintern.model.Flower;
import np.com.rabindraacharya.retrofitintern.model.adapter.FlowerAdapter;
import np.com.rabindraacharya.retrofitintern.model.helper.Constants;
import np.com.rabindraacharya.retrofitintern.model.helper.FlowerDatabase;
import np.com.rabindraacharya.retrofitintern.model.helper.Utils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity
extends AppCompatActivity
implements FlowerAdapter.FlowersClickListener {
private RecyclerView recyclerView;
private RestManager mManager;
FlowerAdapter mFlowerAdapter;
FlowerDatabase mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
});
configViews();
mManager = new RestManager();
mDatabase = new FlowerDatabase();
if(Utils.isNetworkAvailable(getApplicationContext())) {
getFeed();
} else {
getFeedFromDatabase();
}
}
private void getFeedFromDatabase() {
Log.e("RetData", "RetData");
List<Flower> flowerList = mDatabase.getFlower();
for(int i = 0; i < flowerList.size(); i++) {
Flower flower = flowerList.get(i);
mFlowerAdapter.addFlower(flower);
}
}
private void configViews() {
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
LinearLayoutManager verticalLayoutmanager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(verticalLayoutmanager);
mFlowerAdapter = new FlowerAdapter(this);
recyclerView.setAdapter(mFlowerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if(id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(int position) {
Flower selectedFlower = mFlowerAdapter.getSelectedFlower(position);
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra(Constants.REFERENCE.FLOWER, selectedFlower);
startActivity(intent);
}
public void getFeed() {
Call<List<Flower>> listCall = mManager.getFlowerService().getAllFlowers();
listCall.enqueue(new Callback<List<Flower>>() {
@Override
public void onResponse(Call<List<Flower>> call, Response<List<Flower>> response) {
if(response.isSuccessful()) {
List<Flower> flowerList = response.body();
for(int i = 0; i < flowerList.size(); i++) {
Flower flower = flowerList.get(i);
SaveIntoDatabase task = new SaveIntoDatabase();
task.execute(flower);
mFlowerAdapter.addFlower(flower);
}
} else {
int st = response.code();
switch(st) {
}
}
}
@Override
public void onFailure(Call<List<Flower>> call, Throwable t) {
}
});
}
public class SaveIntoDatabase
extends AsyncTask<Flower, Flower, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(Flower... params) {
Flower flower = params[0];
try {
//InputStream stream = new URL("http://services.hanselandpetal.com/photos/" + flower.getPhoto()).openStream();
// Bitmap bitmap = BitmapFactory.decodeStream(stream);
//flower.setPicture(bitmap);
mDatabase.addFlower(flower);
Log.e("ErrorAft", "ErrorAft");
} catch(Exception e) {
}
return null;
}
}
}
は
package np.com.rabindraacharya.retrofitintern.model.helper;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import io.realm.Realm;
import io.realm.RealmResults;
import np.com.rabindraacharya.retrofitintern.model.Flower;
import np.com.rabindraacharya.retrofitintern.model.adapter.FlowerAdapter;
/**
* Created by Rabindra on 8/12/2016.
*/
public class FlowerDatabase
extends AppCompatActivity {
private Realm myRealm;
private int productID;
private static FlowerDatabase flowerDatabase;
private static List<Flower> arrayListFlower = new ArrayList<>();
private FlowerAdapter flowerAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myRealm = Realm.getDefaultInstance();
flowerDatabase = this;
}
public void addFlower(Flower model) {
Log.e("RealmData", "RealmData");
myRealm.beginTransaction();
Flower flower = myRealm.createObject(Flower.class);
flower.setProductId(model.getProductId());
flower.setCategory(model.getCategory());
flower.setName(model.getName());
flower.setPrice(model.getPrice());
flower.setInstruction(model.getInstruction());
arrayListFlower.add(flower);
myRealm.commitTransaction();
flowerAdapter.notifyDataSetChanged();
}
public List<Flower> getFlower() {
Flower model = new Flower();
productID = model.getProductId();
RealmResults<Flower> results = myRealm.where(Flower.class).findAll();
myRealm.beginTransaction();
for(int i = 0; i < results.size(); i++) {
arrayListFlower.add(results.get(i));
}
if(results.size() > 0) {
productID = myRealm.where(Flower.class).max("id").intValue() + 1;
}
myRealm.commitTransaction();
flowerAdapter.notifyDataSetChanged();
return arrayListFlower;
}
}
エラーLogCatをFlowerDatabase
java.lang.RuntimeException:アクティビティを開始できません ComponentInfo {np.com.rabindraacharya.retrofitintern/np.com.rabindraacharya.retrofitintern.ui.MainActivity} java.lang.NullPointerExceptionが でandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317) でandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2375) android.app.ActivityThread.access $ 900(ActivityThread.java:164) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1268) android.os.Handler.dispatchMessage(Handler.java:102)の とandroid.os.Looper.loop(Looper.java:157)012 android.app.ActivityThread.main(ActivityThread.java:5377)の のjava.lang.reflect.Method.invokeNative(ネイティブメソッド) のjava.lang.reflect.Method.invoke(Method.java:515) com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1265) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) at dalvik.system.NativeStart。 main(ネイティブメソッド) 原因:java.lang.NullPointerException at np.com.rabindraacharya.retrofitintern.model.helper.FlowerDatabase.getFlower(FlowerDataba se.java:70) でnp.com.rabindraacharya.retrofitintern.ui.MainActivity.getFeedFromDatabase(MainActivity.java:69) np.com.rabindraacharya.retrofitintern.ui.MainActivity.onCreate(MainActivity.javaで: 63) android.app.Activity.performCreate(Activity.java:5428) でandroid.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) でandroid.app.ActivityThread.performLaunchActivity(ActivityThread.javaで: 2281) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2375)android.app.ActivityThread.access $ 150(ActivityThread.java:164) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1268) at android.os.Handler.dispatchMessage(Handler.java: 102) android.os.Looper.loop(Looper.java:157) android.app.ActivityThread.main(ActivityThread.java:5377) at java.lang。メソッド.invokeNative(ネイティブメソッド) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1265)この行は、ここで
myRealm.where(Flower.class).max("id").intValue() + 1;
が無い花の場合はNullPointerExceptionがスローされます
1.)どのレルムのバージョンを使用しているのですか?2)そのクラスが完全に間違っているので、あなたは 'FlowerDatabase'で何をやろうとしましたか? – EpicPandaForce
最後に問題を解決します。おかげであなたのコメント –