App crashes when trying to use setImageResource - java

I was building another larger app and at some point I needed to dynamically change images and my app crashed. So, to test functionality I made another really simple app that just displays a picture with code:
package com.example.imagetest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setImageResource(R.drawable.pugs);
}
}
The error reported by Android Studio is: 03-07 11:30:01.146
20758-20758/? E/Trace: error opening trace file: No such file or
directory (2) 03-07 11:30:01.284 20758-20758/com.example.imagetest
E/dalvikvm: Could not find class
'android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper',
referenced from method
android.support.v4.view.ViewCompat.addOnUnhandledKeyEventListener
03-07 11:30:01.286 20758-20758/com.example.imagetest E/dalvikvm: Could
not find class 'android.view.WindowInsets', referenced from method
android.support.v4.view.ViewCompat.dispatchApplyWindowInsets 03-07
11:30:01.298 20758-20758/com.example.imagetest E/dalvikvm: Could not
find class 'android.view.WindowInsets', referenced from method
android.support.v4.view.ViewCompat.onApplyWindowInsets 03-07
11:30:01.300 20758-20758/com.example.imagetest E/dalvikvm: Could not
find class 'android.view.View$OnUnhandledKeyEventListener', referenced
from method
android.support.v4.view.ViewCompat.removeOnUnhandledKeyEventListener
03-07 11:30:01.306 20758-20758/com.example.imagetest E/dalvikvm: Could
not find class 'android.support.v4.view.ViewCompat$1', referenced from
method
android.support.v4.view.ViewCompat.setOnApplyWindowInsetsListener
03-07 11:30:01.354 20758-20758/com.example.imagetest E/dalvikvm: Could
not find class 'android.graphics.drawable.RippleDrawable', referenced
from method
android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
03-07 11:30:01.396 20758-20758/com.example.imagetest E/AndroidRuntime:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imagetest/com.example.imagetest.MainActivity}:
android.content.res.Resources$NotFoundException: Resource ID
0x7f060063
and a lot of other stuff. Here's the weird thing: I changed the picture to another folder "mipmap" (before it was in drawable: res/drawable) and the app doesn't crash! But it doesn't show the image either, just nothing on screen. the 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"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Not sure if the image is important, but it was a really cute picture of 3 pugs(Resolution approx 4k) and I added it to drawable just with copy/paste.

Your problem is the wrong placement of resources(images). You are using a device with API below 24 , so resource for API 23 and below is null, because they only API 24 and up.
please put your pugs picture in drawable folder not drawable-v24 , and also don't use a huge image!

imageView.setImageResource(R.drawable.pugs);
You need specify which R it is in the import part.
example:
import com.sample.me.mydraft.R

Related

Android Studio project succesfully launching but nothing pops up - Android Java

I'm running:
package com.example.projectTest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class WelcomePage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_page);
}
}
with:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".WelcomePage">
</androidx.constraintlayout.widget.ConstraintLayout>
but I'm getting the following error in the logcat when I run:
Could not get package user id: run-as: unknown package: com.example.projectTest
Could not find apks for this package: com.example.projectTest
Could not get package user id: run-as: unknown package: com.example.projectTest
Could not find apks for this package: com.example.projectTest
Failed to measure fs-verity, errno 1: /data/app/~~kWSycfQmCvKbfYx2KzxrNA==/com.example.projectTest-x4Zc9BUq0kRGdN5aX5AxXQ==/base.apk
The launch succeeds, but no app shows up on the emulator (Pixel 6 API 32). What could be the problem? Thank you.
I went to Run > Edit Configurations and set launch to a Specified Activity. I then set the specified activity and in AndroidMainfest.xml, inside the activity tag, I set exported=true. Now it works.

Caused by android.content.res.Resources$NotFoundException: Drawable (missing name) with resource ID even the resource is there

On some Android devices, I am getting "Resources$NotFoundException: Drawable (missing name) with resource ID" error, e.g.:
On LGE Android 8.1 devices it is complaining
Unable to start activity ComponentInfo{SplashActivity}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class ImageView
For the following part. In particular, it is complaining "android:src="#drawable/red_logo""
<ImageView
android:id="#+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="#drawable/red_logo" />
I checked the source code directory, "red_logo.png" already exists in the following folders:
drawable
drawable-hdpi
drawable-ldpi
drawable-mdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi
Here is the Java code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash); <---- Fails here
}
The dimension of red_logo.png in drawable folder is 4864 × 2692, same size as the red_logo.png in drawable-xxxhdpi folder. Is it too big?
I did a lot of research but it seems I cannot find a valid answer.
Thank you.
Best regards.
It happens due to scale issues, hence, always use a vector asset instead of an image in such scenarios. Worked for me.

Vector images imported from SVG images not being recognised in ImageView

The SVG that I was attempting to import was not correctly formatted, this lead to the xml for the vecto image not being correctly formatted either Try the following samples here as these are all correctly formatted SVGs.
I am attempting to add a SVG image into a ImageView but i keep on getting an error every time I try to call its #drawable/ value. The file seems to be in the right location since when I use a PNg image it works, but it does not get recognised when its an SVG image.
The XML to add the image is :
<ImageView
android:id="#+id/statusImage"
android:layout_width="304dp"
android:layout_height="391dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/statusText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_temp" />
and the location of the image file is:
I have tried to modify the Gradle file so that it contains:
vectorDrawables.useSupportLibrary = true
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
But that does not seem to solve the issue I'm having.
The error log seems to print that :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: sc17dpc.individualproject, PID: 3467
android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class ImageView
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class ImageView
Caused by: android.content.res.Resources$NotFoundException: Drawable sc17dpc.individualproject:drawable/ic_temp with resource ID #0x7f07006d
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_temp.xml from drawable resource ID #0x7f07006d
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:1166)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:917)
at android.content.res.Resources.getDrawableForDensity(Resources.java:1074)
at android.content.res.Resources.getDrawable(Resources.java:1013)
at android.content.Context.getDrawable(Context.java:630)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:463)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:203)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:59)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:78)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:783)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:874)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at sc17dpc.individualproject.HomeFragment.onCreateView(HomeFragment.java:23)
and points to my class HomeFragment on the line:
View view = inflater.inflate(R.layout.fragment_home, container, false);
Any help is appreciated since the solutions that I've followed currently have not solved the problem.
*********EDIT*********
The SVG file is imported using the "New -> Vector Asset" built in feature in Android Studio and by using a local file.
The image file looks like:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="640dp"
android:height="640dp"
android:viewportWidth="640"
android:viewportHeight="640">
<path
followed by a load of information about the image path.
I have attempted to change app:srcCompat to android:src and this still gives the same error as before.
Can you try again?
android:src="#drawable/ic_temp"
You can't use viewportHeight and viewportWidth as 640
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="640dp"
android:height="640dp">
Source VectorDrawable
Issue has been sumarised at the top of the article. Thanks for the help.

Error inflating class ImageView in android

whenever i hit the imageview i get this error, this happens to me when i switch to test my app on different screen sizes, here is my xml
<ImageView
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="28dp"
android:layout_marginRight="28dp"
android:layout_marginTop="20dp"
app:srcCompat="#drawable/search"/>
and here is the error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ahmed.electionadmin, PID: 30789
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ahmed.electionadmin/com.example.ahmed.electionadmin.Search}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
I have tried so many solutions that asks me to replace src with srccompat and so on, but still is working, ant help would be appreciated
To use srcCompat you need to add vectorDrawables.useSupportLibrary = true to your build.gradle file:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
Or you can use src instead of srcCompat to resolve the error.
android:src="#drawable/search"
Replace this:
app:srcCompat="#drawable/search"
With:
android:src="#drawable/search"
Pay attention, not only src with srcCompat, also app with android.
Let me know if it works.
Explanation: You're not using a support library ImageView, but a regular one. app namespace is usually used for support library, same as srcCompat. So you needed to change those 2 things, not just one. That's why merely changing the srcCompat with src yielded no results.
i fixed it by uploading my image inside draawable no 24
you are doing like fitting image to different resolutions , you should have image with different resolutions like hdpi xhdpi , you you can use vector drawable image

java.lang.IllegalStateException: Could not find a method after Parse implementation

I've read everywhere - couldn't find a solution.
My project was working fine, I stated the methods I wanted to be called to the onClick inside the XML file like this:
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:theme="#style/AppTheme"
android:id="#+id/actualMainActivity"
tools:context=".ActualMainActivity"
><Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add new noob"
android:id="#+id/addNewNoob"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
android:onClick="addNewNoob"
android:layout_alignTop="#+id/listOfNoobs"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /></RelativeLayout>
the method inside my java class (ActualMainActivity) was like this:
public void addNewNoob(View view) {
Log.i("Tag", "you clicked the add button");
}
Only after I've implemented the Parse SDK to work with, my Gradle resynced and now when I run the application I get the 'Method not found exception'.
I would like to point out that when I assign an onClickListener directly to the button like this:
final Button a = (Button) findViewById(R.id.addNewNoob);
a.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Log.i("Tag", "you clicked the add button");
}
});
that works perfectly fine...
What have I changed when I installed the SDK ?
How can I get back to the way that I write the methods on XML and they are executed accordingly.
Really confused... thanks in advance
here's the logcat:
02-09 11:29:34.917 2669-2669/com.example.shaked.sqliteexample02 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.shaked.sqliteexample02, PID: 2669
java.lang.IllegalStateException: Could not find a method listOfNoobs(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'listOfNoobs'
at android.view.View$1.onClick(View.java:3994)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NoSuchMethodException: listOfNoobs [class android.view.View]
at java.lang.Class.getMethod(Class.java:664)
at java.lang.Class.getMethod(Class.java:643)
at android.view.View$1.onClick(View.java:3987)
         
Thanks in advance
Create a New Project move the old Classes to the New one and Check!
its giving methodexception error
so see to it that the method named listofnoobs() is not missing.
also clean your project and then rebuild it

Categories