Android: Crash when setting up media route button - android

I'm having pretty much the same problem as Android: Cast SDK v3 Crashing in Release build only.
The key difference is that my project does it while i'm just debugging and does it on this line
CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), mMediaRouteButton);
I have tried making all my variables public but that doesn't do anything. The full code is
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMediaRouteButton = (MediaRouteButton) findViewById(R.id.media_route_button);
CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), mMediaRouteButton);
mCastContext = CastContext.getSharedInstance(this);
mSelector = new MediaRouteSelector.Builder()
// These are the framework-supported intents
.addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO)
.addControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO)
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.build();
mMediaRouter = MediaRouter.getInstance(this);
}
Also I find it worth mentioning that this code worked and I can not for the life of me figure out what caused it to stop working. As far as I can tell it stopped working when I invalidated the cache in Android Studio.
This is the error I'm getting
java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackage.package/mypackage.package.MainActivity}: java.lang.IllegalStateException: Failed to initialize CastContext.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalStateException: Failed to initialize CastContext.
at com.google.android.gms.cast.framework.CastContext.zzbb(Unknown Source)
at com.google.android.gms.cast.framework.CastContext.getSharedInstance(Unknown Source)
at com.google.android.gms.cast.framework.CastButtonFactory.setUpMediaRouteButton(Unknown Source)
at mypackage.package.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Caused by: java.lang.IllegalAccessException: java.lang.Class<mypackage.package.CastOptionsProvider> is not accessible from java.lang.Class<com.google.android.gms.cast.framework.CastContext>
at java.lang.Class.newInstance(Native Method)
at com.google.android.gms.cast.framework.CastContext.zzbb(Unknown Source) 
at com.google.android.gms.cast.framework.CastContext.getSharedInstance(Unknown Source) 
at com.google.android.gms.cast.framework.CastButtonFactory.setUpMediaRouteButton(Unknown Source) 
at mypackage.package.MainActivity.onCreate(MainActivity.java:51) 
at android.app.Activity.performCreate(Activity.java:6664) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

There might be an issue on how you initialize media CastContext, to properly initialize CastContext, the application must have the implements OptionsProvider interface:
package com.example.app;
public class CastOptionsProvider implements OptionsProvider {
#Override
public CastOptions getCastOptions(Context appContext) {
...
}
}
and specify its fully qualified class name in the AndroidManifest.xml with the key OPTIONS_PROVIDER_CLASS_NAME_KEY:
...
<meta-data
android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
android:value="com.example.app.CastOptionsProvider" />
...
Note: All public methods must be called from the main thread.
I did some research and found this related SO ticket which discuss how to initialize CastContext: How to initialize CastContext outside of onCreate method

This was fixed by making the CastOptionsProvider public. I don't know why this problem occurred to begin with, because it was working fine when it wasn't public until it didn't. I didn't touch the CastOptionsProvider on the build where it started failing.
My CastOptionsProvider now looks like this
public class CastOptionsProvider implements OptionsProvider {
#Override
public CastOptions getCastOptions(Context appContext) {
CastOptions castOptions = new CastOptions.Builder()
.setReceiverApplicationId(appContext.getString(R.string.app_id))
.build();
return castOptions;
}
#Override
public List<SessionProvider> getAdditionalSessionProviders(Context context) {
return null;
}
}

If this is happening only in release buildtype then it is related to proguard. Please add the CastOptionsProvider class to the proguard file and recheck
-keep class abc.xyz.videocast.CastOptionsProvider { *; }
For more safety these classes can also be added
-keep class android.support.** { *; }
-keep class com.google.** { *; }

Related

Couldn't use getRefrence() with firebase in android

iv'e been stuck for hours trying to figure out what's the problem...
every time I try to access the reference the up crushes.
tried to open like 5 new projects and firebases....
MainActivity( only realated stuff):
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button ba;
FirebaseDatabase database;
DatabaseReference myRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ba=findViewById(R.id.ba);
ba.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if(view==ba)
{
database = FirebaseDatabase.getInstance();
myRef=database.getReference("Foods"); //this is where it crushes
// myRef.setValue("ba");
}
}
}
The Run Log:
E/RunLoop: Uncaught exception in Firebase Database runloop (3.0.0). Please report to firebase-database-client#google.com
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/FirebaseApp$IdTokenListener;
at com.google.firebase.database.android.AndroidPlatform.newAuthTokenProvider(com.google.firebase:firebase-database##16.0.4:112)
at com.google.firebase.database.core.Context.ensureAuthTokenProvider(com.google.firebase:firebase-database##16.0.4:246)
at com.google.firebase.database.core.Context.initServices(com.google.firebase:firebase-database##16.0.4:98)
at com.google.firebase.database.core.Context.freeze(com.google.firebase:firebase-database##16.0.4:77)
at com.google.firebase.database.core.RepoManager.createLocalRepo(com.google.firebase:firebase-database##16.0.4:92)
at com.google.firebase.database.core.RepoManager.createRepo(com.google.firebase:firebase-database##16.0.4:42)
at com.google.firebase.database.FirebaseDatabase.ensureRepo(com.google.firebase:firebase-database##16.0.4:357)
at com.google.firebase.database.FirebaseDatabase.getReference(com.google.firebase:firebase-database##16.0.4:201)
at com.example.caloriccalcyair1.MainActivity.onClick(MainActivity.java:43)
at android.view.View.performClick(View.java:7869)
at android.widget.TextView.performClick(TextView.java:14958)
at android.view.View.performClickInternal(View.java:7838)
at android.view.View.access$3600(View.java:886)
at android.view.View$PerformClick.run(View.java:29362)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8016)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.FirebaseApp$IdTokenListener" on path: DexPathList[[zip file "/data/app/com.example.caloriccalcyair1-mv4wi0pKnsqKEHcy89c_hA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.caloriccalcyair1-mv4wi0pKnsqKEHcy89c_hA==/lib/arm64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.google.firebase.database.android.AndroidPlatform.newAuthTokenProvider(com.google.firebase:firebase-database##16.0.4:112) 
at com.google.firebase.database.core.Context.ensureAuthTokenProvider(com.google.firebase:firebase-database##16.0.4:246) 
at com.google.firebase.database.core.Context.initServices(com.google.firebase:firebase-database##16.0.4:98) 
at com.google.firebase.database.core.Context.freeze(com.google.firebase:firebase-database##16.0.4:77) 
at com.google.firebase.database.core.RepoManager.createLocalRepo(com.google.firebase:firebase-database##16.0.4:92) 
at com.google.firebase.database.core.RepoManager.createRepo(com.google.firebase:firebase-database##16.0.4:42) 
at com.google.firebase.database.FirebaseDatabase.ensureRepo(com.google.firebase:firebase-database##16.0.4:357) 
at com.google.firebase.database.FirebaseDatabase.getReference(com.google.firebase:firebase-database##16.0.4:201) 
at com.example.caloriccalcyair1.MainActivity.onClick(MainActivity.java:43) 
at android.view.View.performClick(View.java:7869) 
at android.widget.TextView.performClick(TextView.java:14958) 
at android.view.View.performClickInternal(View.java:7838) 
at android.view.View.access$3600(View.java:886) 
at android.view.View$PerformClick.run(View.java:29362) 
at android.os.Handler.handleCallback(Handler.java:883) 
at android.os.Handler.dispatchMessage(Handler.java:100) 
at android.os.Looper.loop(Looper.java:237) 
at android.app.ActivityThread.main(ActivityThread.java:8016) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076) 
i followed the firebase installation step by step and verifyed the project.
also added the firebase to the porject with the Tools option, so everything should be ok
image of the firebase database
Instead of using your code.
Try using this:
database = FirebaseDatabase.getInstance().getReference().child("Foods");
And also try wrapping your on click listener like this inside onCreate
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
...
}
});

Got an error using ListPreference in Android

I have a question about PreferenceScreen in Android Studio. I used MultiSelectListPreference and did not get an error, however when I used ListPreference, I got an error and does not open setting activity.
<PreferenceCategory
android:title="Time Table Setting">
<ListPreference
android:key="Header_List"
android:entries="#array/setting_header_list"
android:entryValues="#array/setting_header_values"
android:title="Display Dates"
android:summary="Monday to Friday, Saturday or Sunday"/>
</PreferenceCategory>
<Preference
android:key="Restart"
android:title="Save"/>
If you have any suggestions, Please help me out. Thank you
Here is the error message that I've got
2020-03-21 22:06:26.212 14206-14206/com.pythontaro.personaltimetable E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.name, PID: 14206
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.name.appname/com.name.appname.setting.SettingsActivity}: java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.String
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.String
at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:225)
at androidx.preference.Preference.getPersistedString(Preference.java:1686)
at androidx.preference.ListPreference.onSetInitialValue(ListPreference.java:255)
at androidx.preference.Preference.onSetInitialValue(Preference.java:1614)
at androidx.preference.Preference.dispatchSetInitialValue(Preference.java:1587)
at androidx.preference.Preference.onAttachedToHierarchy(Preference.java:1311)
at androidx.preference.Preference.onAttachedToHierarchy(Preference.java:1326)
at androidx.preference.PreferenceGroup.addPreference(PreferenceGroup.java:249)
at androidx.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:170)
at androidx.preference.PreferenceInflater.rInflate(PreferenceInflater.java:345)
at androidx.preference.PreferenceInflater.rInflate(PreferenceInflater.java:346)
at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:157)
at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:109)
at androidx.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:216)
at androidx.preference.PreferenceFragmentCompat.addPreferencesFromResource(PreferenceFragmentCompat.java:361)
at com.name.appname.setting.SettingsActivity$SettingsFragment.onCreatePreferences(SettingsActivity.java:28)
at androidx.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:160)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175)
at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1255)
at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1138)
at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:136)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1989)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2629)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
at android.app.Activity.performStart(Activity.java:6679)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
This is code in Setting Activity Class
public class SettingsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.settings);
}
}}
This is code for string value
<!--Setting-->
<string-array name="setting_header_list">
<item>"Friday(default)"</item>
<item>"Saturday"</item>
<item>"Sunday"</item>
</string-array>
<string-array name="setting_header_values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
This is the code of line 225 in SharedPreferencesImpl.java
final StructStat stat;
try {
/*
* Metadata operations don't usually count as a block guard
* violation, but we explicitly want this one.
*/
BlockGuard.getThreadPolicy().onReadFromDisk();
stat = Os.stat(mFile.getPath());
} catch (ErrnoException e) {
return true;
}
From your stack trace this is the key:
Caused by: java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.String
you are trying to use a java.util.HashSet where a java.lang.String is in use.
It's at SharedPreferencesImpl.java:225 when getString is being called. This is as far as I can go if you do not share the relevant part of your code.
EDIT
As the asker pointed out here in the comment section, the issue was that double quotation marks were used on string-array items.

Cannot load class from .aar file when i added Room persistence library into my library

I am currently developing an alarm library, that needs to store multiple alarm details in sqlite database.
So I used google's Room persistence library, with my alarm library.
I have added below code to build.gradle.
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
I have added database class
#Database(entities = AlarmDetails.class, version = 1)
public abstract class AlarmDatabase extends RoomDatabase {
public abstract AlarmDao alarmDao();
}
I have also added Data Access Object
#Dao
public interface AlarmDao {
#Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(AlarmDetails alarmDetails);
#Update
void update(AlarmDetails alarmDetails);
#Delete
void delete(AlarmDetails alarmDetails);
#Query("SELECT * FROM alarms")
List<AlarmDetails> getAll();
}
I am creating a database instance in the main class of my library as below
/*Constructor*/
public AlarmBase(Context mcontext,Context ncontext) {
this.mContext = mcontext;
alarmDatabase= Room.databaseBuilder(ncontext,
AlarmDatabase.class, "alarm_db")
.build();
}
i am calling the above constructor from my demo project and passing application context as below
Context applicationContext=MyApplicationClass.getContext();
alarmBase = new AlarmBase(this,applicationContext);
And this is my application class
public class MyApplicationClass extends MultiDexApplication {
private static MultiDexApplication instance;
#Override
public void onCreate() {
super.onCreate();
instance = this;
}
public static Context getContext() {
return instance.getApplicationContext();
}
}
But when I run the project I get the below error
--------- beginning of crash
02-21 20:23:34.643 11930-11930/com.mid.alarmdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mid.alarmdemo, PID: 11930
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/mid/alarmlib/roomsqlite/AlarmDatabase;
at com.mid.alarmlib.AlarmBase.<init>(AlarmBase.java:39)
at com.mid.alarmdemo.MainActivity.onCreate(MainActivity.java:54)
at android.app.Activity.performCreate(Activity.java:6362)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2441)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.access$1100(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5613)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mid.alarmlib.roomsqlite.AlarmDatabase" on path: DexPathList[[zip file "/data/app/com.mid.alarmdemo-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mid.alarmdemo-1/lib/arm64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at com.mid.alarmlib.AlarmBase.<init>(AlarmBase.java:39) 
at com.mid.alarmdemo.MainActivity.onCreate(MainActivity.java:54) 
at android.app.Activity.performCreate(Activity.java:6362) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2441) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2548) 
at android.app.ActivityThread.access$1100(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:157) 
at android.app.ActivityThread.main(ActivityThread.java:5613) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 
Suppressed: java.lang.NoClassDefFoundError: com.mid.alarmlib.roomsqlite.AlarmDatabase
at dalvik.system.DexFile.defineClassNative(Native Method)
at dalvik.system.DexFile.defineClass(DexFile.java:226)
at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
at dalvik.system.DexPathList.findClass(DexPathList.java:338)
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
... 16 more
Suppressed: java.lang.ClassNotFoundException: com.mid.alarmlib.roomsqlite.AlarmDatabase
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 15 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
I was able to run the project along with my library until I added Room persistence to my library
I just added below lines to the build.gradle(Module app) of the project also,
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
and it worked fine

My application in android keeps on crashing

I keep on getting this error in my logcat and don't know why. I don't seem to have any errors in code either. Any help will be greatly appreciated.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.trainyourmath, PID: 26931
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.trainyourmath/com.example.admin.trainyourmath.MainActivity}: java.lang.ClassCastException: com.example.admin.trainyourmath.MainActivity cannot be cast to android.view.View$OnClickListener
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5551)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.ClassCastException: com.example.admin.trainyourmath.MainActivity cannot be cast to android.view.View$OnClickListener
at com.example.admin.trainyourmath.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
at android.app.ActivityThread.access$900(ActivityThread.java:157) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5551) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
You are using your MainActivity as a OnClickListener which it does not implement properly. Add implements View.OnClickListener to the end of your MainActivity class declaration if it's not already there. Should look something like:
public class MainActivity extends AppCompatActivity implements View.OnClickListener
You also need to implement all the methods of the View.OnClickListener so inside your MainActivity class add:
#Override
public void onClick(View v) {
// Do stuff
}

Trying to perform task off the main thread. Is this source code doing that?

For a few days I've been trying to figure out this multithreading thing on Android Studio. I've only used SwingWorker in the past and after trying several ways to get my https connection to initiate off of the main thread I'm pulling my hair out. I've tried everything from using ASyncTask to just starting a thread everytime the connection gets made, but nothing seems to work. My app crashes every time. The most recent attempt is pictured below.
Edit: It's not a duplicate. I've read that thread and it doesn't do anything to help solve this problem. I'm attempting to execute this task outside of the main thread, but I don't know why my code isn't doing that already since I change to the background thread in run(); and even start a new thread just in case.
public class YelpActivity extends AppCompatActivity implements Runnable{
private yelpLoader load;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yelp);
}
public void run(){
Thread t = new Thread();
t.start();
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
YelpAPI test = new YelpAPI();
test.run();
}
public void yelpClick(View v){
run();
changeYelpResults(v,"test");
}
private void changeYelpResults(View v, String text){
TextView t = (TextView) findViewById(R.id.ResturauntID);
t.setText(text);
}
}
Error Log:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: tastr.tastr, PID: 3560
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: org.scribe.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service.
at org.scribe.model.Request.send(Request.java:70)
at org.scribe.model.Request.send(Request.java:76)
at tastr.tastr.YelpAPI.sendRequestAndGetResponse(YelpAPI.java:132)
at tastr.tastr.YelpAPI.searchForBusinessesByLocation(YelpAPI.java:89)
at tastr.tastr.YelpAPI.queryAPI(YelpAPI.java:146)
at tastr.tastr.YelpAPI.run(YelpAPI.java:190)
at tastr.tastr.YelpActivity.run(YelpActivity.java:26)
at tastr.tastr.YelpActivity.yelpClick(YelpActivity.java:31)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
at org.scribe.model.Response.<init>(Response.java:29)
at org.scribe.model.Request.doSend(Request.java:117)
at org.scribe.model.Request.send(Request.java:66)
at org.scribe.model.Request.send(Request.java:76) 
at tastr.tastr.YelpAPI.sendRequestAndGetResponse(YelpAPI.java:132) 
at tastr.tastr.YelpAPI.searchForBusinessesByLocation(YelpAPI.java:89) 
at tastr.tastr.YelpAPI.queryAPI(YelpAPI.java:146) 
at tastr.tastr.YelpAPI.run(YelpAPI.java:190) 
at tastr.tastr.YelpActivity.run(YelpActivity.java:26) 
at tastr.tastr.YelpActivity.yelpClick(YelpActivity.java:31) 
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
public class YelpActivity extends AppCompatActivity{
private yelpLoader load;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yelp);
}
public void run(){
new Thread(new Runnable() {
#Override
public void run() {
YelpAPI test = new YelpAPI();
test.run();
}
}).start();
}
public void yelpClick(View v){
run();
changeYelpResults(v,"test");
}
private void changeYelpResults(View v, String text){
TextView t = (TextView) findViewById(R.id.ResturauntID);
t.setText(text);
}
}
Note: It's not a good practice to implement Runnable in Activity
class. Use Core Java threads like I did in the run method or Use other
Android Background mechanism like AsyncTask/IntentService.

Categories

Resources