Recyclerview java.lang.IndexOutOfBoundException - java

When I scroll down too fast and click on whatever item I land on I get this following error then the app crashes: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{440a7260 position=15 id=-1, oldPos=14, pLpos:14 scrap tmpDetached no parent} I have read that it's a (known) bug in the recyclerview. Does someone have a way to bypass it? Maybe restrict scroll to let all the JSON data to load properly in recyclerview
I use the following method to send my JSON request
private void sendJsonRequest(){
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, urlUpcoming, (String) null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//Toast.makeText(getActivity(), response.toString(), Toast.LENGTH_LONG).show();
gamesList.addAll(parseJsonResponse(response));
adapterUpcoming.notifyDataSetChanged();
adapterUpcoming.setData(gamesList);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//error getting data
}
});
//add the request
mRequestQueue.add(request);
}
The parseJsonResponse returns an array list, this is where the magic happens(fetching the data from the api)
private ArrayList<Game> parseJsonResponse(JSONObject response) {
final ArrayList<Game> games = new ArrayList<>();
//{fetching data with volley} code omitted
return games;
}
OnCreateView
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_upcoming, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.upcomingGamesList);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
adapterUpcoming = new AdapterUpcoming(getActivity());
mRecyclerView.setAdapter(adapterUpcoming);
urlUpcoming = MyApplication.getUrl(); //the url from which I'm fetching my data
sendJsonRequest();
return view;
Error (The error is the same but values different):
E/AndroidRuntime? FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{52ea27c0 position=5 id=-1, oldPos=4, pLpos:4 scrap tmpDetached no parent}
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:3229)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3359)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1306)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1942)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1594)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Thanks!!

Please always post the code that you need help with. Also make sure that this code is concise and well commented. However an IndexOutOfBounds exception means that somewhere you are trying to look at a string or an array. And looking at A negative number or a number too high. But it says position 15 so it is going past the end of the array. Trying adding a check statement if(oldPosition+1

Related

Application not working on Android Kitkat

I'm creating my own application. I tested it on Android Lollipop and Marshmallow - everything works great. But whenever I try Android Kitkat, an error occurs. Here's error log:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newproject/com.example.newproject.Progi}: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.math.BigDecimal[][]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.math.BigDecimal[][]
at com.example.newproject.Progi.onCreate(Progi.java:62)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
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:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 
It looks like problem is on line 62 in "progi" class, but - as for me - everything is OK especially that on newer Android versions it works great.
public class Progi extends AppCompatActivity {
Intent intent;
Bundle liczby;
Bundle doble;
BigDecimal tablica [][];
BigDecimal punkty = new BigDecimal(-1);
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
android.content.SharedPreferences shared = android.preference.PreferenceManager.getDefaultSharedPreferences(this);
boolean warunek = shared.getBoolean("serial", false);
android.view.MenuItem menuitem = menu.getItem(0);
if(warunek)
{
menuitem.setTitle("Oceny pojedynczo");
}
else menuitem.setTitle("Oceny seryjnie");
return true;
}
public boolean onCreateOptionsMenu(Menu menu) {
android.view.MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.firstmenu, menu);
return true;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutprogi);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setTitle("Progi punktowe");
intent = getIntent();
liczby = intent.getBundleExtra("liczby");
doble = intent.getBundleExtra("doble");
tablica = (BigDecimal[][]) doble.getSerializable("doble"); //here is line 62
if(intent.hasExtra("max")) {
Bundle a = intent.getBundleExtra("max");
punkty = (BigDecimal) a.getSerializable("max");
}
przejscie(true);
if(punkty.compareTo(new BigDecimal(-3))==0)
{
procenty(findViewById(android.R.id.content));
}
else if(punkty.compareTo(new BigDecimal(-1))!=0)
{
liczymy(findViewById(android.R.id.content));
}}
And in case someone asking "what 'tablica' gets from intent?":
BigDecimal[][] tablica = new BigDecimal[16][3];
//here i get data from file and put in inside "tablica"
Bundle doble = new Bundle();
doble.putSerializable("doble", tablica);
Intent intent = new Intent(Starter.this, MyActivity.class);
intent.putExtra("doble", doble);
startActivity(intent);
Any idea what's wrong?
Try switching your array implementation (BigDecimal[] []) to ArrayList.
If you want to pass an Array through an intent on Android versions
prior to Android 5.0.1 (maybe some older versions work aswell, but up
to Android 4.3 it is NOT working) You'll have to work around it. You
could do this by converting your Array to an ArrayList.
Then you would be casting it something like this:
(ArrayList<ArrayList<BigDecimal>>) doble.getSerializable("doble");
Related SO answer

Library in Android Studio - NullPointerException

i tried to use a library (https://android-arsenal.com/details/1/1414) for a sticky header in a list view. I this configured well the library because i can use classes from there.
The code i'm using is like in the example from the link above, but i get a nullPointerExeption at ".build();"
This is the code :
public class MainActivity extends Activity {
ViewGroup containerLayout ;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
containerLayout = (ViewGroup) findViewById(R.id.containerLayout);
listView = (ListView) findViewById(R.id.list);
String[] values = new String[]{"Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View",
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
StikkyHeaderBuilder.stickTo(listView)
.setHeader(R.id.header,containerLayout )
.minHeightHeader(250)
.animator(new IconAnimator())
.build();
} }
And this is my error log:
08-14 15:10:57.188 32562-32562/com.example.cretucalinn.stickyheader1
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cretucalinn.stickyheader1, PID: 32562
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.cretucalinn.stickyheader1/com.example.cretucalinn.stickyheader1.MainActivity}:
java.lang.NullPointerException
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2412)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
at android.app.ActivityThread.access$900(ActivityThread.java:174)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at
it.carlom.stikkyheader.core.StikkyHeader.disallowTouchBehindHeader(StikkyHeader.java:47)
at
it.carlom.stikkyheader.core.StikkyHeader.build(StikkyHeader.java:38)
at
it.carlom.stikkyheader.core.StikkyHeaderBuilder$ListViewBuilder.build(StikkyHeaderBuilder.java:113)
at
it.carlom.stikkyheader.core.StikkyHeaderBuilder$ListViewBuilder.build(StikkyHeaderBuilder.java:95)
at
com.example.cretucalinn.stickyheader1.MainActivity.onCreate(MainActivity.java:61)
at android.app.Activity.performCreate(Activity.java:5458)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470) 
at android.app.ActivityThread.access$900(ActivityThread.java:174) 
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5593) 
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:1283) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
at dalvik.system.NativeStart.main(Native Method)

Android CursorAdapter giving java.lang.NullPointerException error

I am having a few issues running my custom CursorAdapter in my application. According to my logcat, the error I get:
11-08 06:41:03.228 6109-6109/? W/System.err? java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.name/com.app.name.MyActivity}: java.lang.NullPointerException
11-08 06:41:03.229 6109-6109/? W/System.err? at com.app.name.MyActivity.onCreate(MyActivity.java:71)
11-08 06:41:03.231 6109-6109/? E/AndroidRuntime? FATAL EXCEPTION: main
occurs at line 71 which sets the adapter (obj.setAdapter(myAdapter)). Initially I thought my database data retrieve function (Cursor chatCursor = mydb.selectConversation(msgId);) did not return any data but after testing it with the chatCursor.getCount() function, I realized that was not the case. Kindly assist me in solving this issue. Below are the codes for my activity, adapter and logcat. Thanks in advance.
MyAdapter.java
public class MyAdapter extends CursorAdapter {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public MyAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.chat_left, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView user = (TextView) view.findViewById(R.id.reply_user);
TextView msg = (TextView) view.findViewById(R.id.reply_msg);
String theTime = cursor.getString(cursor.getColumnIndexOrThrow("message_id"));
String theMessage = cursor.getString(cursor.getColumnIndexOrThrow("message"));
user.setText(theTime);
msg.setText(String.valueOf(theMessage));
}
}
MyActivity.java
public class MyActivity extends ListActivity {
//Utils Class
Utils util;
final Context context = this;
private ProgressBar dialog;
ListView obj;
DBHelper mydb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
util = new Utils(this);
mydb = new DBHelper(this);
Bundle extras = getIntent().getExtras();
final Integer msgId = extras.getInt("id");
obj = (ListView) findViewById(R.id.list);
Cursor chatCursor = mydb.selectConversation(msgId);
MyAdapter myAdapter = new MyAdapter(this, chatCursor);
obj.setAdapter(myAdapter);
}
}
logcat
11-08 06:41:03.228 6109-6109/? W/System.err? java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.name/com.app.name.MyActivity}: java.lang.NullPointerException
11-08 06:41:03.229 6109-6109/? W/System.err? at com.app.name.MyActivity.onCreate(MyActivity.java:71)
11-08 06:41:03.231 6109-6109/? E/AndroidRuntime? FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.name/com.app.name.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.app.name.MyActivity.onCreate(MyActivity.java:71)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
11-08 06:41:03.238 559-832/? W/ActivityManager? Force finishing activity com.app.name/.MyActivity
11-08 06:41:03.742 559-574/? W/ActivityManager? Activity pause timeout for ActivityRecord{42e71938 u0 com.app.name/.MyActivity}
Take a look to your activity_view.xml. Probably the listview ID isn't "list" as you state when you try to find it with findViewById(R.layout.list).

ListView.setAdapter NullPointerException

I was doing my homework for coursera course, when i catch NPE. Can't fix this problem for several days and can't continue without fixing. Google doesn't help.
UPDATED CODE DOWN THE PAGE
Here is my code:
MainActivity
List<Selfie> selfies = new ArrayList<Selfie>();
private CustomAdapter ADAPTER = new CustomAdapter(this, selfies);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
protected void onResume() {
super.onResume();
File youDir = new File("/sdcard/DailySelfie/");
outer : for (File f : youDir.listFiles()) {
//try to find selfie in files
if (f.isFile()) {
for (Selfie selfie : selfies) {
if (selfie.getSelfieName() == f.getName()) {
continue outer;
}
else { continue;}
}
//code below works only if file doesn't exist in selfies.
Bitmap myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
try {
selfies.add(new Selfie(f.getName(), myBitmap));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Selfie:
public class Selfie {
private Bitmap selfieBitmap;
private String selfieName;
public Selfie(String selfieName, Bitmap selfieBitmap) {
this.selfieName = selfieName;
this.selfieBitmap = selfieBitmap;
}
public String getSelfieName() {
return selfieName;
}
public Bitmap getSelfieBitmap() {
return selfieBitmap;
}
}
CustomAdapter:
private Context context;
private List<Selfie> selfies = new ArrayList<Selfie>();
public CustomAdapter(Context context, List<Selfie> selfies) {
this.context = context;
this.selfies = selfies;
}
#Override
public int getCount() {
return selfies.size();
}
#Override
public Object getItem(int position) {
return selfies.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
Selfie selfie = selfies.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = new View(context);
//get layout from row.xml
rowView = inflater.inflate(R.layout.row, null);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
imageView.setImageBitmap(selfie.getSelfieBitmap());
TextView textView = (TextView) rowView.findViewById(R.id.text);
textView.setText(selfie.getSelfieName());
}
return convertView;
}
}
ADB:
04-27 13:17:58.792: E/AndroidRuntime(1979): FATAL EXCEPTION: main
04-27 13:17:58.792: E/AndroidRuntime(1979): java.lang.NullPointerException
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.AbsListView.obtainView(AbsListView.java:2179)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.ListView.makeAndAddView(ListView.java:1840)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.ListView.fillDown(ListView.java:675)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.ListView.fillFromTop(ListView.java:736)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.ListView.layoutChildren(ListView.java:1655)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.AbsListView.onLayout(AbsListView.java:2012)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.View.layout(View.java:14289)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewGroup.layout(ViewGroup.java:4562)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.View.layout(View.java:14289)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewGroup.layout(ViewGroup.java:4562)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.View.layout(View.java:14289)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewGroup.layout(ViewGroup.java:4562)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.View.layout(View.java:14289)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewGroup.layout(ViewGroup.java:4562)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.View.layout(View.java:14289)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewGroup.layout(ViewGroup.java:4562)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.View.layout(View.java:14289)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewGroup.layout(ViewGroup.java:4562)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.View.layout(View.java:14289)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewGroup.layout(ViewGroup.java:4562)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.Choreographer.doFrame(Choreographer.java:532)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.os.Handler.handleCallback(Handler.java:730)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.os.Looper.loop(Looper.java:137)
04-27 13:17:58.792: E/AndroidRuntime(1979): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-27 13:17:58.792: E/AndroidRuntime(1979): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 13:17:58.792: E/AndroidRuntime(1979): at java.lang.reflect.Method.invoke(Method.java:525)
04-27 13:17:58.792: E/AndroidRuntime(1979): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-27 13:17:58.792: E/AndroidRuntime(1979): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-27 13:17:58.792: E/AndroidRuntime(1979): at dalvik.system.NativeStart.main(Native Method)
Seems that I have a NPE at lv.setAdapter(ADAPTER);. Found same problems on StackOverflow, but no solution helped me.
UPDATE: rewrote my code, so it's now
List<Selfie> selfies;
ListView lv;
private CustomAdapter ADAPTER;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selfies = new ArrayList<Selfie>();
lv = (ListView) findViewById(R.id.listView);
ADAPTER = new CustomAdapter(this, selfies);
}
#Override
protected void onResume() {
super.onResume();
...
lv.setAdapter(ADAPTER);
}
still have same problem - NPE. I tried to move "lv =.. ; ADAPTER =..." to onResume, tried to put it to start of onResume - it doesn't help :(
Add
private CustomAdapter ADAPTER = new CustomAdapter(this, selfies);
into onCreate method because this context is use in class methods.i.e. rewrite as
private CustomAdapter ADAPTER;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ADAPTER = new CustomAdapter(this, selfies);
}
I believe the NPE relates to your ListView. Try to structure your app so that you declare class variables and initialise them in onCreate(). That way, you reference them later on (as you do in onResume()).
List<Selfie> selfies;
private CustomAdapter ADAPTER;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.listView);
selfies = new ArrayList<Selfie>();
ADAPTER = new CustomAdapter(this, selfies);
lv.setAdapter(ADAPTER);
}
#Override
protected void onResume() {
super.onResume();
/*
*I don't quite see the purpose in this below section,
*as you can account for a null ArrayList in your adapter class
*/
if (ADAPTER.getCount() >= 0) {
lv.setAdapter(ADAPTER);
}
}
Also, to explain the comment I made above a little better, you can account for null data in your adapter getCount() by treating null data as a 0 size list:
getCount() {
if (selfies == null || selfies.isEmpty()) {
return 0;
}
return selfies.size;
}
Edit: If the problem is confined to your adapter, then it may be an NPE coming from getView(). You can try the code below:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
Selfie selfie = selfies.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row, parent, false);
} else {
rowView = convertView;
}
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
imageView.setImageBitmap(selfie.getSelfieBitmap());
TextView textView = (TextView) rowView.findViewById(R.id.text);
textView.setText(selfie.getSelfieName());
return rowView;
}
Check if this returns anything other than null:
ListView lv = (ListView) findViewById(R.id.listView);
Could be that it cannot find the list.
Since you are calling ListView lv = (ListView) findViewById(R.id.listView); in onResume, setContentView has not been called. onResume is always called before onCreate, so your ListView is not currently in the context. Hence the NullPointerException.
Change your Activity code to this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView) findViewById(R.id.listView);
}
#Override
protected void onResume() {
super.onResume();
}
The problem is about the this you have setteded, when you initialazed the new Custom.... You have to initilizate it onCreate() ! (I mean for MainActivity class )
your arraylist of selfies is empty because i see nothing added?
also your class Selfie only has getter but no setters, so how do u set a selfie to your arraylist of selfies then?
The answer is that in my getView() code i returned convertView but inflated rowView. So convertView stays null. PPartisan showed me my mistake and gave worked code(below). To up the rating i must have 15 reputation, and i don't have. Could somebody, please, up his rating and his right answer?
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
Selfie selfie = selfies.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row, parent, false);
} else {
rowView = convertView;
}
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
imageView.setImageBitmap(selfie.getSelfieBitmap());
TextView textView = (TextView) rowView.findViewById(R.id.text);
textView.setText(selfie.getSelfieName());
return rowView;
}

Fetch data from database SQLite with fragment

This is my fragment activity i am getting error while accessing database my code is :-
public class Test extends Fragment {
View view;
private Cursor c;
private final String DB = "Canary";
private SQLiteDatabase db2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.test, container, false);
Button b = (Button) view.findViewById(R.id.test_button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(),"Listning",Toast.LENGTH_LONG).show();
}
});
db2 = SQLiteDatabase.openOrCreateDatabase(DB, null);
Log.d("Lee", "working her1");
c=db2.rawQuery("SELECT * FROM business_master", null);
Log.d("Lee", "working here2");
c.moveToFirst();
String id=c.getString(c.getColumnIndex("id"));
Log.d("id", "" + id);
String email=c.getString(c.getColumnIndex("email"));
Log.d("email", email);
String password=c.getString(c.getColumnIndex("password"));
db2.close();
return view;
} }
The error i am getting :-
01-20 11:35:57.168 17081-17081/com.purplefront.canary E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.purplefront.canary, PID: 17081
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:709)
at com.purplefront.canary.Test.onCreateView(Test.java:36)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
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:5086)
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)
If i remove the database access it will work fine as expected but i need database access in fragment only.
Any help would be appreciated.

Categories

Resources