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
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a trouble in my app retrieving my data from parse.com
I just wanted to recover data from website in an ArrayList made with a custom class, for using later in my app
Here is code but I can't test it on my emulator because my app crashes on boot, despite I don't have coding errors
PS: I'm very new in android coding, so any help is well accepted!
final int duration = Toast.LENGTH_SHORT;
ParseQuery<ParseObject> queryP = ParseQuery.getQuery("Menu");
queryP.orderByAscending("Id");
queryP.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> MenuList, ParseException e) {
int Id_c;
String Image_c;
String Name_c;
String Description_c;
String Type_c;
Double Cost_c;
ArrayList<Plate> FullMenu = new ArrayList<Plate>();
Plate plate;
if (e == null) {
for (ParseObject plate_o : MenuList) {
Id_c = plate_o.getInt("Id");
Image_c = plate_o.getString("Image");
Name_c = plate_o.getString("Name");
Description_c = plate_o.getString("Description");
Type_c = plate_o.getString("Type");
Cost_c = plate_o.getDouble("Cost");
String text = Name_c;
Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
FullMenu.add(Id_c, new Plate(Id_c, Image_c, Name_c, Description_c, Type_c, Cost_c));
plate = FullMenu.get(Id_c);
if (plate.getId() % 5 == 0) {
text = plate.getName();
toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
}
}
}
else {
String text = "The get request failed.";
Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
}
}
});
And here is my logcat
01-02 19:18:58.916 4406-4406/mycompany.restaurantapp I/art: Not late-enabling -Xcheck:jni (already on)
01-02 19:18:58.916 4406-4406/mycompany.restaurantapp I/art: Late-enabling JIT
01-02 19:18:58.918 4406-4406/mycompany.restaurantapp I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
01-02 19:18:58.962 4406-4406/mycompany.restaurantapp W/System: ClassLoader referenced unknown path: /data/app/mycompany.restaurantapp-2/lib/x86
01-02 19:18:59.401 4406-4438/mycompany.restaurantapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-02 19:18:59.446 4406-4406/mycompany.restaurantapp D/: HostConnection::get() New Host Connection established 0xa335d700, tid 4406
01-02 19:18:59.542 4406-4438/mycompany.restaurantapp D/: HostConnection::get() New Host Connection established 0xa335d8d0, tid 4438
01-02 19:18:59.556 4406-4438/mycompany.restaurantapp I/OpenGLRenderer: Initialized EGL, version 1.4
01-02 19:18:59.703 4406-4438/mycompany.restaurantapp W/EGL_emulation: eglSurfaceAttrib not implemented
01-02 19:18:59.704 4406-4438/mycompany.restaurantapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad79f160, error=EGL_SUCCESS
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp D/AndroidRuntime: Shutting down VM
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: FATAL EXCEPTION: main
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: Process: mycompany.restaurantapp, PID: 4406
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at java.util.ArrayList.add(ArrayList.java:147)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at mycompany.restaurantapp.MainActivity$1.done(MainActivity.java:91)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at mycompany.restaurantapp.MainActivity$1.done(MainActivity.java:69)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:115)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-02 19:19:04.799 4406-4406/mycompany.restaurantapp E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-02 19:19:11.067 4406-4406/? I/Process: Sending signal. PID: 4406 SIG: 9
FullMenu.add(Id_c, new Plate(Id_c, Image_c, Name_c, Description_c, Type_c, Cost_c));
The wrong line. You're trying to add the element to empty ArrayList at certain position. Try to make it like this
FullMenu.add(new Plate(Id_c, Image_c, Name_c, Description_c, Type_c, Cost_c));
If you want to have an access by id as key you should use Map structure.
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.
In my current android project I create two bitmaps from xml resources. Every time I build my project using the xml bitmaps as the resource for Bitmap.decodeResources() I get a null pointer exception when I try to access the bitmaps. But when I switch to using the straight .png the error goes away.
Why is decodeResources() returning null?
code where the bitmaps are created:
public OvertButton(float x, float y, float width, float height, int pressedId, int unPressedId, Resources res, boolean visible) {
\\these are null if xml bitmap is passed
pressed = BitmapFactory.decodeResource(res, pressedId);
unPressed = BitmapFactory.decodeResource(res, unPressedId);
this.visible = visible;
relocate(x, y);
resize(width, height); //throws NPE when I createScaledBitmap()
ClickableManager.registerClickable(this);
}
resource file
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/play_button_pressed"
android:filter="false"
android:antialias="false"
android:dither="false"/>
and the resize method
#Override
public void resize(float width, float height) {
box.set(box.left, box.top, box.left + width, box.top + height);
Log.d("resize",String.valueOf(height));
pressed = Bitmap.createScaledBitmap(pressed, (int)width, (int)height, true);
unPressed = Bitmap.createScaledBitmap(unPressed, (int)width, (int)height, true);
}
And the logcat
04-16 09:45:06.341 11824-11824/com.handmade.eed D/dalvikvm﹕ Late-enabling CheckJNI
04-16 09:45:06.450 11824-11824/com.handmade.eed D/MenuActivity﹕ 2130837594$$$2130837596
04-16 09:45:06.451 11824-11824/com.handmade.eed D/skia﹕ --- SkImageDecoder::Factory returned null
04-16 09:45:06.451 11824-11824/com.handmade.eed D/OvertButton﹕ true2130837591
04-16 09:45:06.457 11824-11824/com.handmade.eed D/OvertButton﹕ false2130837595
04-16 09:45:06.457 11824-11824/com.handmade.eed D/resize﹕ 252.0
04-16 09:45:06.458 11824-11824/com.handmade.eed D/AndroidRuntime﹕ Shutting down VM
04-16 09:45:06.459 11824-11824/com.handmade.eed W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41610d40)
04-16 09:45:06.461 11824-11824/com.handmade.eed E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.handmade.eed, PID: 11824
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.handmade.eed/com.handmade.eed.MenuActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
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: java.lang.NullPointerException
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:615)
at com.handmade.overt.visible.OvertButton.resize(OvertButton.java:61)
at com.handmade.overt.visible.OvertButton.<init>(OvertButton.java:29)
at com.handmade.overt.visible.OvertButton.<init>(OvertButton.java:34)
at com.handmade.eed.MenuActivity.onCreate(MenuActivity.java:72)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
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)
04-16 09:45:09.872 11824-11824/com.handmade.eed I/Process﹕ Sending signal. PID: 11824 SIG: 9
This is a case of me being dumb but here is the answer for posterity.
Bitmap is not the same as BitmapDrawable
From the Drawable Resources link above:
COMPILED RESOURCE DATATYPE:
Resource pointer to a BitmapDrawable.
Thus I had to change this:
Bitmap pressed = BitmapFactory.decodeResource(res, R.drawable.play_button_pressed_res); //this is the id for the xml referring to the png
to either this:
Drawable pressed = res.getDrawable(R.drawable.play_button_pressed_res);
or this:
Bitmap pressed = BitmapFactory.decodeResource(res, R.drawable.play_button_pressed); //this is the id for the png
I decided to use bitmap because, as I understand, it is faster.
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.
When I try to run this line in order to hide the keyboard (I get the InputMethodManager):
this.context = context;
InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); //this line crashes the app
What should I do to fix it?
(I am running it from a fragment by the way)
Crash Log:
11-03 16:20:26.700: D/AndroidRuntime(2809): Shutting down VM
11-03 16:20:26.700: W/dalvikvm(2809): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-03 16:20:26.710: E/AndroidRuntime(2809): FATAL EXCEPTION: main
11-03 16:20:26.710: E/AndroidRuntime(2809): java.lang.NullPointerException
11-03 16:20:26.710: E/AndroidRuntime(2809): at co.emuze.tabtest1.Tab1$1.onClick(Tab1.java:32)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.view.View.performClick(View.java:4084)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.view.View$PerformClick.run(View.java:16966)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.os.Handler.handleCallback(Handler.java:615)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.os.Handler.dispatchMessage(Handler.java:92)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.os.Looper.loop(Looper.java:137)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-03 16:20:26.710: E/AndroidRuntime(2809): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 16:20:26.710: E/AndroidRuntime(2809): at java.lang.reflect.Method.invoke(Method.java:511)
11-03 16:20:26.710: E/AndroidRuntime(2809): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-03 16:20:26.710: E/AndroidRuntime(2809): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 16:20:26.710: E/AndroidRuntime(2809): at dalvik.system.NativeStart.main(Native Method)
Full on click method:
public void onClick(View v) {
text1.setText(editText1.getText().toString());
InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText1.getWindowToken(), 0);
}
It would appear context is null - you show the line this.context = context, and if the right hand side context is not a variable local to that method you're doing nothing. I think what you may be looking for is context = getApplicationContext()
Dont forget to use try catch blog because in case when your keyboard not open and if you use key key board hide code app crashed
User below code in Fragment
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}