Hello I make a Fragment now I m trying to show all my contacts name to my ListView I have done all things but i m still getting Cursor null I have also called getActivity(). still the error is same.
public class AllContacts extends Fragment {
public AllContacts() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_all_contacts, container, false);
ListView list = (ListView) view.findViewById(R.id.listview);
ArrayList<String> arraylist= new ArrayList<>();
String sorting = ContactsContract.Contacts.DISPLAY_NAME+ "DESC";
Uri uri = Uri.parse("content://ContactsContract");
Cursor c = getActivity().getContentResolver().query(uri,null,null,null,sorting);
while (c.moveToNext()) {
String contactName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
arraylist.add(contactName);
}//end loop
c.close();
ArrayAdapter adapter = new ArrayAdapter(getContext(),android.R.layout.simple_list_item_1,arraylist);
list.setAdapter(adapter);
return view;
}
}
Exception:
FATAL EXCEPTION: main
Process: com.example.awais_pc.contactapp, PID: 25641
java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToNext()' on a null object reference
at com.example.awais_pc.contactapp.AllContacts.onCreateView(AllContacts.java:43)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
at android.view.View.measure(View.java:20151)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
at android.view.View.measure(View.java:20151)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1549)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
at android.view.Choreographer.doCallbacks(Choreographer.java:695)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
There are two problems with your query, the first one is a missing space before the DESC keyword and the second one is the invalid Uri.
Change the following:
String sorting = ContactsContract.Contacts.DISPLAY_NAME+ "DESC";
Uri uri = Uri.parse("content://ContactsContract");
Cursor c = getActivity().getContentResolver().query(uri,null,null,null,sorting);
To this:
String sorting = ContactsContract.Contacts.DISPLAY_NAME + " DESC";
Cursor c = getActivity().getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, sorting);
Related
I have BottomNavigationViewwith 5 items. Using Viewpager and Adapter pattern to load the fragments into the container. The crash occurs when the app goes to background then killed by the system and I try to open the app from recents screen will throw this error.
Even though the app looks like it's normally reloading, it crashed (but there is no app crashed dialog) and then reloads the app from start. From the log I understand the fragment is NULL but unable to figure out what I'm doing wrong!
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.fragment.app.Fragment.setMenuVisibility(boolean)' on a null object reference
at androidx.fragment.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:123)
at androidx.viewpager.widget.ViewPager.addNewItem(ViewPager.java:1010)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1224)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
at android.view.View.measure(View.java:24545)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:733)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:805)
at android.view.View.measure(View.java:24545)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:735)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
at android.view.View.measure(View.java:24545)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:733)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:805)
at android.view.View.measure(View.java:24545)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:24545)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:24545)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:24545)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:24545)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6828)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:742)
at android.view.View.measure(View.java:24545)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3006)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1833)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2122)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7598)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
at android.view.Choreographer.doCallbacks(Choreographer.java:790)
at android.view.Choreographer.doFrame(Choreographer.java:725)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Activity code block,
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomBar = findViewById(R.id.bottom_bar_home);
bottomBar.setOnNavigationItemSelectedListener(this);
}
void initiateAdapter() {
mHomePagerAdapter = new HomePagerAdapter(getSupportFragmentManager());
pager.setAdapter(null);
pager.setAdapter(mHomePagerAdapter);
pager.setOffscreenPageLimit(NAVIGATION_COUNT); // Count is 5
pager.setCurrentItem(NAVIGATION_HOME);
bottomBar.setSelectedItemId(getCurrentTabId());
}
private int getCurrentTabId() {
switch (currentTab) {
case NAVIGATION_HOME:
return R.id.navigation_home;
case NAVIGATION_FRAG_ONE:
return R.id.navigation_frag_one;
case NAVIGATION_FRAG_TWO:
return R.id.navigation_frag_two;
case NAVIGATION_FRAG_THREE:
return R.id.navigation_frag_three;
case NAVIGATION_FRAG_FOUR:
return R.id.navigation_frag_four;
}
return 0;
}
Adapter code,
public class HomePagerAdapter extends FragmentStatePagerAdapter {
public HomePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return getFragment(position);
}
#Override
public int getCount() {
return NAVIGATION_COUNT;
}
private Fragment getFragment(int position) {
Fragment fragment = null;
switch (position) {
case NAVIGATION_HOME:
fragment = HomeFragment.newInstance();
break;
case NAVIGATION_FRAG_ONE:
fragment = FragmentOne.newInstance();
break;
case NAVIGATION_FRAG_TWO:
fragment = new FragmentTwo();
break;
case NAVIGATION_FRAG_THREE:
fragment = new FragmentThree();
break;
case NAVIGATION_FRAG_FOUR:
fragment = FragmentFour.newInstance();
break;
}
return fragment;
} }
This question already has answers here:
Android - Custom ListView Adapter - Multi Selection remove - Indexoutofbounds - why?
(6 answers)
Closed 5 years ago.
I understand that how the error is being caused as there seems to be a problem with array indexing after removal of a row at random ,so how do go around to fix it.Any help will be appreciated.
Here is my Code
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<String> reminderMessage;
private ArrayList<String> receiverName;
public DataAdapter(ArrayList<String> reminderMessage,ArrayList<String> receiverName) {
this.reminderMessage = reminderMessage;
this.receiverName = receiverName;
}
#Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup,int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards_layout, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final DataAdapter.ViewHolder viewHolder, final int i) {
viewHolder.reminderText.setText(reminderMessage.get(i));
viewHolder.receiverName.setText(receiverName.get(i));
viewHolder.button_reject.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rejectItem(viewHolder.getAdapterPosition());
}
});
}
#Override
public int getItemCount() {
return reminderMessage.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView reminderText;
private TextView receiverName;
Button button_reject;
public ViewHolder(View view) {
super(view);
reminderText = (TextView)view.findViewById(R.id.remindertext);
receiverName = (TextView)view.findViewById(R.id.receiverName);
button_reject=(Button)view.findViewById(R.id.button_reject);
}
}
private void rejectItem(int position) {
DatabaseReference mdatabase = FirebaseDatabase.getInstance().getReference();
try {
mdatabase.child("reminders").child(messageKeys.get(position)).child("status").setValue("reject");
Log.d("in the loop","writing");
} catch (Exception e) {
e.printStackTrace();
}
reminderMessage.remove(position);
receiverName.remove(position);
messageKeys.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, reminderMessage.size());
}
}
And here is the error
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.import2, PID: 6891
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.get(ArrayList.java:411)
at com.example.admin.import2.DataAdapter.onBindViewHolder(DataAdapter.java:47)
at com.example.admin.import2.DataAdapter.onBindViewHolder(DataAdapter.java:27)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6279)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6312)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5258)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5521)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2969)
at android.view.View.measure(View.java:19734)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19734)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1658)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:687)
at android.view.View.measure(View.java:19734)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1358)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1607)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.firebase.auth.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/com.example.admin.import2-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.admin.import2-2/lib/x86, /system/lib, /vendor/lib]]
Application terminated.
Why don't you just use one list of some object like this?
class Data{
String name, message;
}
Plus
public DataAdapter(List<Data> data) {
this.data = data;
}
By using two lists, you are only using the size of one, while removing and getting from both, which only causes errors later on
Besides that, messageKeys is not defined in the question, so please debug that too
If you want to use Firebase, give FirebaseUI a chance
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I want use viewPager in my application and I want use this library: UltraViewPager.
I write below codes in adapter:
public class BoxOfficeAdapter extends PagerAdapter {
private Context context;
private List<BoxOfficeDatum> model;
private LayoutInflater inflater;
private BoxOfficeDateListener listener;
public BoxOfficeAdapter(Context context, List<BoxOfficeDatum> model, BoxOfficeDateListener listener) {
this.context = context;
this.model = model;
this.listener = listener;
this.inflater = (LayoutInflater) this.context.getSystemService(this.context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return model.size();
}
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
View view = this.inflater.inflate(R.layout.row_box_office, container, false);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
But when run application, show me this error :
FATAL EXCEPTION: main
Process: com.example.app, PID: 5948
java.lang.NullPointerException: Attempt to invoke virtual method 'int com.tmall.ultraviewpager.UltraViewPagerAdapter.getCount()' on a null object reference
at com.tmall.ultraviewpager.UltraViewPagerView.getCurrentItem(UltraViewPagerView.java:220)
at com.tmall.ultraviewpager.UltraViewPagerView.onMeasurePage(UltraViewPagerView.java:88)
at com.tmall.ultraviewpager.UltraViewPagerView.onMeasure(UltraViewPagerView.java:84)
at android.view.View.measure(View.java:19826)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at com.tmall.ultraviewpager.UltraViewPager.onMeasure(UltraViewPager.java:177)
at android.view.View.measure(View.java:19826)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.CardView.onMeasure(CardView.java:211)
at android.view.View.measure(View.java:19826)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19826)
at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1420)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:482)
at android.view.View.measure(View.java:19826)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19826)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1658)
at android.view.View.measure(View.java:19826)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:19826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19826)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:688)
at android.view.View.measure(View.java:19826)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2335)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1410)
06-04 16:37:47.075 5948-5948/com.example.app E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1660)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1286)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6536)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
How can I fix it?
List is null that why null Pointer is coming .
try different method to set data from response to your list
Like:
BoxOfficeResponse responseBody=response.body();
boxOfficeModel.addAll(responseBody.getBoxOfficeData().getBoxOfficeDatum())
or
for(int i=0; i<responseBody.getBoxOfficeData().getBoxOfficeDatum(); i++){
boxOfficeModel.add(responseBody.getBoxOfficeData().getBoxOfficeDatum().get(i))
}
I want to add new TextView to my ListView which is already in my XML file.
Just before add them, I setText() with static String[].
I've tried a lot of solutions but I got sometimes an exception about TextView id, and other times this one:
04-20 14:51:57.139 8034-8034/com.example.irit.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.irit.myapp, PID: 8034
java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
at android.widget.AdapterView.addView(AdapterView.java:700)
at com.example.irit.myapp.Model.ListObjet.creatView(ListObjet.java:65)
at com.example.irit.myapp.Model.ListObjet.access$100(ListObjet.java:31)
at com.example.irit.myapp.Model.ListObjet$1.onClick(ListObjet.java:50)
at android.view.View.performClick(View.java:5697)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
My class:
public class ListObjet{
private ImageButton mButton;
private EnvoiMailActivity mEnvoiMailActivity;
private LayoutInflater inflater;
private ListView mList;
private int ressource;
public ListObjet(EnvoiMailActivity pEnvoiMailActivity, int resourceId) {
this.ressource = resourceId;
this.mEnvoiMailActivity = pEnvoiMailActivity;
this.inflater = LayoutInflater.from(mEnvoiMailActivity);
this.mList = (ListView) inflater.inflate(this.ressource, null);
this.mButton = (ImageButton) mEnvoiMailActivity.findViewById(R.id.listobjet);
this.mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder myList = new AlertDialog.Builder(mEnvoiMailActivity);
myList.setView(creatView());
AlertDialog alert = myList.create();
alert.show();
}
});
}
private View creatView(){
ScrollView mScrollView = new ScrollView(mEnvoiMailActivity);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mEnvoiMailActivity, this.ressource, Conf.OBJET_PROPOSE);
this.mList.setAdapter(adapter);
for (int i = 0; i < Conf.OBJET_PROPOSE.length; i++ ) {
TextView item = new TextView(mEnvoiMailActivity);
item.setText(Conf.OBJET_PROPOSE[i]);
mList.addView(item);
}
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//todo
}
});
mScrollView.addView(mList);
return mScrollView;
}
}
I changed my code:
private View creatView(){
ScrollView mScrollView = new ScrollView(mEnvoiMailActivity);
ArrayAdapter<TextView> adapter = new ArrayAdapter<TextView>(mEnvoiMailActivity, this.ressource);
this.mList.setAdapter(adapter);
for (int i = 0; i < Conf.OBJET_PROPOSE.length; i++ ) {
TextView item = new TextView(mEnvoiMailActivity);
item.setText(Conf.OBJET_PROPOSE[i]);
adapter.add(item);
}
This exception occured:
04-20 15:07:51.819 32350-32350/com.example.irit.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.irit.myapp, PID: 32350
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:393)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
at android.widget.AbsListView.obtainView(AbsListView.java:2929)
at android.widget.ListView.onMeasure(ListView.java:1180)
at android.view.View.measure(View.java:20151)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:2104)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.widget.ScrollView.onMeasure(ScrollView.java:510)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
at android.view.View.measure(View.java:20151)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1524)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
at android.view.Choreographer.doCallbacks(Choreographer.java:695)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.ClassCastException: android.widget.ListView cannot be cast to android.widget.TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
at android.widget.AbsListView.obtainView(AbsListView.java:2929)
at android.widget.ListView.onMeasure(ListView.java:1180)
at android.view.View.measure(View.java:20151)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:2104)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.widget.ScrollView.onMeasure(ScrollView.java:510)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
at android.view.View.measure(View.java:20151)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1524)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
at android.view.Choreographer.doCallbacks(Choreographer.java:695)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Instead of assigning TextView dynamically, just pass id of layout which contains on TextView. You can create custom layout having only TextView or you can use default template layout provided by android sdk itself.
example:
android.R.layout.simple_list_item_1
full example:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,list);
I'm trying to retrieve the objectId as a string from my User class in Parse, but I'm getting a NullPointer.
Log.w(getClass().toString(), ParseUser.getCurrentUser().getParseObject("currentGroup").getObjectId());
java.lang.NullPointerException: println needs a message
I'm querying the column "currentGroup", which is a pointer to a class called "Group". All I want is to retrieve its objectId. Both the currentGroup and Group objectId columns have data. They are not null. What's going on here?
Error:
It seems to think the object is null but I can't see why it would be.
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String com.parse.ParseObject.getObjectId()' on a null object reference
at at com.yitter.android.fragment.FeedFragment.retrieveData(FeedFragment.java:112)
at com.yitter.android.fragment.FeedFragment.onCreateView(FeedFragment.java:105)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1638)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
at android.view.View.measure(View.java:18794)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18794)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1085)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18794)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18794)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
ParseQuery<ParseObject> query = ParseQuery.getQuery("Orders");
ParseObject obj = ParseObject.createWithoutData("ShopLocations",idd); // this pointer object class name and pointer value
query.whereEqualTo("shop", obj); // this pointer object and parse object
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> object, ParseException e) {
if (e == null) {}}
This is working correctly retrieving data from parse.
I found the solution. You can not retrieve the object of a pointer using ParseUser.getCurrentUser(). You have to do a full ParseUser query to retrieve the current User, by finding the user in your User table with an objectId equal to ParseUser.getCurrentUser().getObjectId().
ParseQuery query = ParseUser.getQuery();
query.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
query.findInBackground(new FindCallback() {
#Override
public void done(List<ParseObject> users, ParseException e) {
if (e == null) for (userObject : users) {
// I retrieve the objectId from the pointer
// only after directly querying for the current User.
userObject.getParseObject("currentGroup").getObjectId();
}
}
});
At least, this is the only way I managed to get this to work in my app. I'm not sure why the .getParseObject() method is available to ParseUser.getCurrentUser() if you can't just directly retrieve the objectId of the specified pointer object, but oh well. If anyone knows, I'd love to know how you did it.