将intent.putExtras传递给新活动tablayout

时间:2016-03-18 04:14:21

标签: android android-tablayout

如何将数据从我的intent.putExtra传递给新的活动,这是一个tablayout?我尝试了很多方法,但当我滑动到标签的另一个位置时数据不会加上我会崩溃。

这是我的代码

customAdapter.java

这段代码来自我的recyclerview我添加了一个onClickListener并使用toast我能够测试从我的数据库显示相应数据的项目,这很好,因为它告诉我我已成功从我的数据库获取数据我点击了。但现在我希望它将它们的值从我的数据库传递到另一个活动,但它不会显示任何内容。

请注意onClick()方法

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);

        }


    }

这是我的活动,其中包含tablayout 我尝试添加一个if / else语句,如果它们的位置改变了textView值,则替换它从另一个activity获取的内容。它在onTabSelectedListener()内,但每当我从另一个位置改变时都会导致崩溃。

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
            }





     }

我的LogCat已经明确表示我在onTabSelectedListener()第63行中有一个nullPointerException但我不知道如何解决这个问题。

这是我的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

1 个答案:

答案 0 :(得分:0)

如果@+id/textView不在my_notes_view_main.xml

然后content为空。

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

来自findViewById

的文档
  

查找由onCreate(Bundle)中处理的XML中的id属性标识的视图。

     

返回 - 找到的视图或 null否则

由于您提到该视图是Fragment布局的一部分,因此它为null,并且您将获得NullPointerException。