Attempt to invoke virtual method on a null object reference (service) - java

I keep getting the same error, Unable to start activity ... java.lang.NullPointerException: Attempt to invoke virtual method ... on a null object reference, every time I try to invoke a function in a service from an activity.
The error is below.
10-18 22:30:46.371 1773-1773/com.example.test I/art: Not late-enabling -Xcheck:jni (already on)
10-18 22:30:47.054 1773-1785/com.example.test I/art: Background sticky concurrent mark sweep GC freed 2727(238KB) AllocSpace objects, 0(0B) LOS objects, 17% free, 939KB/1135KB, paused 19.920ms total 60.374ms
10-18 22:30:47.070 1773-1773/com.example.test D/AndroidRuntime: Shutting down VM
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: FATAL EXCEPTION: main
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: Process: com.example.test, PID: 1773
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.test.MyService.getRandomNumber()' on a null object reference
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:144)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5221)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.test.MyService.getRandomNumber()' on a null object reference
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at com.example.test.MainActivity.onCreate(MainActivity.java:27)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5937)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:144) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5221) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
10-18 22:30:47.071 1773-1773/com.example.test E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
10-18 22:30:58.000 1773-1780/com.example.test W/art: Suspending all threads took: 12.169ms
10-18 22:31:27.545 1773-1780/com.example.test W/art: Suspending all threads took: 17.580ms
10-18 22:31:31.555 1773-1780/com.example.test W/art: Suspending all threads took: 18.440ms
10-18 22:31:39.070 1773-1780/com.example.test W/art: Suspending all threads took: 11.531ms
10-18 22:31:51.100 1773-1780/com.example.test W/art: Suspending all threads took: 5.540ms
The MainActivity is below.
package com.example.test;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
MyService mService;
boolean mBound = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Bind this and the service together
Intent noiseIntent = new Intent(this, MyService.class);
bindService(noiseIntent, mConnection, Context.BIND_AUTO_CREATE);
startService(noiseIntent);
int num = mService.getRandomNumber();
Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className, IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
MyService.ServiceBinder binder = (MyService.ServiceBinder) service;
mService = binder.getService();
mBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
}
And the MyService is below.
package com.example.test;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import java.util.Random;
public class MyService extends Service {
// Binder given to clients
private final IBinder mBinder = new ServiceBinder();
// Random number generator
private final Random mGenerator = new Random();
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class ServiceBinder extends Binder {
MyService getService() {
// Return this instance of LocalService so clients can call public methods
return MyService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
/**
* method for clients
*/
public int getRandomNumber() {
return mGenerator.nextInt(100);
}
}
I've been stuck on this for a couple of hours and I couldn't find any solutions. Any help would be appreciated. :D

Both bindService() and startService() are asynchronous. Your mService object will be null until onServiceConnected() is called, so you cannot use it until that point.

Related

Passing Integer Values Between Activities - Android

Other stuck posts unfortunately couldn't help me.
When I clicked button while easy radiobutton is checked, the app stops working. I couldn't go and see another activity.
Sender Side:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(radiobutton_arm_triceps_easy.isChecked()) {
String dene = "my example test";
int myValue=2;
Intent intent = new Intent(getApplicationContext(), exercise_arm_triceps_execute.class);
intent.putExtra("attempt1", myValue );
startActivity(intent);
}
}
});
Receiver Side:
int receiveValue=getIntent().getIntExtra("attempt1",0);
textshow.setText(receiveValue);
LOGCAT
04-26 16:52:06.320 31527-31527/com.example.kerem.tutorial_project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kerem.tutorial_project, PID: 31527
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kerem.tutorial_project/com.example.kerem.tutorial_project.exercise_arm_triceps_execute}: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:244)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3888)
at com.example.kerem.tutorial_project.exercise_arm_triceps_execute.onCreate(exercise_arm_triceps_execute.java:28)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Use
textshow.setText(String.valueOf(receiveValue));

Cannot locate all audio files to store in list view

so I am having trouble locating where in my code I have gone wrong. I am building an audio player. The function cannot find all audio files in my device except 2 of them. I am thinking the problem may lie somewhere in my song locating algorithm, findSongs.
Here is my log cat:
03-17 15:29:24.492 20066-20066/? I/SELinux﹕ Function: selinux_android_load_priority , priority [3] , priority version is VE=SEPF_SGH-M919_4.4.4_0048
03-17 15:29:24.492 20066-20066/? E/dalvikvm﹕ >>>>> Normal User
03-17 15:29:24.492 20066-20066/? E/dalvikvm﹕ >>>>> com.example.joe.audiodomain [ userId:0 | appId:10237 ]
03-17 15:29:24.492 20066-20066/? D/dalvikvm﹕ Late-enabling CheckJNI
03-17 15:29:24.632 20066-20066/? W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
03-17 15:29:24.632 20066-20066/? I/dalvikvm﹕ Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
03-17 15:29:24.632 20066-20066/? W/dalvikvm﹕ VFY: unable to resolve interface method 15027: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
03-17 15:29:24.632 20066-20066/? D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
03-17 15:29:24.632 20066-20066/? I/dalvikvm﹕ Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
03-17 15:29:24.632 20066-20066/? W/dalvikvm﹕ VFY: unable to resolve interface method 15031: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
03-17 15:29:24.632 20066-20066/? D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
03-17 15:29:24.702 20066-20066/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
03-17 15:29:24.702 20066-20066/? W/dalvikvm﹕ VFY: unable to resolve virtual method 396: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-17 15:29:24.702 20066-20066/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-17 15:29:24.702 20066-20066/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
03-17 15:29:24.702 20066-20066/? W/dalvikvm﹕ VFY: unable to resolve virtual method 418: Landroid/content/res/TypedArray;.getType (I)I
03-17 15:29:24.702 20066-20066/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-17 15:29:24.712 20066-20066/? E/VdcInflateDelegate﹕ Exception while inflating <vector>
org.xmlpull.v1.XmlPullParserException: Binary XML file line #17<vector> tag requires viewportWidth > 0
at android.support.graphics.drawable.VectorDrawableCompat.updateStateFromTypedArray(VectorDrawableCompat.java:535)
at android.support.graphics.drawable.VectorDrawableCompat.inflate(VectorDrawableCompat.java:472)
at android.support.graphics.drawable.VectorDrawableCompat.createFromXmlInner(VectorDrawableCompat.java:436)
at android.support.v7.widget.AppCompatDrawableManager$VdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:829)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:303)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:178)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:173)
at android.support.v7.widget.TintTypedArray.getDrawable(TintTypedArray.java:60)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:254)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:196)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:600)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:379)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:300)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:264)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
at com.example.joe.audiodomain.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5585)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.access$900(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
03-17 15:29:24.712 20066-20066/? D/AndroidRuntime﹕ Shutting down VM
03-17 15:29:24.712 20066-20066/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41845da0)
03-17 15:29:24.712 20066-20066/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.joe.audiodomain, PID: 20066
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.joe.audiodomain/com.example.joe.audiodomain.MainActivity}: android.view.InflateException: Binary XML file line #36: Error inflating class android.support.v7.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.access$900(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class android.support.v7.widget.Toolbar
at android.view.LayoutInflater.createView(LayoutInflater.java:626)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:379)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:300)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:264)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
at com.example.joe.audiodomain.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5585)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
            at android.app.ActivityThread.access$900(ActivityThread.java:170)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5635)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:600)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:379)
            at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:300)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:264)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
            at com.example.joe.audiodomain.MainActivity.onCreate(MainActivity.java:25)
            at android.app.Activity.performCreate(Activity.java:5585)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
            at android.app.ActivityThread.access$900(ActivityThread.java:170)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5635)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable-v19/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020016
at android.content.res.Resources.loadDrawable(Resources.java:3439)
at android.content.res.Resources.getDrawable(Resources.java:1893)
at android.support.v7.widget.TintResources.superGetDrawable(TintResources.java:48)
at android.support.v7.widget.AppCompatDrawableManager.onDrawableLoadedFromResources(AppCompatDrawableManager.java:374)
at android.support.v7.widget.TintResources.getDrawable(TintResources.java:44)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:180)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:173)
at android.support.v7.widget.TintTypedArray.getDrawable(TintTypedArray.java:60)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:254)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:196)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:600)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:379)
            at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:300)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:264)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
            at com.example.joe.audiodomain.MainActivity.onCreate(MainActivity.java:25)
            at android.app.Activity.performCreate(Activity.java:5585)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
            at android.app.ActivityThread.access$900(ActivityThread.java:170)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5635)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid drawable tag vector
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:986)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:930)
at android.content.res.Resources.loadDrawable(Resources.java:3435)
            at android.content.res.Resources.getDrawable(Resources.java:1893)
            at android.support.v7.widget.TintResources.superGetDrawable(TintResources.java:48)
            at android.support.v7.widget.AppCompatDrawableManager.onDrawableLoadedFromResources(AppCompatDrawableManager.java:374)
            at android.support.v7.widget.TintResources.getDrawable(TintResources.java:44)
            at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:180)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:173)
            at android.support.v7.widget.TintTypedArray.getDrawable(TintTypedArray.java:60)
            at android.support.v7.widget.Toolbar.<init>(Toolbar.java:254)
            at android.support.v7.widget.Toolbar.<init>(Toolbar.java:196)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:600)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:379)
            at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:300)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:264)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
            at com.example.joe.audiodomain.MainActivity.onCreate(MainActivity.java:25)
            at android.app.Activity.performCreate(Activity.java:5585)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
            at android.app.ActivityThread.access$900(ActivityThread.java:170)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5635)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
03-17 15:29:31.839 20066-20066/? I/Process﹕ Sending signal. PID: 20066 SIG: 9
Here is my code:
`
package com.example.joe.mp3demo;
import android.app.Activity;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ListActivity extends Activity {
ListView lv;
ArrayAdapter<String> adapter;
String songs[];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
//Creating a List view instance
lv= (ListView) findViewById(R.id.listView);
//Array list to hold all songs, using findSongs method passing the SD card directory
ArrayList<File> allSongs= findSongs(Environment.getExternalStorageDirectory());
songs= new String[allSongs.size()];
for(int c=0; c< allSongs.size(); c++){
songs[c]=allSongs.get(c).getName().toString();
}
adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songs);
lv.setAdapter(adapter);
}
//function to find all songs in SD card. Storing them in a temp ArrayList and returning
public ArrayList<File> findSongs(File root){
ArrayList<File> al= new ArrayList<File>();
File[] files=root.listFiles();
for(File singleFile: files){
if(singleFile.isDirectory() && !singleFile.isHidden()) {
al.addAll(findSongs(singleFile));
}
else{
if(singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav") || singleFile.getName().endsWith(".wma") ){
al.add(singleFile);
}
}
}
return al;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_list, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Try this code:-
public class FindMp3Files extends AppCompatActivity {
ListView mp3List;
String title, artist, path, displayName, songDuration;
ArrayList<Mp3Model> songs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_mp3_files);
mp3List = (ListView) findViewById(R.id.mp3List);
songs = new ArrayList<>();
scanSdcard();
SongAdapter adapter = new SongAdapter(this, songs);
mp3List.setAdapter(adapter);
}
private void scanSdcard() {
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION
};
final String sortOrder = MediaStore.Audio.AudioColumns.TITLE + " COLLATE LOCALIZED ASC";
Cursor cursor = null;
try {
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
cursor = this.getContentResolver().query(uri, projection, selection, null, sortOrder);
if (cursor != null) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Mp3Model mp3Model = new Mp3Model();
title = cursor.getString(0);
artist = cursor.getString(1);
path = cursor.getString(2);
displayName = cursor.getString(3);
songDuration = cursor.getString(4);
mp3Model.setTitle(title);
mp3Model.setArtist(artist);
mp3Model.setPath(path);
mp3Model.setDisplayName(displayName);
mp3Model.setSongDuration(songDuration);
songs.add(mp3Model);
cursor.moveToNext();
}
}
} catch (Exception e) {
Log.e("Tag", e.toString());
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
Here is the Mp3Model class :-
public class Mp3Model {
String title, artist, path, displayName, songDuration;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getSongDuration() {
return songDuration;
}
public void setSongDuration(String songDuration) {
this.songDuration = songDuration;
}
}
SongAdapter.java
public class SongAdapter extends BaseAdapter {
Context context;
ArrayList<Mp3Model> mp3Models;
LayoutInflater inflater;
Typeface captureFont;
public SongAdapter(Context context, ArrayList<Mp3Model> mp3Models) {
this.context = context;
this.mp3Models = mp3Models;
inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
captureFont = Typeface.createFromAsset(this.context.getAssets(), "fonts/Montserrat.ttf");
}
#Override
public int getCount() {
return mp3Models.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
TextView songName;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if (convertView == null) {
vh = new ViewHolder();
convertView = inflater.inflate(R.layout.song_row, parent, false);
vh.songName = (TextView) convertView.findViewById(R.id.songName);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.songName.setText(mp3Models.get(position).getTitle());
vh.songName.setTypeface(captureFont);
vh.songName.setPadding(10, 10, 10, 10);
vh.songName.setTextColor(Color.WHITE);
vh.songName.setBackgroundColor(Color.parseColor("#333333"));
vh.songName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
return convertView;
}
}

Calculator App : The app shuts down after I click on a (sign)button

I cant figure out where I went wrong with my code. Every time I press a button my apps shuts down unless I add a try and catch. As soon as I enter the two numbers and add the sign, the results should print out, but instead the app is just shutting down. Here is my LogCat and code. BTW I'm new to java & android studio, so excuse my mistakes.
public class MainActivity extends AppCompatActivity {
Button btnAdd, btnSubtract,btnMultiply, btnDivide;
TextView textResults, textSigns;
EditText firstnumber, secondnumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstnumber = (EditText) findViewById(R.id.firstnumber);
secondnumber =(EditText) findViewById(R.id.secondnumber);
textResults = (TextView) findViewById(R.id.results);
textSigns = (TextView) findViewById(R.id.invisible_signs);
btnAdd = (Button) findViewById(R.id.Add);
btnDivide = (Button) findViewById(R.id.divide);
btnMultiply =(Button) findViewById(R.id.Multiply);
btnSubtract =(Button) findViewById(R.id.Subtract);
final String number1 = firstnumber.getText().toString();
final String number2 = secondnumber.getText().toString();
btnDivide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {int divide = Integer.parseInt(number1) / Integer.parseInt(number2);
textResults.setText(number1 + " / " + number2 );
textResults.setVisibility(View.VISIBLE);
textResults.append("\n" + divide);}
catch(Exception e){
Toast.makeText(getApplicationContext(), "Cannot Divide These Numbers", Toast.LENGTH_SHORT).show();
}
}
});
btnMultiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int multiply = Integer.parseInt(number1) * Integer.parseInt(number2);
textResults.setText(number1 + " * " + number2 );
textResults.setVisibility(View.VISIBLE);
textResults.append("\n" + multiply);
}
});
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int addition = Integer.parseInt(number1) + Integer.parseInt(number2);
textResults.setText(number1 + " + " + number2 );
textResults.setVisibility(View.VISIBLE);
textResults.append("\n" + addition);
}
});
btnSubtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int subtract = Integer.parseInt(number1) - Integer.parseInt(number2);
textResults.setText(number1 + " - " + number2 );
textResults.setVisibility(View.VISIBLE);
textResults.append("\n" + subtract);
}
});
}
}
.
Logcat:
0-25 16:12:28.330 1860-1860/com.squarespace.atpublishing.calculatorapp I/art: Not late-enabling -Xcheck:jni (already on)
10-25 16:12:28.640 1860-1867/com.squarespace.atpublishing.calculatorapp E/art: Failed writing handshake bytes (-1 of 14): Broken pipe
10-25 16:12:28.640 1860-1867/com.squarespace.atpublishing.calculatorapp I/art: Debugger is no longer active
10-25 16:12:29.680 1860-1872/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 20ms
10-25 16:12:29.680 1860-1872/com.squarespace.atpublishing.calculatorapp I/art: Background partial concurrent mark sweep GC freed 371(64KB) AllocSpace objects, 0(0B) LOS objects, 47% free, 556KB/1068KB, paused 20ms total 280ms
10-25 16:12:30.370 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 40ms
10-25 16:12:30.620 1860-1900/com.squarespace.atpublishing.calculatorapp D/OpenGLRenderer: Render dirty regions requested: true
10-25 16:12:30.690 1860-1860/com.squarespace.atpublishing.calculatorapp D/: HostConnection::get() New Host Connection established 0x7fc39eccf580, tid 1860
10-25 16:12:30.690 1860-1860/com.squarespace.atpublishing.calculatorapp D/Atlas: Validating map...
10-25 16:12:30.870 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 100ms
10-25 16:12:30.890 1860-1872/com.squarespace.atpublishing.calculatorapp I/art: Background sticky concurrent mark sweep GC freed 824(47KB) AllocSpace objects, 0(0B) LOS objects, 13% free, 919KB/1068KB, paused 0 total 160ms
10-25 16:12:30.940 1860-1872/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 50ms
10-25 16:12:31.300 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 10ms
10-25 16:12:31.540 1860-1872/com.squarespace.atpublishing.calculatorapp I/art: Background sticky concurrent mark sweep GC freed 437(19KB) AllocSpace objects, 0(0B) LOS objects, 11% free, 942KB/1068KB, paused 0 total 240ms
10-25 16:12:31.680 1860-1900/com.squarespace.atpublishing.calculatorapp I/OpenGLRenderer: Initialized EGL, version 1.4
10-25 16:12:31.720 1860-1900/com.squarespace.atpublishing.calculatorapp D/OpenGLRenderer: Enabling debug mode 0
10-25 16:12:31.760 1860-1900/com.squarespace.atpublishing.calculatorapp W/EGL_emulation: eglSurfaceAttrib not implemented
10-25 16:12:31.760 1860-1900/com.squarespace.atpublishing.calculatorapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7fc39ece3040, error=EGL_SUCCESS
10-25 20:12:38.850 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 220ms
10-25 20:12:39.010 1860-1860/com.squarespace.atpublishing.calculatorapp I/Choreographer: Skipped 72 frames! The application may be doing too much work on its main thread.
10-25 20:12:39.790 1860-1860/com.squarespace.atpublishing.calculatorapp I/Choreographer: Skipped 46 frames! The application may be doing too much work on its main thread.
10-25 16:12:42.610 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 30ms
10-25 16:12:42.940 1860-1860/com.squarespace.atpublishing.calculatorapp I/Choreographer: Skipped 37 frames! The application may be doing too much work on its main thread.
10-25 16:12:43.120 1860-1900/com.squarespace.atpublishing.calculatorapp W/EGL_emulation: eglSurfaceAttrib not implemented
10-25 16:12:43.120 1860-1900/com.squarespace.atpublishing.calculatorapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7fc3a5ff6fc0, error=EGL_SUCCESS
10-25 16:12:49.020 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 150ms
10-25 16:12:49.380 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 80ms
10-25 16:12:58.690 1860-1860/com.squarespace.atpublishing.calculatorapp D/AndroidRuntime: Shutting down VM
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: FATAL EXCEPTION: main
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: Process: com.squarespace.atpublishing.calculatorapp, PID: 1860
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: java.lang.NumberFormatException: Invalid int: ""
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at java.lang.Integer.invalidInt(Integer.java:138)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at java.lang.Integer.parseInt(Integer.java:358)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at java.lang.Integer.parseInt(Integer.java:334)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at com.squarespace.atpublishing.calculatorapp.MainActivity$2.onClick(MainActivity.java:56)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at android.view.View.performClick(View.java:4756)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19749)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5221)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
10-25 16:12:58.770 1860-1860/com.squarespace.atpublishing.calculatorapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-25 16:13:05.160 1860-1867/com.squarespace.atpublishing.calculatorapp W/art: Suspending all threads took: 250ms
Remove these lines
final String number1 = firstnumber.getText().toString();
final String number2 = secondnumber.getText().toString();
And adjust this one
int divide = Integer.parseInt(firstnumber.getText()) / Integer.parseInt(secondnumber.getText());
You don't read the values of the inputs when doing calculations, so the strings will be always empty.
Variables get their values when assigned, the assignment doesn't mean they're bound to the inputs and change their values when the inputs change.

java.lang.OutOfMemoryError in Bitmap.copy function

I thought there is two copies of same object.that may be the issue.but I do not know how to solve that.please help me.Thanks in advance
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable) drawable).getBitmap();
// I got error in this line.
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
int w = getWidth(), h = getHeight();
Bitmap roundBitmap = getRoundedCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0, 0, null);
}
logcat
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: java.lang.OutOfMemoryError
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: at android.graphics.Bitmap.nativeCopy(Native Method)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: at android.graphics.Bitmap.copy(Bitmap.java:403)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: at com.example.rajitha.myapplication.RoundedImageView.onDraw(RoundedImageView.java:39)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: at android.view.View.draw(View.java:11054)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: at android.view.View.getDisplayList(View.java:10493)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: at android.view.ViewGroup.drawChild(ViewGroup.java:2958)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2596)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: at android.view.View.getDisplayList(View.java:10491)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: at android.view.ViewGroup.drawChild(ViewGroup.java:2958)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2596)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: at android.view.View.draw(View.java:11057)
10-18 11:04:33.208 32110-32110/com.e
java.lang.OutOfMemoryError: Requested size exceeds VM limit.
This error indicates that a Java application attempts ti allocate an array, whose size is larger than the heap size.
The OutOfMemoryError extends the VirtualMachineError class, which indicates that the JVM is broken, or it has run out of resources and cannot operate.
Verify that your application does not store unnecessary information.
Store and maintain only those pieces of information required for the
proper execution of your Java application.
Please Read Below Document
http://developer.android.com/intl/es/training/displaying-bitmaps/load-bitmap.html
Strange out of memory issue while loading an image to a Bitmap object

AChartEngine - Charts with data from parse.com won't display

I would like to create applications forming charts based on data from parse.com. I have read some examples and tutorials but still have problem with displaying charts. Below is my code:
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import java.util.ArrayList;
public class LineGraph {
public ArrayList<Integer> dataArray;
XYMultipleSeriesDataset dataset;
XYMultipleSeriesRenderer renderer;
public static boolean ClickEnabled = true;
public Intent getIntent(Context context) {
ArrayList<Integer> y = this.dataArray;
XYSeries seriesY = new XYSeries("Y");
for (int i = 0; i < y.size(); i++) {
seriesY.add(i, y.get(i));
}
dataset = new XYMultipleSeriesDataset();
dataset.addSeries(seriesY);
renderer.setPanEnabled(true, false);
renderer.setClickEnabled(ClickEnabled);
renderer.setBackgroundColor(Color.WHITE);
renderer.setApplyBackgroundColor(true);
renderer.setChartTitle("Simple data");
renderer.setAxesColor(Color.BLACK);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.RED);
renderer.setPointStyle(PointStyle.DIAMOND);
mRenderer.addSeriesRenderer(renderer);
Intent intent = ChartFactory.getLineChartIntent(context, dataset, mRenderer, "Line Graph Title");
return intent;
}
public void getData() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Counters_data");
query.getInBackground("lxFzCTeOcl", new GetCallback<ParseObject>() {
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
String object = parseObject.getString("value");
Integer objectValue = Integer.parseInt(object);
if (dataArray == null) {
dataArray = new ArrayList<Integer>();
dataArray.add(objectValue);
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
}
}
And there is how I invoke charts:
public void lineGraphHandler(View view) {
LineGraph line = new LineGraph();
line.getData();
Intent lineIntent = line.getIntent(this);
startActivity(lineIntent);
}
And XML part:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/counters"
android:onClick="lineGraphHandler"
android:text="Charts"
android:id="#+id/charts"/>
There is my logcat:
03-26 08:42:13.096 1229-1229/com.example.tst D/dalvikvm﹕ Late-enabling
CheckJNI 03-26 08:42:13.487 1229-1229/com.example.tst D/libEGL﹕ loaded
/system/lib/egl/libEGL_genymotion.so 03-26 08:42:13.491
1229-1229/com.example.tst D/﹕ HostConnection::get() New Host
Connection established 0xb94f4270, tid 1229 03-26 08:42:13.551
1229-1229/com.example.tst D/libEGL﹕ loaded
/system/lib/egl/libGLESv1_CM_genymotion.so 03-26 08:42:13.551
1229-1229/com.example.tst D/libEGL﹕ loaded
/system/lib/egl/libGLESv2_genymotion.so 03-26 08:42:14.035
1229-1229/com.example.tst W/EGL_genymotion﹕ eglSurfaceAttrib not
implemented 03-26 08:42:14.039 1229-1229/com.example.tst
E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache 03-26
08:42:14.043 1229-1229/com.example.tst E/OpenGLRenderer﹕
MAX_TEXTURE_SIZE: 4096 03-26 08:42:14.055 1229-1229/com.example.tst
E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from
Caches::initConstraints() 03-26 08:42:14.063 1229-1229/com.example.tst
E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 4096 03-26 08:42:14.063
1229-1229/com.example.tst D/OpenGLRenderer﹕ Enabling debug mode 0
03-26 08:42:50.327 1229-1229/com.example.tst D/dalvikvm﹕ GC_FOR_ALLOC
freed 200K, 8% free 2975K/3228K, paused 10ms, total 13ms 03-26
08:42:51.675 1229-1229/com.example.tst D/dalvikvm﹕ GC_FOR_ALLOC freed
431K, 14% free 3056K/3540K, paused 22ms, total 28ms 03-26 08:42:52.043
1229-1229/com.example.tst W/EGL_genymotion﹕ eglSurfaceAttrib not
implemented 03-26 08:42:53.543 1229-1229/com.example.tst
I/Choreographer﹕ Skipped 89 frames! The application may be doing too
much work on its main thread. 03-26 08:43:01.747
1229-1229/com.example.tst D/AndroidRuntime﹕ Shutting down VM 03-26
08:43:01.747 1229-1229/com.example.tst W/dalvikvm﹕ threadid=1: thread
exiting with uncaught exception (group=0xa4d8fb20) 03-26 08:43:01.767
1229-1229/com.example.tst E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.tst, PID: 1229 java.lang.IllegalStateException:
Could not execute method of the activity at
android.view.View$1.onClick(View.java:3823) at
android.view.View.performClick(View.java:4438) at
android.view.View$PerformClick.run(View.java:18422) at
android.os.Handler.handleCallback(Handler.java:733) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:136) at
android.app.ActivityThread.main(ActivityThread.java:5017) at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:515) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at
dalvik.system.NativeStart.main(Native Method) Caused by:
java.lang.reflect.InvocationTargetException at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:515) at
android.view.View$1.onClick(View.java:3818) at
android.view.View.performClick(View.java:4438) at
android.view.View$PerformClick.run(View.java:18422) at
android.os.Handler.handleCallback(Handler.java:733) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:136) at
android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:515) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method) Caused by:
java.lang.NullPointerException at
com.example.tst.LineGraph.getIntent(LineGraph.java:36) at
com.example.tst.MainActivity.lineGraphHandler(MainActivity.java:44)
at java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:515) at
android.view.View$1.onClick(View.java:3818) at
android.view.View.performClick(View.java:4438) at
android.view.View$PerformClick.run(View.java:18422) at
android.os.Handler.handleCallback(Handler.java:733) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:136) at
android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:515) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method) 03-26 08:43:04.507
1229-1229/com.example.tst I/Process﹕ Sending signal. PID: 1229 SIG: 9
I don't understand where the problem is. My app starts but crashes immediately when I push "chart" button. Is it data type of problem or because I misunderstand something?
Thank you in advance.
I tried like this but still got crash:
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
String object = parseObject.getString("value");
Integer objectValue = Integer.parseInt(object);
if (dataArray == null) {
dataArray = new ArrayList<Integer>();
dataArray.add(objectValue);
ArrayList<Integer> y = dataArray;
XYSeries seriesY = new XYSeries("Y");
for (int i = 0; i < y.size(); i++) {
seriesY.add(i, y.get(i));
dataset = new XYMultipleSeriesDataset();
dataset.addSeries(seriesY);
}
}
Your getData() retrieves the data asynchronously. dataArray won't be initialized immediately when you call getIntent().
Wait for the async operation to complete before using the data there. For example, call the code requiring that data from the done() callback.

Categories