I followed stackoverflow answer and medium tutorial to make Splash Screen of Android version, but when I start the app, it shows Splash Screen a moment and stop the application.
My Splash Screen logo image directoy
launch_background.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/white" />
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/splash_logo" />
</item>
</layer-list>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="LaunchTheme" parent="#android:style/Theme.Black.NoTitleBar">
<!-- Show a splash screen on the activity. Automatically removed when
Flutter draws its first frame -->
<item name="android:windowBackground">#drawable/launch_background</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>
MainActivity.java
package com.example.privacyofanimal;
import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
public class MainActivity extends FlutterActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setStatusBarColor(0x00000000);
GeneratedPluginRegistrant.registerWith(this);
ViewTreeObserver vto = getFlutterView().getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
getFlutterView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
});
}
}
main.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
SystemChrome.setEnabledSystemUIOverlays([]);
runApp(PrivacyOfAnimal());
}
class PrivacyOfAnimal extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: '동물의 사생활',
home: Test(),
debugShowCheckedModeBanner: false,
);
}
}
class Test extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(),
);
}
}
My phone is Samsung Galaxy S8+ and I wanna fix this issue. What is the problem?
Error log
2019-01-23 16:44:03.443 18922-18922/com.handong.privacyofanimal
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.handong.privacyofanimal, PID: 18922
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.handong.privacyofanimal/com.handong.privacyofanimal.MainActivity}:
java.lang.ClassNotFoundException: Didn't find class
"com.handong.privacyofanimal.MainActivity" on path: DexPathList[[zip
file
"/data/app/com.handong.privacyofanimal-rCGlJOS-fSwTgzOMfceE0g==/base.apk"],nativeLibraryDirectories=[/data/app/com.handong.privacyofanimal-rCGlJOS-fSwTgzOMfceE0g==/lib/arm64,
/data/app/com.handong.privacyofanimal-rCGlJOS-fSwTgzOMfceE0g==/base.apk!/lib/arm64-v8a,
/system/lib64, /system/vendor/lib64]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2839)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.handong.privacyofanimal.MainActivity" on path: DexPathList[[zip
file
"/data/app/com.handong.privacyofanimal-rCGlJOS-fSwTgzOMfceE0g==/base.apk"],nativeLibraryDirectories=[/data/app/com.handong.privacyofanimal-rCGlJOS-fSwTgzOMfceE0g==/lib/arm64,
/data/app/com.handong.privacyofanimal-rCGlJOS-fSwTgzOMfceE0g==/base.apk!/lib/arm64-v8a,
/system/lib64, /system/vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.Instrumentation.newActivity(Instrumentation.java:1180)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2829)
Related
This question already has answers here:
getActionBar() returns null
(25 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
So I'm writing code for turning a website into it's web view app, it's working fine when Dark Mode is switched on & is crashing when it's off, however, I haven't written any piece of code keeping those modes in my mind.
I'm getting the following error:
2021-01-22 11:51:40.868 7271-7271/com.shopitapp.shopit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.shopitapp.shopit, PID: 7271
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shopitapp.shopit/com.shopitapp.shopit.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.hide()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.hide()' on a null object reference
at com.shopitapp.shopit.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-01-22 11:51:40.926 7271-7271/com.shopitapp.shopit I/Process: Sending signal. PID: 7271 SIG: 9
MainActivity.java
package com.shopitapp.shopit;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView web;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().hide();
web = findViewById(R.id.webView);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
web.setWebViewClient(new Callback());
web.loadUrl("https://shopit.app/");
}
private class Callback extends WebViewClient {
#Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
return false;
}
}
/*#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN)
{
switch(keyCode) {
case KeyEvent.KEYCODE_BACK:
if(web.canGoBack())
web.goBack();
else
finish();
}
}
return super.onKeyDown(keyCode, event);
}*/
#Override
public void onBackPressed() {
if(web.canGoBack())
web.goBack();
else
super.onBackPressed();
}
}
SplashScreen.java
package com.shopitapp.shopit;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*Bitmap bitmapImage = BitmapFactory.decodeFile("src\main\res\drawable\logo.png");
int nh = (int) ( bitmapImage.getHeight() * (512.0 / bitmapImage.getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(bitmapImage, 512, nh, true);
ImageView your_imageview = null;
your_imageview.setImageBitmap(scaled);*/
startActivity(new Intent(this,MainActivity.class));
finish();
}
}
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shopitapp.shopit">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:networkSecurityConfig="#xml/network_security_config"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.Shopit">
<activity android:name="com.shopitapp.shopit.SplashScreen"
android:theme="#style/SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.shopitapp.shopit.MainActivity"
android:theme="#style/Theme.Shopit"></activity>
</application>
</manifest>
themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Shopit" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/purple_500</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<style name="SplashScreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/background_splashscreen</item>
</style>
</resources>
themes.xml(night)
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Shopit" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/purple_200</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_200</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
On your MainActivity you are trying to hide the Action Bar by using the code getActionBar().hide,
But the problem is your MainActivity doesn't have an Action Bar because your Main Activity is using the Theme called Theme.Shopit which removes the Action Bar by default because its parent is Theme.MaterialComponents.DayNight.NoActionBar.
So to fix this issue you can simply remove the code getActionBar().hide from your MainActivity.
I am working on an app in Android Studio, and after adding buttons to MainActivity to move to other activities, despite the fact that the layout shows up properly in the editor, I get the error which will be posted at the end. There are other activities in my project which I did not include, because the error refers to activity_main.xml, but I will include them if they are necessary to solve the problem.
Here is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/view_img_main_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/grocery_img_1" />
<Button
android:id="#+id/btn_inventory"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_below="#id/view_img_main_menu"
android:text="#string/btn_my_groceries_text"
android:theme="#style/BtnTheme" />
<Button
android:id="#+id/btn_shopping_list"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_below="#id/btn_inventory"
android:text="#string/btn_shopping_list_text"
android:theme="#style/BtnTheme" />
</RelativeLayout>
Here is MainActivity:
public class MainActivity extends AppCompatActivity {
private Button btnInventory;
private Button btnShopList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnInventory = findViewById(R.id.btn_inventory);
btnShopList = findViewById(R.id.btn_shopping_list);
btnInventory.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent inventoryIntent = new Intent(MainActivity.this, Inventory.class);
startActivity(inventoryIntent);
}
});
btnShopList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent shopListIntent = new Intent(MainActivity.this, ShoppingList.class);
startActivity(shopListIntent);
}
});
}
Here is the error I get awhen I try to run the code:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.grocerystatusapplication/com.example.grocerystatusapplication.MainActivity}: android.view.InflateException: Binary XML file line #15 in com.example.grocerystatusapplication:layout/activity_main: Binary XML file line #15 in com.example.grocerystatusapplication:layout/activity_main: Error inflating class Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3344)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3488)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
at android.os.Handler.dispatchMessage(Han
E/AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7506)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:956)
Here is styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/colorLayoutBackground</item>
</style>
<style name="BtnTheme" parent="TextAppearance.AppCompat.Widget.Button.Colored">
<item name="colorButtonNormal">#color/colorViewBackground</item>
</style>
<style name="TextViewTheme" parent="">
<item name="android:background">#drawable/rounded_corners</item>
<item name="android:textColor">#android:color/white</item>
</style>
I have tried changing the attributes of the Button that the error refers to (line 15, btn_inventory), but I did not get anywhere. Did not come across this error before and don't really understand what is the problem. What can I change to make it work? After looking through posts about the same error, I did not get an answer to my question, there seemed to be a quite a few reasons this error came up.
In both Button tags change
android:theme="#style/BtnTheme"
to
style="#style/BtnTheme"
I am currently writing a flutter project in dart and build the android app from flutter. I currently have an image that covers the entire screen as the splash screen. Now I want to provide other images such that the app shows a random image each time the user opens the app.
I have searched "how to randomize splash screen for android" but it all needs to modify some java code (but I am writing a dart project). Is there another way like only modify .dart and/or .xml code?
I have added the second image to different places but none works. See below:
In launch_background.xml, the following only shows the splash_screen_1 image, splash_screen_2 is never shown.
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/white" />
<!-- You can insert your own image assets here -->
<item>
<bitmap
android:gravity="fill_horizontal|fill_vertical"
android:src="#drawable/splash_screen_1" />
<bitmap
android:gravity="fill_horizontal|fill_vertical"
android:src="#drawable/splash_screen_2" />
</item>
</layer-list>
And the following only shows the splash_screen_2 image, splash_screen_1 is never shown.
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/white" />
<!-- You can insert your own image assets here -->
<item>
<bitmap
android:gravity="fill_horizontal|fill_vertical"
android:src="#drawable/splash_screen_1" />
</item>
<item>
<bitmap
android:gravity="fill_horizontal|fill_vertical"
android:src="#drawable/splash_screen_2" />
</item>
</layer-list>
Is there a way to solve it in flutter? e.g. only modify .xml and/or .dart code and no need to modify java code? Thank you!
Try this:
import 'package:flutter/material.dart';
import 'dart:math';
void main() => runApp(MyApp());
Widget randomScreen(int max) {
var rnd = Random();
var num = rnd.nextInt(max);
print(num);
switch (num) {
case 1: return SecondSplashScreen();break;
case 2: return ThirdSplashScreen();break;
default: return FirstSplashScreen();break;
}
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: randomScreen(4),
);
}
}
class FirstSplashScreen extends StatefulWidget {
FirstSplashScreen({Key key}) : super(key: key);
_FirstSplashScreenState createState() => _FirstSplashScreenState();
}
class _FirstSplashScreenState extends State<FirstSplashScreen> {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.blue
);
}
}
class SecondSplashScreen extends StatefulWidget {
SecondSplashScreen({Key key}) : super(key: key);
_SecondSplashScreenState createState() => _SecondSplashScreenState();
}
class _SecondSplashScreenState extends State<SecondSplashScreen> {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.red
);
}
}
class ThirdSplashScreen extends StatefulWidget {
ThirdSplashScreen({Key key}) : super(key: key);
_ThirdSplashScreenState createState() => _ThirdSplashScreenState();
}
class _ThirdSplashScreenState extends State<ThirdSplashScreen> {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.green
);
}
}
I tried to create a game show the pictures and list of character name it. But when I run the app, it keeps crashing and show the error:
Error inflating class android.support.design.widget.AppBarLayout.
Here is the log:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.celebguess, PID: 3166
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.celebguess/com.example.user.celebguess.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.widget.AppBarLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.widget.AppBarLayout
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.design.widget.AppBarLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.AppBarLayout" on path: DexPathList[[zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/base.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:606)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.user.celebguess.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Here is the activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.user.celebguess.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
/>
Also here is the styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
Also here is the main_activity.java for the Java part:
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private GridView gridView;
private SharedPreferences preferences;
private ArrayAdapter<String> names;
private Toast currentToast;
private ImageView img;
private int numberOfChoice;
private ArrayList<String> nameList;
private ArrayList<Integer> photos;
private ArrayList<String> nameForChoose;
private Map<String,Integer> nameMap;
private int currentPicNumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
protected void onStart() {
super.onStart();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
int orientation = getResources().getConfiguration().orientation;
if(orientation== Configuration.ORIENTATION_PORTRAIT) {
getMenuInflater().inflate(R.menu.menu_main, menu);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent intent = new Intent(this,Settings.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
For latest version of android, in build.gradle(app)
implementation 'com.android.support:design:30.0.0'
and for usage, in your .xml:
<com.google.android.material.appbar.AppBarLayout
Add below to bulid.gradle:app module :
implementation 'com.android.support:design:28.0.0'
## design library here you can copy and add to your build.gradle file
implementation 'com.android.support:design:28.0.0'
I am using NoNonsense-FilePicker for filtering the sd card for pdf files by extending the FilePickerActivity and creating a FilteredFilePickerFragment instance through it but i get an error related to an Image button in it's resources.
this is the File Picker Fragment Extender:
public class FilePickerFragmentExtender extends FilePickerActivity {
public FilePickerFragmentExtender() {
super();
}
#Override
protected AbstractFilePickerFragment<File> getFragment(#Nullable String startPath, int mode, boolean allowMultiple, boolean allowCreateDir, boolean allowExistingFile, boolean singleClick) {
AbstractFilePickerFragment<File> fragment = new FilteredFilePickerFragment();
// startPath is allowed to be null. In that case, default folder should be SD-card and not "/"
fragment.setArgs(startPath != null ? startPath : Environment.getExternalStorageDirectory().getPath(),
mode, allowMultiple, allowCreateDir, allowExistingFile, singleClick);
return fragment;
}
}
and this is the Filtered File Picker Fragment:
public class FilteredFilePickerFragment extends FilePickerFragment {
private static final String TAG = "FilteredFilePickerFragm";
// File extension to filter on
private static final String EXTENSION = ".pdf";
public FilteredFilePickerFragment() {
super();
}
/**
* #param file
* #return The file extension. If file has no extension, it returns null.
*/
private String getExtension(#NonNull File file) {
String path = file.getPath();
int i = path.lastIndexOf(".");
if (i < 0) {
return null;
} else {
return path.substring(i);
}
}
#Override
protected boolean isItemVisible(final File file) {
boolean ret = super.isItemVisible(file);
if (ret && !isDir(file) && (mode == MODE_FILE || mode == MODE_FILE_AND_DIR)) {
String ext = getExtension(file);
return ext != null && EXTENSION.equalsIgnoreCase(ext);
}
return ret;
}
}
the error:
FATAL EXCEPTION: main
Process: com.pdfviewer.debug, PID: 15693
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pdfviewer.debug/com.pdfviewer.FilePickerFragmentExtender}: android.view.InflateException: Binary XML file line #90: Binary XML file line #90: Error inflating class ImageButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2684)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6186)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: android.view.InflateException: Binary XML file line #90: Binary XML file line #90: Error inflating class ImageButton
Caused by: android.view.InflateException: Binary XML file line #90: Error inflating class ImageButton
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x7f010031 a=-1}
at android.content.res.TypedArray.getColorStateList(TypedArray.java:528)
at android.widget.ImageView.<init>(ImageView.java:180)
at android.widget.ImageButton.<init>(ImageButton.java:84)
at android.widget.ImageButton.<init>(ImageButton.java:80)
at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:60)
at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:118)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1026)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1083)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:192)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.nononsenseapps.filepicker.AbstractFilePickerFragment.inflateRootView(AbstractFilePickerFragment.java:239)
at com.nononsenseapps.filepicker.AbstractFilePickerFragment.onCreateView(AbstractFilePickerFragment.java:165)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2239)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2063)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
com.pdfviewer.debug E/AndroidRuntime: at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:554)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1249)
at android.app.Activity.performStart(Activity.java:6701)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6186)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
I searched and found that it's related to the theme of the file-picker but also i didn't find anything wrong in the theme:
<style name="FilePickerTheme" parent="NNF_BaseTheme">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Setting a divider is entirely optional -->
<item name="nnf_list_item_divider">?android:attr/listDivider</item>
<!-- Need to set this also to style create folder dialog -->
<item name="alertDialogTheme">#style/FilePickerAlertDialogTheme</item>
</style>
<style name="FilePickerAlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
colorss.xml:
<color name="colorPrimary">#3f51b5</color>
<color name="colorPrimaryDark">#303f9f</color>
<color name="colorAccent">#5495fd</color>
The problem was actually related to the theme I am applying to the parent activity of this fragment android:theme="#style/AppTheme" was causing the app to crash because the layout wasn't finding it's appropriate attributes and debugging xml is kinda tricky.
The ImageButton in the resources:
<ImageButton
android:id="#+id/nnf_button_ok_newfile"
// this style line is the cause
style="?attr/borderlessButtonStyle"
android:layout_width="48dp"
android:layout_height="48dp"
android:hint="#android:string/ok"
app:srcCompat="#drawable/nnf_ic_save_black_24dp"
android:tint="?attr/nnf_save_icon_color"/>
and the solution to this problem was to either change your Activity theme in the manifest to android:theme="#style/FilePickerTheme" which is bad for user experience to user different themes between activities, or the other solution which is to Override the onCreate method and set the theme before the creation of the activity happens as follows:
FilteredFilePickerFragment
#Override
public void onCreate(Bundle savedInstanceState) {
getActivity().setTheme(R.style.FilePickerTheme);
super.onCreate(savedInstanceState);
}