NineOldAndroids animation not working on API>10 - java

I am using NineOldAndroid library to perform animations. The animations work fine for API<=10. But for API>10 the app force closes. This is my code:
import static com.nineoldandroids.view.ViewPropertyAnimator.animate;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ObjectAnimator;
public class ActivityActualMain extends SherlockActivity {
LinearLayout container1, container2;
RelativeLayout viewTree;
ImageView image, image1, image2;
TextView tv, tv1, tv2, tv3, tv4;
ObjectAnimator anim;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final int duration = 2000;
setContentView(R.layout.activity_actual_main);
ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);
bar.setBackgroundDrawable(getResources().getDrawable(
R.drawable.red_actionbar));
viewTree = (RelativeLayout) findViewById(R.id.viewTree);
container1 = (LinearLayout) findViewById(R.id.linearLayout1);
container2 = (LinearLayout) findViewById(R.id.linearLayout2);
image = (ImageView) findViewById(R.id.imageView1);
image1 = (ImageView) findViewById(R.id.imageView2);
image2 = (ImageView) findViewById(R.id.imageView3);
tv = (TextView) findViewById(R.id.text_tech_des);
viewTree.getViewTreeObserver().addOnGlobalLayoutListener(//to check if the layout has been placed in activity
new OnGlobalLayoutListener() {
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < 16) {
viewTree.getViewTreeObserver().removeGlobalOnLayoutListener(this);}
else{
viewTree.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}
anim = ObjectAnimator.ofFloat(image, "y", 0f,
image.getTop());
anim.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator arg0) {
// TODO Auto-generated method stub
ObjectAnimator.ofFloat(tv, "alpha", 1, 0, 1)
.setDuration(duration).start();//line no 82
ObjectAnimator.ofFloat(container1, "x", 0f,
container1.getLeft()).setDuration(1000).start();
ObjectAnimator.ofFloat(container2, "x", 0f,
container2.getLeft()).setDuration(1000).start();
}
#Override
public void onAnimationRepeat(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationCancel(Animator arg0) {
// TODO Auto-generated method stub
}
});
anim.setDuration(duration).start();//line no 106
}
});
}
}
This is my stack trace:
10-26 19:23:15.203: E/AndroidRuntime(21541): FATAL EXCEPTION: main
10-26 19:23:15.203: E/AndroidRuntime(21541): java.lang.NullPointerException
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:523)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:410)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:538)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:928)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.vishalaksh.technex.ActivityActualMain$2$1.onAnimationStart(ActivityActualMain.java:82)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:937)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.vishalaksh.technex.ActivityActualMain$2.onGlobalLayout(ActivityActualMain.java:106)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:808)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1768)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.Choreographer.doFrame(Choreographer.java:532)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.os.Handler.handleCallback(Handler.java:730)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.os.Looper.loop(Looper.java:137)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-26 19:23:15.203: E/AndroidRuntime(21541): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 19:23:15.203: E/AndroidRuntime(21541): at java.lang.reflect.Method.invoke(Method.java:525)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-26 19:23:15.203: E/AndroidRuntime(21541): at dalvik.system.NativeStart.main(Native Method)

I had an issue with this library on devices API < 10, which gave the same stacktrace. Given i was using the source code of NineOldAndroids i decided to investigate the problem inside the library.
After some tests i noticed that this error occurs because the lib tries to invoke some methods that doesn't exists on the view (since its an old API level). Searching a bit more i found the AnimatorProxy class which has a static method called "wrap". This method is used to encapsulate View objects in older Android versions emulating the presence of some animation methods as such setScaleX/Y, setTransalationX/Y.
To fix the problem i had to open the ObjectAnimator class and search for all occurrences of this line (In my library code i found 4 occurrences):
mTarget = target;
Inside that class i created the following method:
private void setTarget(Object obj) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB && obj instanceof View) {
mTarget = AnimatorProxy.wrap((View) obj);
} else {
mTarget = obj;
}
}
And replaced the line above with:
setTarget(target);
Don't know if it can fix your problem since you said that it occurs on API 10+ (the opposite of mine) but its a good point to start.

Related

Unfortunately app has stopped in Android when try to implement RecylerView

I am following slidenerd Material design tutorial(Android RecyclerView Example part1,2,3,4) & I am using real device to test my android application.I have successfully build navigation drawer & when I try to add RecylerView into it by adding adapter (ThalakazAdapter.java) , app unfortunately get stopped.
This is my NavigationDrawerFragment.java
package com.thaalakaz.nadeesha.materialdesignapp1;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
//import android.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {#link Fragment} subclass.
*/
public class NavigationDrawerFragment extends Fragment {
private RecyclerView recyclerView;
public static final String PREF_FILE_NAME = "testpref";
public static final String KAY_USER_LEARNED_DRAWER ="user_learned_drawer";
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private ThalakazAdapter adapter;
private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;
private View containerView;
public NavigationDrawerFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUserLearnedDrawer=Boolean.valueOf(readFromPreferences(getActivity(),KAY_USER_LEARNED_DRAWER,"false"));
if (savedInstanceState != null){
mFromSavedInstanceState = true;
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawer_layout);
adapter = new ThalakazAdapter(getActivity(),getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<Information> getData(){
List<Information> data = new ArrayList<>();
int[] icons ={R.drawable.ic_m,R.drawable.ic_m1,R.drawable.ic_m2,R.drawable.ic_m3};
String[] titles = {"Nadeesha","Thilakarathne","Semini","Roshani"};
for (int i=0;i<titles.length && i<icons.length;i++){
Information current = new Information();
current.iconId = icons[i];
current.titlel = titles[i];
data.add(current);
}
return data;
}
public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) {
containerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerToggle = new ActionBarDrawerToggle(getActivity(),drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if(!mUserLearnedDrawer){
mUserLearnedDrawer = true;
saveToPreferences(getActivity(),KAY_USER_LEARNED_DRAWER,mUserLearnedDrawer+"");
}
getActivity().invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
if (slideOffset<0.6){
toolbar.setAlpha(1-slideOffset);
}
}
};
if (!mUserLearnedDrawer && mFromSavedInstanceState){
mDrawerLayout.openDrawer(containerView);
}
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
}
public static void saveToPreferences(Context context,String prferenceName,String preferenceValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(prferenceName,preferenceValue);
editor.apply();
}
public static String readFromPreferences(Context context,String prferenceName,String defaultValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME,Context.MODE_PRIVATE);
return sharedPreferences.getString(prferenceName,defaultValue);
}
}
This is my Adapter (ThalakazAdapter.java)
package com.thaalakaz.nadeesha.materialdesignapp1;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
/**
* Created by hp on 7/22/2015.
*/
public class ThalakazAdapter extends RecyclerView.Adapter <ThalakazAdapter.myViewHolder>{
private LayoutInflater inflater;
List<Information> data = Collections.emptyList();
public ThalakazAdapter(Context context, List<Information> data){
inflater=LayoutInflater.from(context);
this.data = data;
}
#Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.custom_row, parent, false);
myViewHolder holder = new myViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(myViewHolder holder, int position) {
Information current = data.get(position);
holder.title.setText(current.titlel);
holder.icon.setImageResource(current.iconId);
}
#Override
public int getItemCount() {
return 0;
}
class myViewHolder extends RecyclerView.ViewHolder{
ImageView icon;
TextView title;
public myViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.listText);
icon = (ImageView) itemView.findViewById(R.id.listIcon);
}
}
}
My information.java class
package com.thaalakaz.nadeesha.materialdesignapp1;
/**
* Created by hp on 7/22/2015.
*/
public class Information {
int iconId;
String titlel;
}
You can view my entire project from here
Logcat
07-21 22:32:07.101 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 D/dalvikvm﹕ Late-enabling CheckJNI
07-21 22:32:07.111 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 I/SystemProperties﹕ get key=ro.kernel.android.tracing
07-21 22:32:07.281 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
07-21 22:32:07.281 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 W/dalvikvm﹕ VFY: unable to resolve virtual method 14685: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
07-21 22:32:07.281 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
07-21 22:32:07.301 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
07-21 22:32:07.301 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 W/dalvikvm﹕ VFY: unable to resolve virtual method 414: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-21 22:32:07.301 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-21 22:32:07.311 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
07-21 22:32:07.311 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 W/dalvikvm﹕ VFY: unable to resolve virtual method 436: Landroid/content/res/TypedArray;.getType (I)I
07-21 22:32:07.311 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-21 22:32:07.381 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 D/AndroidRuntime﹕ Shutting down VM
07-21 22:32:07.381 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41027208)
07-21 22:32:07.391 7099-7099/com.thaalakaz.nadeesha.materialdesignapp1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thaalakaz.nadeesha.materialdesignapp1/com.thaalakaz.nadeesha.materialdesignapp1.MainActivity}: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.thaalakaz.nadeesha.materialdesignapp1.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
            at android.app.ActivityThread.access$600(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.thaalakaz.nadeesha.materialdesignapp1.NavigationDrawerFragment.onCreateView(NavigationDrawerFragment.java:63)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:924)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1116)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1218)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2170)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:300)
at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(AppCompatDelegateImplV7.java:838)
at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:34)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:826)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:668)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.thaalakaz.nadeesha.materialdesignapp1.MainActivity.onCreate(MainActivity.java:20)
            at android.app.Activity.performCreate(Activity.java:4465)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
            at android.app.ActivityThread.access$600(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
            at dalvik.system.NativeStart.main(Native Method)
fragment_navigation_drawer.xml
<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:background="#DADADA"
tools:context="com.thaalakaz.nadeesha.materialdesignapp1.NavigationDrawerFragment">
<LinearLayout
android:id="#+id/containerDrawerImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF6F00">
<ImageView
android:layout_width="280dp"
android:layout_height="140dp"
android:src = "#drawable/dog"
android:layout_margin="16dp"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_height="match_parent"
android:layout_width="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
in your onCreateView your are looking for the wrong id. Change
recyclerView = (RecyclerView) layout.findViewById(R.id.drawer_layout);
with
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
I also noticed that in your Adapter's constructor, you are assingn this.data to itself. Change
this.data = this.data;
to
this.data = data
and getItemCount shouldn't return 0, but the size of data (if it is not null)

Null pointer exception on CursorAdapter

I am getting a NullPointerException. Below you can find my Logcat and the relevant code.
Logcat:
12-23 00:17:35.330: E/AndroidRuntime(2019): FATAL EXCEPTION: main
12-23 00:17:35.330: E/AndroidRuntime(2019): Process: com.android.timesheet, PID: 2019
12-23 00:17:35.330: E/AndroidRuntime(2019): java.lang.NullPointerException
12-23 00:17:35.330: E/AndroidRuntime(2019): at com.android.timesheet.adapter.CustomCursorAdapter$1.onClick(CustomCursorAdapter.java:54)
12-23 00:17:35.330: E/AndroidRuntime(2019): at android.view.View.performClick(View.java:4438)
12-23 00:17:35.330: E/AndroidRuntime(2019): at android.view.View$PerformClick.run(View.java:18422)
12-23 00:17:35.330: E/AndroidRuntime(2019): at android.os.Handler.handleCallback(Handler.java:733)
12-23 00:17:35.330: E/AndroidRuntime(2019): at android.os.Handler.dispatchMessage(Handler.java:95)
12-23 00:17:35.330: E/AndroidRuntime(2019): at android.os.Looper.loop(Looper.java:136)
12-23 00:17:35.330: E/AndroidRuntime(2019): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-23 00:17:35.330: E/AndroidRuntime(2019): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 00:17:35.330: E/AndroidRuntime(2019): at java.lang.reflect.Method.invoke(Method.java:515)
12-23 00:17:35.330: E/AndroidRuntime(2019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-23 00:17:35.330: E/AndroidRuntime(2019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-23 00:17:35.330: E/AndroidRuntime(2019): at dalvik.system.NativeStart.main(Native Method)
CustomCursorAdapter.java:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.TextView;
import com.android.timesheet.ModifyMember;
import com.android.timesheet.R;
public class CustomCursorAdapter extends CursorAdapter {
Button delete_btn;
TextView memID_tv, memName_tv;
#SuppressWarnings("deprecation")
public CustomCursorAdapter(Activity context, Cursor c) {
super(context, c);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// when the view will be created for first time,
// we need to tell the adapters, how each item will look
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.single_row_item, parent, false);
return retView;
}
#Override
public void bindView(View view, final Context context, Cursor cursor) {
// here we are setting our data
// that means, take the data from the cursor and put it in views
TextView textViewPersonName = (TextView) view.findViewById(R.id.tv_person_name);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
delete_btn=(Button)view.findViewById(R.id.delete_btn);
delete_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
memID_tv = (TextView)v.findViewById(R.id.member_id);
memName_tv = (TextView)v.findViewById(R.id.member_name);
String memberID_val = memID_tv.getText().toString();; ---->54th Line
String memberName_val = memName_tv.getText().toString();
Intent i = new Intent(context,
ModifyMember.class);
i.putExtra("memberName", memberName_val);
i.putExtra("memberID", memberID_val);
((Activity)context).startActivity(i);
}
});
}
}
I am using a delete button to delete all the listview row items. At that point I am getting the NullPointerException.
Use view(view of row) instead of v parameter of onClick method which is view of Button :
delete_btn=(Button)view.findViewById(R.id.delete_btn);
delete_btn.setTag(view);
delete_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View rowview=(View)v.getTag();
memID_tv = (TextView)rowview.findViewById(R.id.member_id);
memName_tv = (TextView)rowview.findViewById(R.id.member_name);
}
});
Use view instead of v
memID_tv = (TextView)view.findViewById(R.id.member_id);
In bindview try this-
if (view == null) {
view = inflater.inflate(R.layout.single_row_item, parent, null);
}
TextView textViewPersonName = (TextView) view.findViewById(R.id.tv_person_name);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
delete_btn=(Button)view.findViewById(R.id.delete_btn);
.
.
.
you are getting the view from clicked view, but you have to get the views from the root view.
memID_tv = (TextView)v. findViewById(R.id.member_id);
memName_tv = (TextView)v.findViewById(R.id.member_name);
change these lines into this:
memID_tv = (TextView)view. findViewById(R.id.member_id);
memName_tv = (TextView)view.findViewById(R.id.member_name);

NullPointerException whenever I try to switch tabs

I currently have an activity with 3 tabs, each containing it's own fragment. The first and second tab are relatively the same, as far as layout goes. The first tab loads fine whenever the activity loads, however when i switch to the second tab, I immediately get a NullPointerException. Here is the code for the second tab
StudentFragmentTab2
[package]
[imports]
public class StudentFragmentTab2 extends Fragment {
View view;
LinearLayout layout;
ListView listview;
String Student_JSON;
JSONArray examArray;
ExamObject exams;
ArrayList<ExamObject> list = new ArrayList<ExamObject>();
public static loginFunctions session = new loginFunctions();
AdapterView.AdapterContextMenuInfo info;
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_student_tab1, container, false);
Bundle args = getArguments();
Student_JSON = args.getString("STUDENT_JSON");
try {
examArray = new JSONArray(Student_JSON);
for (int i = 0; i < examArray.length(); i++) {
if (examArray.getJSONObject(i).getString("examTaken").equals("False")) {
exams = new ExamObject();
exams.setId(examArray.getJSONObject(i).getString("examID"));
exams.setName(examArray.getJSONObject(i).getString("examName"));
}
list.add(exams);
}
layout = (LinearLayout) view;
listview = (ListView) view.findViewById(R.id.listView1);
listview.setAdapter(new CurrentExamAdapter(list,
getActivity().getBaseContext()));
registerForContextMenu(listview);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
} catch (JSONException e) {
Log.w("JSON PARSING ERROR",
"Imported JSON String can't be converted to JSON object");
e.printStackTrace();
}
return view;
}
...
}
The NullPointerException points to the following class CurrentExamAdapter
CurrentExamAdapter
[package]
[imports]
public class CurrentExamAdapter extends BaseAdapter {
private ArrayList<ExamObject> data;
Context c;
public CurrentExamAdapter(ArrayList<ExamObject> datain, Context cin) {
data = datain;
c = cin;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_current_exams, null);
}
TextView examName = (TextView) v.findViewById(R.id.EXAMNAME);
TextView examStatus = (TextView) v.findViewById(R.id.status);
ExamObject msg = data.get(position);
examName.setText(msg.EXAM_NAME); //<---LOGCAT says this is
// where the exception occurs
examStatus.setText(msg.EXAM_STATUS);
return v;
}
}
The first tab fragment is completely identical to the second, except for a different condition. What is the app trying to reference that isn't there?
EDIT
As requested, here is the stack trace:
10-26 04:28:41.529: W/IInputConnectionWrapper(21182): showStatusIcon on inactive InputConnection
10-26 04:28:44.856: I/Adreno-EGL(22413): <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_2.7_RB1.04.04.02.007.040_msm8960_KK_2.7_RB1_CL3869936_release_AU (CL3869936)
10-26 04:28:44.856: I/Adreno-EGL(22413): OpenGL ES Shader Compiler Version: 17.01.11.SPL
10-26 04:28:44.856: I/Adreno-EGL(22413): Build Date: 02/26/14 Wed
10-26 04:28:44.856: I/Adreno-EGL(22413): Local Branch:
10-26 04:28:44.856: I/Adreno-EGL(22413): Remote Branch: quic/kk_2.7_rb1.29
10-26 04:28:44.856: I/Adreno-EGL(22413): Local Patches: NONE
10-26 04:28:44.856: I/Adreno-EGL(22413): Reconstruct Branch: NOTHING
10-26 04:28:44.903: D/OpenGLRenderer(22413): Enabling debug mode 0
10-26 04:28:53.881: W/InputEventReceiver(22413): Attempted to finish an input event but the input event receiver has already been disposed.
10-26 04:28:59.566: D/AndroidRuntime(22413): Shutting down VM
10-26 04:28:59.566: W/dalvikvm(22413): threadid=1: thread exiting with uncaught exception (group=0x416dad40)
10-26 04:28:59.577: E/AndroidRuntime(22413): FATAL EXCEPTION: main
10-26 04:28:59.577: E/AndroidRuntime(22413): Process: com.example.cs490project, PID: 22413
10-26 04:28:59.577: E/AndroidRuntime(22413): java.lang.NullPointerException
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.example.cs490project.CurrentExamAdapter.getView(CurrentExamAdapter.java:47)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.AbsListView.obtainView(AbsListView.java:2255)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.ListView.onMeasure(ListView.java:1175)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.View.measure(View.java:16537)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.Choreographer.doFrame(Choreographer.java:544)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.os.Handler.handleCallback(Handler.java:733)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.os.Handler.dispatchMessage(Handler.java:95)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.os.Looper.loop(Looper.java:136)
10-26 04:28:59.577: E/AndroidRuntime(22413): at android.app.ActivityThread.main(ActivityThread.java:5086)
10-26 04:28:59.577: E/AndroidRuntime(22413): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 04:28:59.577: E/AndroidRuntime(22413): at java.lang.reflect.Method.invoke(Method.java:515)
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-26 04:28:59.577: E/AndroidRuntime(22413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-26 04:28:59.577: E/AndroidRuntime(22413): at dalvik.system.NativeStart.main(Native Method)
The null pointer expception kept emerging from this line:
ExamObject msg = data.get(position);
If convert that piece of code to:
if(data.get(position) != null){
ExamObject msg = data.get(position);
examName.setText(msg.EXAM_NAME);
examStatus.setText(msg.EXAM_STATUS);
}
The second tab will show as it should. Strange, but I'm not complaining.

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.

How to print my file?

Hi how to print file list...now I trying to print my file list...but I am its not printing...please help me....
This is my coding:
package ImageViewExample.ImageViewExample;
import java.io.File;
import java.util.ArrayList;
import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
//import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class ImageViewExample extends Activity {
/** Called when the activity is first created. */
private Cursor imagecursor, actualimagecursor;
private int image_column_index, actual_image_column_index;
GridView imagegrid;
private int count;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init_phone_image_grid();
}
private void init_phone_image_grid() {
String[] img = { MediaStore.Images.Thumbnails._ID };
imagecursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
img, null,null,
MediaStore.Images.Thumbnails.IMAGE_ID + "");
System.out.println("cursor"+managedQuery(MediaStore.Images
.Thumbnails.EXTERNAL_CONTENT_URI, img, null,null,
MediaStore.Images.Thumbnails.IMAGE_ID + ""));
image_column_index =
imagecursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
System.out.println("index"+imagecursor.getColumnIndexOrThrow(MediaStore
.Images.Thumbnails._ID));
count = imagecursor.getCount();
System.out.println("image"+count);
imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imagegrid.setAdapter(new ImAdapterh(this));
imagegrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,int position, long id)
{
System.gc();
String[] proj = { MediaStore.Images.Media.DATA };
actualimagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
proj,null, null, null);
// System.out.println("doubt"+managedQuery(MediaStore
.Images.Media.EXTERNAL_CONTENT_URI, proj,null, null, null));
actual_image_column_index =
actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToPosition(position);
String i = actualimagecursor.getString(actual_image_column_index);
System.gc();
Intent intent = new Intent(getApplicationContext(), ViewImage.class);
intent.putExtra("filename", i);
startActivity(intent);
}
});
}
public class ImAdapterh extends BaseAdapter{
File dir=new File(Environment.getExternalStorageDirectory(),"/myImages/");
int count=dir.list().length;
String[] fileNames = dir.list();
private Context mContext;
public ImAdapterh(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
for(String bitmapFileName : fileNames)
{
if (convertView == null)
{ // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new Gallery.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
Bitmap bmp = BitmapFactory.decodeFile(dir.getPath() + "/" +
bitmapFileName);
imageView.setImageBitmap(bmp);
}else
{
imageView = (ImageView) convertView;
}
} return imageView;
}
}}
I wish to print my file list:
File dir=new File(Environment.getExternalStorageDirectory(),"/myImages/");
int count=dir.list().length;
String[] fileNames = dir.list();
logcat error:
05-05 22:39:42.147: DEBUG/dalvikvm(739): GC_EXTERNAL_ALLOC freed 1035 objects / 68800
bytes in 141ms
05-05 22:39:42.497: DEBUG/dalvikvm(739): GC_EXTERNAL_ALLOC freed 418 objects / 217344
bytes in 118ms
05-05 22:39:42.568: DEBUG/AndroidRuntime(739): Shutting down VM
05-05 22:39:42.578: WARN/dalvikvm(739): threadid=1: thread exiting with uncaught
exception (group=0x4001d800)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): FATAL EXCEPTION: main
05-05 22:39:42.608: ERROR/AndroidRuntime(739): java.lang.ClassCastException:
android.widget.Gallery$LayoutParams
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.widget.GridView.onMeasure(GridView.java:934)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.view.View.measure(View.java:8171)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.view.View.measure(View.java:8171)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.view.View.measure(View.java:8171)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.view.View.measure(View.java:8171)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.view.View.measure(View.java:8171)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.view.ViewRoot.performTraversals(ViewRoot.java:801)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.os.Handler.dispatchMessage(Handler.java:99)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.os.Looper.loop(Looper.java:123)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
android.app.ActivityThread.main(ActivityThread.java:4627)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
java.lang.reflect.Method.invokeNative(Native Method)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
java.lang.reflect.Method.invoke(Method.java:521)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-05 22:39:42.608: ERROR/AndroidRuntime(739): at
dalvik.system.NativeStart.main(Native Method)
05-05 22:39:42.638: WARN/ActivityManager(64): Force finishing activity
ImageViewExample.ImageViewExample/.ImageViewExample
05-05 22:39:43.157: WARN/ActivityManager(64): Activity pause timeout for
HistoryRecord{43fb0c88 ImageViewExample.ImageViewExample/.ImageViewExample}
This will print your files to the logcat, but note that this code has to be located in some method, not just in the class.
for (String fName : fileNames)
{
Log.i("MYAPPTAG", fName);
}

Categories