如何在“活动”中从“片段”中调用Toast?当我尝试时,我有这个error: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
下面是我的整个Activity类,片段和Logcat。
活动
public class MainActivity extends AppCompatActivity {
private TabsAdapter tabsAdapter;
private TabLayout tabLayout;
private ViewPager viewPager;
private int [] tabicons = {
R.drawable.money,
R.drawable.gold
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager= findViewById(R.id.pager);
tabLayout= findViewById(R.id.tab_layout);
tabsAdapter= new TabsAdapter(getSupportFragmentManager());
tabsAdapter.addFragment(new Fragment1(),"Kursy Walut");
tabsAdapter.addFragment(new Fragment2(),"Kursy Złota");
// tabsAdapter.addFragment(new Fragment3(),"Tab 3");
viewPager.setAdapter(tabsAdapter);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
Fragment2 fragment2= new Fragment2();
fragment2.MetodaFragmentu();
}
private void setupTabIcons() {
if (tabLayout!=null){
if (tabLayout.getTabAt(0)!=null)
tabLayout.getTabAt(0).setIcon(tabicons[0]);
if (tabLayout.getTabAt(1)!=null)
tabLayout.getTabAt(1).setIcon(tabicons[1]);
if (tabLayout.getTabAt(2)!=null)
tabLayout.getTabAt(2).setIcon(tabicons[2]);
}
}
}
片段
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.layout_fragment_2, container,false);
}
public void MetodaFragmentu(){
Toast.makeText(getActivity(),"Ok",Toast.LENGTH_LONG).show();
}
}
Logcat
2019-05-20 12:05:52.040 10566-10566 / com.example.goldandcurrency E / AndroidRuntime:FATAL EXCEPTION:main 流程:com.example.goldandcurrency,PID:10566 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.goldandcurrency / com.example.goldandcurrency.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'java.lang.String android.content.Context。空对象引用上的getPackageName()' 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6119) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String android.content.Context.getPackageName()' 在android.widget.Toast(Toast.java:103) 在android.widget.Toast.makeText(Toast.java:256) 在com.example.goldandcurrency.Fragment2.MetodaFragmentu(Fragment2.java:29) 在com.example.goldandcurrency.MainActivity.onCreate(MainActivity.java:40) 在android.app.Activity.performCreate(Activity.java:6679) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6119) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)