问题
I have 3 fragments used in bottom navigation in Main Activity.
In one of the fragments, I have screenslidepagefragment
in viewpager2
which has 3 buttons. When I click on any of these 3 buttons I have this error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.headlines, PID: 3209
java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.lifecycle.LiveData com.example.headlines.Model.NewsViewModel.getData(java.lang.String)' on a null object reference
at com.example.headlines.Activity.HomeFragment.retrieveData(HomeFragment.java:133)
at com.example.headlines.MainActivity.getQuery(MainActivity.java:35)
at com.example.headlines.slideFragment.ScreenSlidePageFragment$1.onClick(ScreenSlidePageFragment.java:75)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
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:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Main Activity
public class MainActivity extends AppCompatActivity implements ScreenSlidePageFragment.send {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView=findViewById(R.id.navigation_view);
bottomNavigationView.setOnNavigationItemSelectedListener(listener);
getSupportFragmentManager().beginTransaction().replace(R.id.layout,new HomeFragment()).commit();
}
public void getQuery(String query)
{
HomeFragment fragment=new HomeFragment();
fragment.retrieveData(query);
}
private BottomNavigationView.OnNavigationItemSelectedListener listener=
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment=null;
switch (item.getItemId())
{
case R.id.nav_home:
fragment=new HomeFragment();
break;
case R.id.nav_search:
fragment=new Search();
break;
case R.id.nav_favorites:
fragment=new Favorite();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.layout,fragment).commit();
return true;
}
};
}
HomeFragment
public class HomeFragment extends Fragment {
private RecyclerView recyclerView;
LinearLayoutManager manager;
private Button button;
private EditText editText;
private ArrayList<Articles> articleArrayList=new ArrayList<>();
private ViewPager2 viewPager;
private FragmentStateAdapter pagerAdapter;
protected FragmentActivity mActivity;
FragmentActivity activity;
NewsViewModel newsViewModel;
public HomeFragment(){}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.home_layout,container,false);
viewPager=view.findViewById(R.id.pager);
recyclerView=view.findViewById(R.id.recyclerView);
editText=view.findViewById(R.id.edit_query);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
newsViewModel=new ViewModelProvider(HomeFragment.this).get(NewsViewModel.class);
pagerAdapter=new ScreenSlidePagerActivity.ScreenSlidePagerAdapter(getActivity());
viewPager.setAdapter(pagerAdapter);
manager=new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(manager);
retrieveData("general");
}
public void retrieveData(String query)
{
newsViewModel.getData(query).observe(mActivity, new Observer<Headlines>() {
@Override
public void onChanged(Headlines headlines) {
List<Articles> articles = headlines.getArticles();
articleArrayList.clear();
articleArrayList.addAll(articles);
final Adapter adapter = new Adapter(getActivity(), articleArrayList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
if (context instanceof Activity){
this.mActivity = (FragmentActivity) context;
}
}
@Override
public void onDetach() {
super.onDetach();
this.mActivity=null;
}
}
in ScreenslidePageFragment i have 3 buttons for different news categories..so i want that whenever any button clicked,same category news appear in home fragment.
public class ScreenSlidePageFragment extends Fragment {
private static Button button_tech,button_health,button_sports,button_temp;
private send send_listener;
public interface send {
void getQuery(String query);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup v1 = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page1, container, false);
button_tech = v1.findViewById(R.id.button_tech);
button_sports = v1.findViewById(R.id.button_sports);
button_health = v1.findViewById(R.id.button_health);
button_tech.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button_health.setPressed(false);
button_health.setSelected(false);
button_sports.setPressed(false);
button_sports.setSelected(false);
button_tech.setSelected(true);
button_tech.setPressed(true);
button_temp=(Button)button_tech;
send_listener.getQuery("technology");
}
});
button_health.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button_sports.setPressed(false);
button_sports.setSelected(false);
button_tech.setSelected(false);
button_tech.setPressed(false);
button_health.setPressed(true);
button_health.setSelected(true);
button_temp=(Button)button_health;
send_listener.getQuery("health");
}
});
button_sports.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button_tech.setSelected(false);
button_tech.setPressed(false);
button_health.setPressed(false);
button_health.setSelected(false);
button_sports.setPressed(true);
button_sports.setSelected(true);
button_temp=(Button)button_sports;
send_listener.getQuery("sports");
}
});
return v1;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
send_listener=(send)context;
}
@Override
public void onDetach() {
super.onDetach();
send_listener=null;
}
}
and my NewsViewModel
public class NewsViewModel extends AndroidViewModel {
private NewsRepository newsRepository;
private LiveData<Headlines> data,data2;
String API_KEY = "MY_API_KEY";
String country="in";
String category="general";
int pageSize=100;
public NewsViewModel(@NonNull Application application) {
super(application);
newsRepository=new NewsRepository();
}
public LiveData<Headlines> getData(String query)
{
data=newsRepository.getArticles(country,query,pageSize,API_KEY);
return data;
}
public LiveData<Headlines> getData2(String query)
{
data=newsRepository.getArticles2(query,API_KEY);
return data;
}
}
I have tried many things.none worked.
Please suggest a way to solve this error. I will be very thankful to you.
来源:https://stackoverflow.com/questions/62970958/viewpager2-in-fragment-of-bottomnavigation-nullpointerexception