在创建领域配置之前,领域数据库/应用程序在启动/调用 realm.init(context) 时崩溃
Realm Database / App crashes in start up / call realm.init(context) before creating a realm configuration
我是 Android 的新手,我正在尝试通过观看教程来创建 Realm 数据库。但是我的应用程序没有启动,它崩溃了。我的主要想法是用户根据他的位置找到附近的商店,如果他想要 select 类型的商店(餐厅,咖啡厅等),如果他找不到他想要的商店,该应用程序可以让他选择添加商店(通过在地图上固定标记,select 名称和类型)。在错误中给我 call realm.init(context) before creating a realm configuration
谁能帮助我,我将不胜感激。这个应用程序是我的毕业设计。我已经走到这一步了;
MainScreen.java
public class MainScreen extends AppCompatActivity {
private ImageButton btn;
Animation scale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
scale = AnimationUtils.loadAnimation(this, R.anim.gps_button_animation);
btn = (ImageButton) findViewById(R.id.ImageButton);
btn.startAnimation(scale);
}
public void InfoActivity(View view) {
Intent intent = new Intent(this, InfoActivity.class);
startActivity(intent);
}
public void MapsActivity (View view){
Intent intent = new Intent(this, MapsActivity.class);
startActivity(intent);
}
public void Anazitisi (View view){
Intent intent = new Intent(this, Anazitisi.class);
startActivity(intent);
}
public void AddShopActivity (View view){
Intent intent = new Intent(this, AddShopActivity.class);
startActivity(intent);
}
}
AddShopActivity.java
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.example.konarx.a11042016.ShopProperties.Shop;
import io.realm.Realm;
import io.realm.RealmResults;
public class AddShopActivity extends MainScreen{
private EditText editText_name;
private Spinner spinner;
private ArrayAdapter<CharSequence> adapter;
private Button button_save;
private Realm realm;
private TextView textView_log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_shop);
realm = Realm.getDefaultInstance();
editText_name = (EditText) findViewById(R.id.editName);
button_save = (Button) findViewById(R.id.buttonSave);
textView_log = (TextView) findViewById(R.id.textView_Log);
spinner = (Spinner)findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this,R.array.eidoskatastimatos, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new android.widget.AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getBaseContext(), adapterView.getItemAtPosition(i)+" selected", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
button_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
save_into_database(editText_name.getText().toString().trim(), spinner.getAdapter().toString().trim());
refresh_views();
}
});
}
private void refresh_views() {
RealmResults<Shop> shopRealmResults = realm.where(Shop.class).findAll();
String output = "";
for (Shop shop: shopRealmResults){
output += shop.toString();
}
textView_log.setText(output);
}
private void save_into_database(final String title, final String type) {
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
Shop shop = bgRealm.createObject(Shop.class);
shop.setTitle(title);
shop.setType(type);
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
Log.v("Database", "saved!");
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
Log.v("Database", error.getMessage());
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
}
Shop.java
package com.example.konarx.a11042016.ShopProperties;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
public class Shop extends RealmObject{
@PrimaryKey
private int id;
private String title;
private String type;
private String Latitude;
private String Longitude;
// Standard getters & setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLatitude() {
return Latitude;
}
public void setLatitude(String latitude) {
Latitude = latitude;
}
public String getLongitude() {
return Longitude;
}
public void setLongitude(String longitude) {
Longitude = longitude;
}
@Override
public String toString() {
return "Shop{" +
"id=" + id +
", title='" + title + '\'' +
", type='" + type + '\'' +
", Latitude='" + Latitude + '\'' +
", Longitude='" + Longitude + '\'' +
'}';
}
}
BaseApplication.java
import android.app.Application;
import io.realm.Realm;
import io.realm.RealmConfiguration;
public class BaseApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
RealmConfiguration config = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(config);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.konarx.a11042016">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.location.gps" />
<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:name=".BaseApplication"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".InfoActivity"
android:label="Πληροφορίες"></activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="Χάρτης"></activity>
<activity
android:name=".Anazitisi"
android:label="Αναζήτηση"></activity>
<activity
android:name=".AddShopActivity"
android:label="Προσθήκη Καταστήματος"></activity>
</application>
</manifest>
public class BaseApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
Realm.init(this); // add this line
RealmConfiguration config = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(config);
}
}
我是 Android 的新手,我正在尝试通过观看教程来创建 Realm 数据库。但是我的应用程序没有启动,它崩溃了。我的主要想法是用户根据他的位置找到附近的商店,如果他想要 select 类型的商店(餐厅,咖啡厅等),如果他找不到他想要的商店,该应用程序可以让他选择添加商店(通过在地图上固定标记,select 名称和类型)。在错误中给我 call realm.init(context) before creating a realm configuration
谁能帮助我,我将不胜感激。这个应用程序是我的毕业设计。我已经走到这一步了;
MainScreen.java
public class MainScreen extends AppCompatActivity {
private ImageButton btn;
Animation scale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
scale = AnimationUtils.loadAnimation(this, R.anim.gps_button_animation);
btn = (ImageButton) findViewById(R.id.ImageButton);
btn.startAnimation(scale);
}
public void InfoActivity(View view) {
Intent intent = new Intent(this, InfoActivity.class);
startActivity(intent);
}
public void MapsActivity (View view){
Intent intent = new Intent(this, MapsActivity.class);
startActivity(intent);
}
public void Anazitisi (View view){
Intent intent = new Intent(this, Anazitisi.class);
startActivity(intent);
}
public void AddShopActivity (View view){
Intent intent = new Intent(this, AddShopActivity.class);
startActivity(intent);
}
}
AddShopActivity.java
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.example.konarx.a11042016.ShopProperties.Shop;
import io.realm.Realm;
import io.realm.RealmResults;
public class AddShopActivity extends MainScreen{
private EditText editText_name;
private Spinner spinner;
private ArrayAdapter<CharSequence> adapter;
private Button button_save;
private Realm realm;
private TextView textView_log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_shop);
realm = Realm.getDefaultInstance();
editText_name = (EditText) findViewById(R.id.editName);
button_save = (Button) findViewById(R.id.buttonSave);
textView_log = (TextView) findViewById(R.id.textView_Log);
spinner = (Spinner)findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this,R.array.eidoskatastimatos, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new android.widget.AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getBaseContext(), adapterView.getItemAtPosition(i)+" selected", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
button_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
save_into_database(editText_name.getText().toString().trim(), spinner.getAdapter().toString().trim());
refresh_views();
}
});
}
private void refresh_views() {
RealmResults<Shop> shopRealmResults = realm.where(Shop.class).findAll();
String output = "";
for (Shop shop: shopRealmResults){
output += shop.toString();
}
textView_log.setText(output);
}
private void save_into_database(final String title, final String type) {
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
Shop shop = bgRealm.createObject(Shop.class);
shop.setTitle(title);
shop.setType(type);
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
Log.v("Database", "saved!");
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
Log.v("Database", error.getMessage());
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
}
Shop.java
package com.example.konarx.a11042016.ShopProperties;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
public class Shop extends RealmObject{
@PrimaryKey
private int id;
private String title;
private String type;
private String Latitude;
private String Longitude;
// Standard getters & setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLatitude() {
return Latitude;
}
public void setLatitude(String latitude) {
Latitude = latitude;
}
public String getLongitude() {
return Longitude;
}
public void setLongitude(String longitude) {
Longitude = longitude;
}
@Override
public String toString() {
return "Shop{" +
"id=" + id +
", title='" + title + '\'' +
", type='" + type + '\'' +
", Latitude='" + Latitude + '\'' +
", Longitude='" + Longitude + '\'' +
'}';
}
}
BaseApplication.java
import android.app.Application;
import io.realm.Realm;
import io.realm.RealmConfiguration;
public class BaseApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
RealmConfiguration config = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(config);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.konarx.a11042016">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.location.gps" />
<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:name=".BaseApplication"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".InfoActivity"
android:label="Πληροφορίες"></activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="Χάρτης"></activity>
<activity
android:name=".Anazitisi"
android:label="Αναζήτηση"></activity>
<activity
android:name=".AddShopActivity"
android:label="Προσθήκη Καταστήματος"></activity>
</application>
</manifest>
public class BaseApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
Realm.init(this); // add this line
RealmConfiguration config = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(config);
}
}