0

How can I pass the data the from my intent.putExtra to the new activity which is a tablayout? I tried many ways possible but the data doesn't view plus when I swipe to another position of a tab I'll get a crash.

Here's my code

customAdapter.java

this code is from my recyclerview I added an onClickListener and using toast I was able to test the items that were displaying the appropriate data from my database which is good since It tells me I've successfully get a data from my database when I click. But now I want it to pass their values from my database to another activity but it doesnt display anything.

Note the onClick() method

public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView textViewTerm;
        List<DataModel> items = new ArrayList<DataModel>();
        Context context;

        public MyViewHolder(View itemView, Context context, List<DataModel> items) {
            super(itemView);
            this.items = items;
            this.context = context;
            itemView.setOnClickListener(this);


            this.textViewTerm = (TextView) itemView.findViewById(R.id.textViewTerm);


        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            int position = getAdapterPosition();
            DataModel item = this.items.get(position);

            Toast.makeText(context, "ID number: "+item.getId() +" is selected", Toast.LENGTH_SHORT).show();
            Intent i = new Intent(this.context,My_Notes_View_Main.class);
            i.putExtra("note_id",item.getId());
            i.putExtra("note_def",item.getDefinition());
            i.putExtra("note_syntax",item.getSyntax());
            i.putExtra("note_code", item.getCode()); 
            this.context.startActivity(i);

        }


    }

Here is my activity that contains the tablayout I tried adding an if/else statement where if their positions change the textView value replaces it's content which it gets from the other activity.It is inside the onTabSelectedListener() but that causes a crash whenever I change from another position.

package com.zaid.recyclerviewsqlite.viewNotes;

import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.ImageButton; import android.widget.TextView; import android.widget.Toast;

import com.zaid.recyclerviewsqlite.R;

public class My_Notes_View_Main extends AppCompatActivity implements View.OnClickListener {

    private final int numOfPages = 3;
    private final String[] pageTitle = {"Definition", "Syntax", "Code"};
    private ImageButton view;
    private TextView content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_notes_view_main);

        view = (ImageButton)findViewById(R.id.view);
        content = (TextView)findViewById(R.id.textView);
        view();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
        setSupportActionBar(toolbar);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);

        for (int i = 0; i < numOfPages; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(pageTitle[i]));
        }

        //set gravity for tab bar
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new My_Notes_View_MyPagerAdapter(getSupportFragmentManager(), numOfPages);

        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(onTabSelectedListener(viewPager));
    }

    private TabLayout.OnTabSelectedListener onTabSelectedListener(final ViewPager pager) {
        return new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                pager.setCurrentItem(tab.getPosition());
            /**    
                if(tab.getPosition() == 1){
                    content.setText(getIntent().getStringExtra("note_def"));
                } else if (tab.getPosition() == 2) {

                    content.setText(getIntent().getStringExtra("note_syntax"));
                } else if (tab.getPosition() == 3) {

                    content.setText(getIntent().getStringExtra("note_code"));
                }
                */
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        };
    }
       /**  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_notes_add_main_menu, menu);        
        return true;
    } */

    public void view(){
        view.setOnClickListener(new View.OnClickListener() {
                        @Override           public void onClick(View v) {
                // TODO Auto-generated method stub

                Toast.makeText(getApplicationContext(), "Add Click", Toast.LENGTH_SHORT).show();            }
            });
    }

    @Override   public void onClick(View v) {       // TODO Auto-generated method stub
            }





     }

My LogCat already made it clear that I have a nullPointerException inside my onTabSelectedListener() line 63 but I have no idea how to resolve this issue.

Here is my LogCat

 03-18 12:25:39.582: E/InputEventReceiver(16326): Exception dispatching input event.
03-18 12:25:39.582: E/MessageQueue-JNI(16326): Exception in MessageQueue callback: handleReceiveCallback
03-18 12:25:39.592: D/dalvikvm(16326): GC_FOR_ALLOC freed 242K, 8% free 3959K/4276K, paused 10ms, total 10ms
03-18 12:25:39.602: E/MessageQueue-JNI(16326): java.lang.NullPointerException
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at com.zaid.recyclerviewsqlite.viewNotes.My_Notes_View_Main$1.onTabSelected(My_Notes_View_Main.java:62)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.support.design.widget.TabLayout.selectTab(TabLayout.java:871)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.support.design.widget.TabLayout$TabLayoutOnPageChangeListener.onPageSelected(TabLayout.java:1759)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1794)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:569)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:553)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2102)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.View.dispatchTouchEvent(View.java:7711)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.View.dispatchPointerEvent(View.java:7891)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.os.MessageQueue.nativePollOnce(Native Method)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.os.MessageQueue.next(MessageQueue.java:138)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.os.Looper.loop(Looper.java:123)
03-18 12:25:39.602: E/MessageQueue-JNI(16326):  at android.app.ActivityThread
03-18 12:25:39.602: D/AndroidRuntime(16326): Shutting down VM
David Diaz
  • 113
  • 3
  • 14
  • 1
    Please add the logcat to your question and what line is the error – OneCricketeer Mar 18 '16 at 04:16
  • @cricket updated my post – David Diaz Mar 18 '16 at 04:21
  • there are two possibilities here either reference variable "content" is not getting the right id in the layout or your passing a null String using getIntent() debug these things and you are good to go – Pankaj Nimgade Mar 18 '16 at 04:25
  • If you are using `getStringExtra` you should use `putStringExtra` – OneCricketeer Mar 18 '16 at 04:29
  • @Pankaj the id for the content is correct as for passing a null String I have no idea how to solve this issue. I checked my onClick method from my CustomAdapter the intent looks fine to me – David Diaz Mar 18 '16 at 04:31
  • @cricket the putStringExtra are you referring it to the CustomAdapter? – David Diaz Mar 18 '16 at 04:33
  • yes that may be the case, use log cat to see what you are getting from the previous activity, Log.d(TAG,""+getIntent().getStringExtra("note_def")), do the same for the other string in the onCreate() of your activity and check what you are getting there – Pankaj Nimgade Mar 18 '16 at 04:36
  • @cricket thanks let me try – David Diaz Mar 18 '16 at 04:41
  • @cricket I can't test my logcat the TAG from your code says it cannot resolved to a variable also the method getIntent is undefined – David Diaz Mar 18 '16 at 04:48
  • You are tagging the wrong person. I said nothing about TAG. But TAG is just any string variable – OneCricketeer Mar 18 '16 at 04:52
  • @cricket sorry I'm still having trouble with the getIntent() it is still undefined – David Diaz Mar 18 '16 at 05:02
  • I have no idea what variables are on line 63, but one of them is obviously null. – OneCricketeer Mar 18 '16 at 05:10
  • @cricket that actually refers to the tab page which I selected in this case it is the tab with the title Syntax. there's a code in that line which is content.setText(getIntent().getStringE;xtra("note_syntax") – David Diaz Mar 18 '16 at 05:16
  • And if you click the third tab, same error? – OneCricketeer Mar 18 '16 at 05:21
  • I highly suggest making a much, much simpler view with only a Textview, pass an Intent to it, then test your getStringExtra in that – OneCricketeer Mar 18 '16 at 05:23
  • @cricket thanks for the help I was able to understand that there must not be a null value when fetching putextra but now I'm having a new.problem it still won't show the value to my textView which is in my tab but it was able to show in he logcat – David Diaz Mar 18 '16 at 05:31
  • `content` is part of the Activity layout, not the Tab. Please use `tab.setText()` – OneCricketeer Mar 18 '16 at 05:33
  • @cricket crap that was a mistake they're replacing the tab title instead. – David Diaz Mar 18 '16 at 05:36
  • It is also unclear why you only set the text when you select a tab. Shouldn't you be manually adding tabs with the specified text inside onCreate? – OneCricketeer Mar 18 '16 at 05:40
  • @cricket can you give me a sample I didn'n know about that since I'm still new to android sorry – David Diaz Mar 18 '16 at 05:44
  • Instead of your for loop over your string array with `tabLayout.addTab`.... individually do that three times with your different Intent values – OneCricketeer Mar 18 '16 at 05:47
  • @cricket Ive manually added them now as you suggested and check if my tabs are working and it did. Then I manually added content.getIntent.setText from each tab but then again it crashea – David Diaz Mar 18 '16 at 05:58
  • content.getIntent isn't a valid method... – OneCricketeer Mar 18 '16 at 06:00
  • @cricket my apologize that was a typo error I mean to say content.setText(getIntent().getStringExtra("note_def")); But it still crashe saying I'm still having a nullpointer – David Diaz Mar 18 '16 at 06:05
  • Then content is null! You haven't shown your XML layout for the activity but `findViewById` returns null if the id cannot be found. I'm assuming content is inside the ViewPager layout?? If so, then yes, it will be null from the Activity. Besides, you don't need that content.setText anymore inside the listener – OneCricketeer Mar 18 '16 at 06:19
  • @cricket it is inside my tabfragment layout – David Diaz Mar 18 '16 at 06:32

1 Answers1

0

If @+id/textView is not in my_notes_view_main.xml

Then content is null.

content = (TextView)findViewById(R.id.textView);

From the documentation for findViewById

Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).

Returns - The view if found or null otherwise

Since you mentioned that view is part of your Fragment layout, it is null, and you are getting a NullPointerException.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • where should I create my textView instead? And more importantly how do I do that? – David Diaz Mar 18 '16 at 06:39
  • Use `Fragment.setArguments` inside of `My_Notes_View_MyPagerAdapter.getItem` to pass the string that you want at the positions. Please refer to http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment – OneCricketeer Mar 18 '16 at 06:46
  • I created another different solution. inside my view_main.xml their is pager right? below it I added another textview and declare it on my activity and at the same time made an if/else statemen inside the tabselectedlistener as seen on my code and it work. Will this be ok? – David Diaz Mar 18 '16 at 06:55
  • I don't know what you want exactly, but if it is R.id.textView, then that will solve your null pointer, yes – OneCricketeer Mar 18 '16 at 06:57