gpt4 book ai didi

android - 在创建 Realm 配置之前, Realm 数据库/应用程序在启动/调用 realm.init(context) 时崩溃

转载 作者:搜寻专家 更新时间:2023-10-30 21:49:43 28 4
gpt4 key购买 nike

我是 Android 的新手,我正在尝试通过观看教程来创建 Realm 数据库。但是我的应用程序没有启动,它崩溃了。我的主要想法是用户根据他的位置找到附近的商店,如果他想要(餐厅,咖啡厅等)选择商店类型,如果他找不到他想要的商店,该应用程序让他可以选择添加商店(通过在 map 中固定标记,选择名称和类型)。在错误中给我 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 配置之前, Realm 数据库/应用程序在启动/调用 realm.init(context) 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40971587/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com