问题描述
- 从activity调用ViewPager里Fragment的方法传参出现空指针异常?
-
报错信息如下:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thefat.lingjunqi.enricher/com.thefat.lingjunqi.enricher.ActivityReading}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5338)
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: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.thefat.lingjunqi.enricher.FragmentArticleEN.refresh(FragmentArticleEN.java:32)
at com.thefat.lingjunqi.enricher.ActivityReading.onCreate(ActivityReading.java:103)
at android.app.Activity.performCreate(Activity.java:5232)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2272)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)?
at android.app.ActivityThread.access$600(ActivityThread.java:156)?
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)?
at android.os.Handler.dispatchMessage(Handler.java:99)?
at android.os.Looper.loop(Looper.java:153)?
at android.app.ActivityThread.main(ActivityThread.java:5338)?
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:833)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)?
at dalvik.system.NativeStart.main(Native Method)ActivityReading.class
package com.thefat.lingjunqi.enricher; import android.content.Context; import android.content.Intent; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.util.DisplayMetrics; import android.view.MenuItem; import android.view.View; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; public class ActivityReading extends FragmentActivity { private ViewPager mPager; private ArrayList<Fragment> fragmentsList; private ImageView bottomLine; private TextView textView1, textView2, textView3; private int currIndex = 0; private int bottomLineWidth; private int offset = 0; /*String [] articleTitle; String [] articleContent; String [] transTitle; String [] transContent; String wordsTitle; String [] wordsContent;*/ public static final String ITEM_ID = "item_id"; int color_current; int color_notCurrent; public static void actionStart(Context context, int id){ Intent intent=new Intent(context,ActivityReading.class); intent.putExtra(ActivityReading.ITEM_ID,id); context.startActivity(intent); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_reading); mPager = (ViewPager) findViewById(R.id.viewpager); getActionBar().setDisplayHomeAsUpEnabled(true); color_current=getResources().getColor(R.color.color_current); color_notCurrent =getResources().getColor(R.color.color_notcurrent); String [] articleTitle=getResources().getStringArray(R.array.lesson_name_list_en); String []articleContent=getResources().getStringArray(R.array.array_article); String []transTitle=getResources().getStringArray(R.array.lesson_name_list_cn); String []transContent=getResources().getStringArray(R.array.array_trans); String wordsTitle=getResources().getString(R.string.words_title); String []wordsContent=getResources().getStringArray(R.array.array_words); initImageView(); int index=getIntent().getIntExtra(ActivityReading.ITEM_ID,0); textView1 = (TextView) findViewById(R.id.text1); textView2 = (TextView) findViewById(R.id.text2); textView3 = (TextView) findViewById(R.id.text3); textView1.setOnClickListener(new MyOnClickListener(0)); textView2.setOnClickListener(new MyOnClickListener(1)); textView3.setOnClickListener(new MyOnClickListener(2)); final TextView[] titles = {textView1, textView2, textView3}; fragmentsList = new ArrayList<>(); FragmentArticleEN fragmentArticleEN = new FragmentArticleEN(); FragmentArticleCN fragmentArticleCN = new FragmentArticleCN(); FragmentWords fragmentWords = new FragmentWords(); fragmentsList.add(fragmentArticleEN); fragmentsList.add(fragmentArticleCN); fragmentsList.add(fragmentWords); MyFragmentPagerAdapter Pageradapter=new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList); mPager.setAdapter(Pageradapter); mPager.setCurrentItem(0); FragmentArticleEN fragmentArticleEN_ = (FragmentArticleEN) Pageradapter.instantiateItem(mPager,0); FragmentArticleCN fragmentArticleCN_ = (FragmentArticleCN) Pageradapter.instantiateItem(mPager,1); FragmentWords fragmentWords_= (FragmentWords) Pageradapter.instantiateItem(mPager,2); fragmentArticleEN_.refresh(articleTitle[index],articleContent[index]); fragmentArticleCN_.refresh(transTitle[index],transContent[index]); fragmentWords_.refresh(wordsTitle,wordsContent[index]); mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { int one=offset*2+bottomLineWidth;//页卡1->页卡2偏移量 @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { Animation animation=new TranslateAnimation(one*currIndex,one*position,0,0); animation.setFillAfter(true); animation.setDuration(250); bottomLine.startAnimation(animation); titles[currIndex].setTextColor(color_notCurrent); titles[position].setTextColor(color_current); currIndex=position; } @Override public void onPageScrollStateChanged(int state) { } }); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { //创建启动ActivityTitle的Intent Intent intent = new Intent(this, ActivityTitle.class); //添加额外的Flag,将Activity栈中处于FirstActivity之上的Activity弹出 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //启动intent对应的Activity startActivity(intent); return true; } return super.onOptionsItemSelected(item); } private void initImageView() { bottomLine = (ImageView)findViewById(R.id.cursor); bottomLineWidth = BitmapFactory.decodeResource(getResources(), R.drawable.cursor).getWidth(); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int screenW_Phone = dm.widthPixels; offset = (screenW_Phone/3 - bottomLineWidth)/2; //imgageview设置平移,使下划线平移到初始位置(平移一个offset) Matrix matrix = new Matrix(); matrix.postTranslate(offset, 0); bottomLine.setImageMatrix(matrix); } public class MyOnClickListener implements View.OnClickListener { private int index = 0; public MyOnClickListener(int i) { index = i; } @Override public void onClick(View v) { mPager.setCurrentItem(index); } } }
FragmentArticleEN.class
package com.thefat.lingjunqi.enricher; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * Created by lingjunqi on 2015/12/2. */ public class FragmentArticleEN extends Fragment { private View view; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view=inflater.inflate(R.layout.fragment_fragment_article_en,container,false); return view; } public void refresh(String title, String content){ View visibilityLayout=view.findViewById(R.id.visibility_layout); visibilityLayout.setVisibility(View.VISIBLE); TextView articleTitleText=(TextView)view.findViewById(R.id.article_titile); TextView articleContentText=(TextView)view.findViewById(R.id.article_content); articleTitleText.setText(title); articleContentText.setText(content); } }
如果我采用的方法错误,谁能告诉一下正确的从一个activity向另一个activity内的ViewPager里的fragment传参或者调用它的方法。
解决方案
自己调试,看看谁是空就行了,传参数,可以通过广播
解决方案二:
或者注册回调函数(监听)
解决方案三:
Activity 中
fragment.setArguments(bundle)
Fragment 中 fragment.getArguments();