Android Retrofit: java.lang.ExceptionInInitializerError - retrofit

ERROR:
When working with Retrofit, the application crashes with the following error: java.lang.ExceptionInInitializerError.
private const val MAIN_URL = "https://api.main.com"
private const val ADDITIONAL_URL = "/trending"

{/} the whole problem is slash
it should be at the end of the main URL and not at the beginning of the additional.
private const val MAIN_URL = "https://api.main.com/"
private const val ADDITIONAL_URL = "trending"

Related

The return type of an async method must be void

I am currently following a course on how to build a ASP.NET blazor app and have reached a dead end. As the title states, the method CreateMauiApp() requires a void return type. One critical difference is that I am following a course that is 1 year old and does not cover Maui which I am using. Is there any work-around to this issue that anyone knows of?
public static class MauiProgram
{
public static async MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.RegisterBlazorMauiWebView()
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
//builder.Services.AddSingleton<IUserManager, UserManager>();
//builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddSingleton<IUserManager, UserManagerFake>();
builder.Services.AddScoped<ICurrentUserService, CurrentUserService>();
builder.Services.AddBlazorWebView();
var host = builder.Build();
var currentUserService = host.Services.GetRequiredService<ICurrentUserService>();
TestData.CreateTestUser();
currentUserService.CurrentUser = TestData.TestUser;
await host.RunAsync();
}
}
}
Error messages:
Error CS0161 'MauiProgram.CreateMauiApp()': not all code paths return a value UdemyCourseIntro (net6.0-android), UdemyCourseIntro (net6.0-ios), UdemyCourseIntro (net6.0-maccatalyst), UdemyCourseIntro (net6.0-windows10.0.19041) C:\Users\matej\source\repos\UdemyCourseIntro\UdemyCourseIntro\MauiProgram.cs 11 Active
Error CS1983 The return type of an async method must be void, Task, Task, a task-like type, IAsyncEnumerable, or IAsyncEnumerator UdemyCourseIntro (net6.0-android), UdemyCourseIntro (net6.0-ios), UdemyCourseIntro (net6.0-maccatalyst), UdemyCourseIntro (net6.0-windows10.0.19041) C:\Users\matej\source\repos\UdemyCourseIntro\UdemyCourseIntro\MauiProgram.cs 11 Active
So the issue was exactly as someone politely stated in one of the comments. All I had to do is delete the async and return a MauiApp object.
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.RegisterBlazorMauiWebView()
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
//builder.Services.AddSingleton<IUserManager, UserManager>();
builder.Services.AddSingleton<IUserManager, UserManagerFake>();
builder.Services.AddScoped<ICurrentUserService, CurrentUserService>();
builder.Services.AddBlazorWebView();
//builder.Services.AddSingleton<WeatherForecastService>();
var host = builder.Build();
var currentUserService = host.Services.GetRequiredService<ICurrentUserService>();
TestData.CreateTestUser();
currentUserService.CurrentUser = TestData.TestUser;
host.RunAsync();
return host;
}
}
}

Opening SQLite database in Android Studio Kotlin

I'm very new to Kotlin so please bear with me. I want to open a SQLite database or create it if it doesn't exist yet. Afterwards I'd run the appropriate statements to make a table, etc. I'm trying to use SQLiteDatabase. The line of code I'm trying is:
val db = SQLiteDatabase.openOrCreateDatabase("database.db",null, SQLiteDatabase.OPEN_READWRITE)
I get this runtime error when I try the app:
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 1294 SQLITE_CANTOPEN_ENOENT[1294]): Could not open database
I'm not picky about the database, I just want an easy way to read and write to one from within my function.
This is the surrounding code:
package com.google.firebase.codelab.barcode_scanning
// Date/time formatting functions
import android.database.sqlite.SQLiteDatabase
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.item_row.view.*
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
class QrCodeAdapter(private val qrList: ArrayList<QrCode>) : RecyclerView.Adapter<QrCodeAdapter.QrHolder>() {
//
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QrHolder {
return QrHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_row, parent, false))
}
override fun getItemCount() = qrList.size
override fun onBindViewHolder(holder: QrHolder, position: Int) {
//internal var dbHelper = DatabaseHelper(this)
with(qrList[position]) {
var objname = "Barcode Not Found"
var statustext = ""
// status: 0 = no record, 1 = dirty, 2 = clean, 3 = dwell, 4: dwell done
var status = 0
var lastcleaned = ""
// objcategory: 0 = no record, 1 = contact object, 2 = dwell item
var objcategory = 0
var objcategorytext = ""
// initial setup of current date/time
var unixtime = System.currentTimeMillis()
var currenttime = LocalDateTime.now()
var timeformat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
var testtime = currenttime.format(timeformat).toString()
//val db = SQLiteDatabase.openDatabase("//app/maindb.db", null, 0);
val db = SQLiteDatabase.openOrCreateDatabase("database.db",null, null)
//val sql = "SELECT COUNT(*) FROM main"
//val statement = db.compileStatement(sql)
//val result = statement.simpleQueryForLong()
//Convert barcode output to string
var barcodevalue = this.value.toString()
// hide settings
holder.itemView.textSettingsTitle.visibility=View.GONE
holder.itemView.textboxName.visibility=View.GONE
}

AnKo SQLite : populate listview asynchronously from database?

I'm trying to translate my app from Java to Kotlin.
I'm managing database with AnKo SQLite
All is OK except listviews with CursorLoaders : I can't find how to replace CursorLoader while using AnKo SQLite.
(and same problem with expandableListViews)
Can somebody help me please?
OK, here is my solution... I don't know if it is the best :
create a new kotlin class "MyCursorLoader" that extends CursorLoader
set the primary constructor like this :
class MyCursorLoader(
mContext: Context,
val mTableName: String,
var mProjection: Array<String>? = null,
var mSelection: String = "1",
var mSelectionArgs: Array<String> = emptyArray(),
var mGroupBy: String = MySqlHelper.ID,
var mHaving: String = "",
var mSortOrder: String = "${MySqlHelper.ID} ASC",
var mLimit: String = "",
var mDistinct: Boolean = true
): CursorLoader(mContext) {
val mObserver: Loader<Cursor>.ForceLoadContentObserver = Loader<Cursor>(mContext).ForceLoadContentObserver()
var mCancellationSignal: CancellationSignal? = null
override the OnLoadInBackground method with te same code than built-in one, just replacing the val cursor = ContentResolverCompat.query(... line with :
val cursor = MySqlHelper.instance.readableDatabase.query(
mDistinct, mTableName, mProjection, mSelection, mSelectionArgs, mGroupBy, mHaving, mSortOrder, mLimit, mCancellationSignal)
So no need to recreate a dataprovider in manifest, no need to deal with Uri's... I can use MyCursorLoader exactly like built-in CursorLoader, calling it like this :
override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor> {
when (id) {
DAY_HEADER_LOADER ->
return MyCursorLoader(mContext, TABLE_EVENTS, arrayOf(ID, DAY), mGroupBy = DAY, mSortOrder = "$DAY DESC")
...
}
}
Let me know if ther is a better solution.
Hope that can help.

How to split database schema in Realm?

I need to have two different schema in one app using realm db.
It seems that should work below solution:
open class AModel : RealmObject() {
var a: Int = 0
}
open class BModel : RealmObject() {
var b: Int = 0
}
open class XModel : RealmObject() {
var x: Int = 0
}
open class YModel : RealmObject() {
var y: Int = 0
}
#RealmModule(classes = [AModel::class,BModel::class])
open class Schema1
val conf1 = new RealmConfiguration.Builder()
.name("db1.realm")
.schemaVersion(1)
.modules(Schema1())
.build();
#RealmModule(classes = [XModel::class,YModel::class])
open class Schema2
val conf2 = new RealmConfiguration.Builder()
.name("db2.realm")
.schemaVersion(2)
.modules(Schema2())
.build();
but when app starts and calls Realm.setDefaultConfiguration(conf1), it prints below error:
com.example.XModel is not part of the schema for this Realm
So it seems that I miss something in configuration but looking at docs I cannot figure out what. So what I miss?
The problem is that you set the schema version of conf2 as a second version, but the first one was never created. Change it for this:
...
val conf2 = new RealmConfiguration.Builder()
.name("db2.realm")
.schemaVersion(1)
.modules(Schema2())
.build();

Why am I getting random errors in my Minecraft 1.7.10 mod using eclipse?

Sorry, I'm not sure if I'm in the right forum or if I'm wording it right. People may call this vague or something. I won't care.
Anyway, I've started to get random errors after trying something. It didn't turn out well. Here's the code + errors of my main mod file.
package com.harry.MoStuff;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
#Mod(modid = "ms", name = "Mo' Stuff", version = "a-1.0")
public class MoStuff {
public static Item itemRuby;
public static Item itemChain;
public static Item itemRubyEssence;
public static Item itemRubyShard;
public static Item itemRedBull;
public static Block blockRubyOre;
#EventHandler
public void preInit(FMLPreInitializationEvent event) {
//Item/block init and registering
//Config handling
itemRuby = new ItemRuby().setUnlocalizedName("ruby").setTextureName("ms:ruby");
itemChain = new ItemChain().setUnlocalizedName("chain");
blockRubyOre = new BlockRubyOre(Material.rock).setBlockName("ruby_ore").setBlockTextureName("ms:ruby_ore");
itemRubyShard = new ItemRubyShard().setUnlocalizedName("ruby_shard");
itemRubyEssence = new ItemRubyEssence().setUnlocalizedName("ruby_essence");
itemRedBull = new ItemFood(8, 1.0F, true).setUnlocalizedName("red_bull").setTextureName("ms:red_bull");
}
GameRegistry.registerItem(itemRuby, itemRuby.getUnlocalizedName().substring(5));
GameRegistry.registerItem(itemChain, itemChain.getUnlocalizedName().substring(5));
GameRegistry.registerItem(itemRubyShard, itemRubyShard.getUnlocalizedName().substring(5));
GameRegistry.registerItem(itemRubyEssence, itemRubyEssence.getUnlocalizedName().substring(5));
GameRegistry.registerBlock(blockRubyOre, blockRubyOre.getUnlocalizedName().substring(5));
GameRegistry.registerItem(itemRedBull, itemRedBull.getUnlocalizedName().substring(5));
#EventHandler
public void init(FMLInitializationEvent event) {
//Proxy, tile entity, entity, GUI, packet reg.
GameRegistry.addRecipe(new ItemStack(itemRuby), new Object[]{"RRR","RRR","RRR", 'R', itemRubyShard});
GameRegistry.addRecipe(new ItemStack(itemChain), new Object[] {"III","I I","III", 'I', Items.iron_ingot});
GameRegistry.addRecipe(new ItemStack(itemRubyEssence, 5), new Object[]{" "," R "," ", 'R', itemRuby});
}
#EventHandler
public void postInit(FMLPostInitializationEvent event) {
}
}
The errors are:
Multiple markers at this line (38, where GameRegistry.registerItem(itemRuby) and so on):
Syntax error on token ".", > expected.
Syntax error on token "(", < expected.
Syntax error on token ".", { expected.
Syntax error on token ")", delete this token.
Multiple markers at this line (46, where public void init(params) is.)
Syntax error on token "(", ; expected.
Syntax error on token ")", ; expected.
Multiple markers at this line (54, where public void postInit(params) is.)
Syntax error on token "(", ; expected.
Syntax error on token ")", ; expected.
That's all I can say. Thanks in advance.
On line 37, you closed the brace. Close it after all your GameRegistry.register

Resources