浮动操作按钮 onClick 上的 viewPagerAdapter 片段 NullPointerException

Posted

技术标签:

【中文标题】浮动操作按钮 onClick 上的 viewPagerAdapter 片段 NullPointerException【英文标题】:viewPagerAdapter fragment NullPointerException on Floating Action Button onClick 【发布时间】:2015-06-25 16:03:14 【问题描述】:

我正在 android 中创建一个可以处理重要操作的浮动操作按钮或 FAB。在我们的应用程序中,我们有一个带有 3 个菜单项的浮动操作菜单,每个菜单项用于按名称、日期和评级对电影结果进行排序。我正在使用 java 比较器并重写 compareTo 方法来比较 Lhs 和 Rhs 电影对象并返回比较结果。

但是,当我按下其中一个菜单时,例如:按名称排序电影结果,总是出现错误 java.lang.NullPointerException

我的代码有什么问题?

以及这段代码中的 java.lang.NullPointerException :

Fragment mFragment = (Fragment) mAdapter.instantiateItem(viewPager, viewPager.getCurrentItem());

还有Logcat:

04-16 02:10:22.137    3410-3410/com.qferiz.qferiztrafficinfo E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.qferiz.qferiztrafficinfo.activities.MainActivity.onClick(MainActivity.java:229)
            at android.view.View.performClick(View.java:4336)
            at android.view.View$PerformClick.run(View.java:17853)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5469)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:857)

我已在***公共类 MainActivity 上声明:

private ViewPagerAdapter mAdapter;

我的代码有什么问题? 请帮帮我..:(

这是我在 MainActivity.java 中的代码:

package com.qferiz.qferiztrafficinfo.activities;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.oguzdev.circularfloatingactionmenu.library.FloatingActionButton;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;
import com.oguzdev.circularfloatingactionmenu.library.SubActionButton;
import com.qferiz.qferiztrafficinfo.extras.SortListener;
import com.qferiz.qferiztrafficinfo.fragments.FragmentBoxOffice;
import com.qferiz.qferiztrafficinfo.fragments.FragmentSearch;
import com.qferiz.qferiztrafficinfo.fragments.FragmentUpcoming;
import com.qferiz.qferiztrafficinfo.fragments.MyFragment;
import com.qferiz.qferiztrafficinfo.fragments.NavigationDrawerFragment;
import com.qferiz.qferiztrafficinfo.R;
import com.qferiz.qferiztrafficinfo.logging.L;

import it.neokree.materialtabs.MaterialTab;
import it.neokree.materialtabs.MaterialTabHost;
import it.neokree.materialtabs.MaterialTabListener;


public class MainActivity extends ActionBarActivity implements MaterialTabListener, View.OnClickListener 

    private Toolbar toolbar;
    private ViewPager viewPager;
    private MaterialTabHost tabHost;
    public static final int MOVIES_SEARCH_RESULT = 0;
    public static final int MOVIES_HITS = 1;
    public static final int MOVIES_UPCOMING = 2;
    //tag associated with the FAB menu button that sorts by name
    private static final String TAG_SORT_NAME = "sortName";
    //tag associated with the FAB menu button that sorts by date
    private static final String TAG_SORT_DATE = "sortDate";
    //tag associated with the FAB menu button that sorts by ratings
    private static final String TAG_SORT_RATINGS = "sortRatings";
    private ViewPagerAdapter mAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) 

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

        tabHost = (MaterialTabHost) findViewById(R.id.materialTabHost);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        //mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() 
            @Override
            public void onPageSelected(int position) 
                super.onPageSelected(position);
                tabHost.setSelectedNavigationItem(position);
            
        );

        for (int i = 0; i < adapter.getCount(); i++) 
            // TAB TEXT
            //tabHost.addTab(tabHost.newTab().setText(adapter.getPageTitle(i)).setTabListener(this));

            // TAB ICON
            tabHost.addTab(tabHost.newTab().setIcon(adapter.getIcon(i)).setTabListener(this));
        

        // CircularFloatingActionMenu Button
        setupFAB();

    

    private void setupFAB() 
        // CircularFloatingActionMenu Button
        //define the icon for the main floating action button
        ImageView iconActionButton = new ImageView(this);
        iconActionButton.setImageResource(R.drawable.ic_action_new);


        // Create a button to attach the menu:
        //set the appropriate background for the main floating action button along with its icon
        FloatingActionButton actionButton = new FloatingActionButton.Builder(this)
                .setContentView(iconActionButton)
                .setBackgroundDrawable(R.drawable.selector_button_red)
                .build();

        // Create menu items:
        //define the icons for the sub action buttons
        ImageView iconSortName = new ImageView(this);
        iconSortName.setImageResource(R.drawable.ic_notifications_white_36dp);
        ImageView iconSortDate = new ImageView(this);
        iconSortDate.setImageResource(R.drawable.ic_map_white_36dp);
        ImageView iconSortRatings = new ImageView(this);
        iconSortRatings.setImageResource(R.drawable.ic_directions_white_36dp);

        //set the background for all the sub buttons
        SubActionButton.Builder itemBuilder = new SubActionButton.Builder(this);
        itemBuilder.setBackgroundDrawable(getResources().getDrawable(R.drawable.selector_sub_button_gray));

        //build the sub buttons
        SubActionButton buttonSortName = itemBuilder.setContentView(iconSortName).build();
        SubActionButton buttonSortDate = itemBuilder.setContentView(iconSortDate).build();
        SubActionButton buttonSortRatings = itemBuilder.setContentView(iconSortRatings).build();

        //to determine which button was clicked, set Tags on each button
        buttonSortName.setTag(TAG_SORT_NAME);
        buttonSortDate.setTag(TAG_SORT_DATE);
        buttonSortRatings.setTag(TAG_SORT_RATINGS);

        buttonSortName.setOnClickListener(this);
        buttonSortDate.setOnClickListener(this);
        buttonSortRatings.setOnClickListener(this);

        //add the sub buttons to the main floating action button
        FloatingActionMenu actionMenu = new FloatingActionMenu.Builder(this)
                .addSubActionView(buttonSortName)
                .addSubActionView(buttonSortDate)
                .addSubActionView(buttonSortRatings)
                .attachTo(actionButton)
                .build();
    


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) 
            Toast.makeText(this, "Anda telah meng-Klik " + item.getTitle(), Toast.LENGTH_SHORT).show();
            return true;
        

        if (id == R.id.navigate) 
            startActivity(new Intent(this, SubActivity.class));
        

        if (id == R.id.tabWithLibrary) 
            startActivity(new Intent(this, ActivityUsingTabLibrary.class));
        

        if (id == R.id.vectorTestActivity) 
            startActivity(new Intent(this, VectorTestActivity.class));
        

        return super.onOptionsItemSelected(item);
    

    @Override
    public void onTabSelected(MaterialTab materialTab) 
        viewPager.setCurrentItem(materialTab.getPosition());
    

    @Override
    public void onTabReselected(MaterialTab materialTab) 

    

    @Override
    public void onTabUnselected(MaterialTab materialTab) 

    

    @Override
    public void onClick(View view) 
        Fragment mFragment = (Fragment) mAdapter.instantiateItem(viewPager, viewPager.getCurrentItem());

        if (mFragment != null)

            if (mFragment instanceof SortListener)

                if (view.getTag().equals(TAG_SORT_NAME))
                    //L.t(this,"Submenu SortName Clicked");
                    ((SortListener) mFragment).onSortByName();
                

                if (view.getTag().equals(TAG_SORT_DATE))
                    //L.t(this,"Submenu SortDate Clicked");
                    ((SortListener) mFragment).onSortByDate();
                

                if (view.getTag().equals(TAG_SORT_RATINGS))
                    //L.t(this,"Submenu SortRatings Clicked");
                    ((SortListener) mFragment).onSortByRating();
                
            
        
        else 
            L.t(this, "Error Fragment Null");
        


    

    private class ViewPagerAdapter extends FragmentStatePagerAdapter 

        int icons[] = R.drawable.ic_notifications_white_36dp,
                R.drawable.ic_map_white_36dp,
                R.drawable.ic_directions_white_36dp;

        FragmentManager fragmentManager;

        public ViewPagerAdapter(FragmentManager fm) 
            super(fm);
            fragmentManager = fm;
        

        @Override
        public Fragment getItem(int num) 
            //return MyFragment.getInstance(num);
            Fragment fragment = null;

            switch (num)
                case MOVIES_SEARCH_RESULT:
                    fragment = FragmentSearch.newInstance("", "");
                    break;
                case MOVIES_HITS:
                    fragment = FragmentBoxOffice.newInstance("", "");
                    break;
                case MOVIES_UPCOMING:
                    fragment = FragmentUpcoming.newInstance("", "");
                    break;
            
            return fragment;
        

        @Override
        public int getCount() 
            return 3; // Jumlah TAB
        

        @Override
        public CharSequence getPageTitle(int position) 
            return getResources().getStringArray(R.array.tabs)[position];
        

        private Drawable getIcon(int position) 
            return getResources().getDrawable(icons[position]);
        
    


代码 MovieSorter.java :

package com.qferiz.qferiztrafficinfo.extras;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created by Qferiz on 15/04/2015.
 */
public class MovieSorter 
    public void sortMoviesByName(ArrayList<Movie> movies)
        Collections.sort(movies, new Comparator<Movie>() 
            @Override
            public int compare(Movie lhs, Movie rhs) 
                return lhs.getTitle().compareTo(rhs.getTitle());
            
        );
    

    public void sortMoviesByDate(ArrayList<Movie> movies)
        Collections.sort(movies, new Comparator<Movie>() 
            @Override
            public int compare(Movie lhs, Movie rhs) 
                return lhs.getReleaseDateTheater().compareTo(rhs.getReleaseDateTheater());
            
        );
    

    public void sortMoviesByRating(ArrayList<Movie> movies)
        Collections.sort(movies, new Comparator<Movie>() 
            @Override
            public int compare(Movie lhs, Movie rhs) 
                //return lhs.getReleaseDateTheater().compareTo(rhs.getReleaseDateTheater());
                int ratingLhs = lhs.getAudienceScore();
                int ratingRhs = rhs.getAudienceScore();
                if (ratingLhs < ratingRhs)
                    return 1;
                
                else if (ratingLhs > ratingRhs)
                    return -1;
                
                else 
                    return 0;
                
            
        );
    



FragmentBoxOffice.java的部分代码、方法:

@Override
    public void onSortByName() 
        //L.t(getActivity(),"Sort name Box Office");
        movieSorter.sortMoviesByName(listMovies);
        adapterBoxOffice.notifyDataSetChanged();

    

    @Override
    public void onSortByDate() 
        //L.t(getActivity(),"Sort Date Box Office");
        movieSorter.sortMoviesByDate(listMovies);
        adapterBoxOffice.notifyDataSetChanged();

    

    @Override
    public void onSortByRating() 
        //L.t(getActivity(),"Sort Rating Box Office");
        movieSorter.sortMoviesByRating(listMovies);
        adapterBoxOffice.notifyDataSetChanged();

    

这是 FragmentBoxOffice.java 中的完整代码:

package com.qferiz.qferiztrafficinfo.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkError;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.qferiz.qferiztrafficinfo.R;
import com.qferiz.qferiztrafficinfo.adapters.AdapterBoxOffice;
import com.qferiz.qferiztrafficinfo.extras.Constants;
import com.qferiz.qferiztrafficinfo.extras.Movie;
import com.qferiz.qferiztrafficinfo.extras.MovieSorter;
import com.qferiz.qferiztrafficinfo.extras.MyApplication;
import com.qferiz.qferiztrafficinfo.extras.SortListener;
import com.qferiz.qferiztrafficinfo.logging.L;
import com.qferiz.qferiztrafficinfo.network.VolleySingleton;
import static com.qferiz.qferiztrafficinfo.extras.UrlEndpoints.*;
import static com.qferiz.qferiztrafficinfo.extras.Keys.EndpointBoxOffice.*;
import static com.qferiz.qferiztrafficinfo.extras.Constants.*;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * A simple @link Fragment subclass.
 * Use the @link FragmentBoxOffice#newInstance factory method to
 * create an instance of this fragment.
 */
public class FragmentBoxOffice extends Fragment implements SortListener 
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    public static final String URL_ROTTEN_TOMATOES_BOX_OFFICE =
            "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private RequestQueue requestQueue;
    private ArrayList<Movie> listMovies = new ArrayList<>();
    private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private RecyclerView listMovieHits;
    private AdapterBoxOffice adapterBoxOffice;
    private TextView textVolleyError;
    private MovieSorter movieSorter;


    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FragmentBoxOffice.
     */
    // TODO: Rename and change types and number of parameters
    public static FragmentBoxOffice newInstance(String param1, String param2) 
        FragmentBoxOffice fragment = new FragmentBoxOffice();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    

    public static String getRequestUrl(int limit)
        //return URL_ROTTEN_TOMATOES_BOX_OFFICE+"?apikey="+ MyApplication.API_KEY_ROTTEN_TOMATOES+"&limit="+limit;
        return URL_BOX_OFFICE
                + URL_CHAR_QUESTION
                + URL_PARAM_API_KEY
                + MyApplication.API_KEY_ROTTEN_TOMATOES
                + URL_CHAR_AMEPERSAND
                + URL_PARAM_LIMIT + limit;

    

    public FragmentBoxOffice() 
        // Required empty public constructor
    

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        if (getArguments() != null) 
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        

        volleySingleton = VolleySingleton.getInstance();
        requestQueue = volleySingleton.getRequestQueue();
        sendJsonRequest();

    

    private void sendJsonRequest() 
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
                getRequestUrl(30),
                (String)null,
                new Response.Listener<JSONObject>() 
                    @Override
                    public void onResponse(JSONObject response) 
                        //L.t(getActivity(), response.toString());
                        textVolleyError.setVisibility(View.GONE);
                        listMovies = parseJSONResponse(response);
                        adapterBoxOffice.setMovieList(listMovies);

                    
                , new Response.ErrorListener() 
            @Override
            public void onErrorResponse(VolleyError error) 
               //L.t(getActivity(), error.getMessage() + "");
                handleVolleyError(error);

            
        );

        requestQueue.add(request);
    

    private void handleVolleyError(VolleyError error) 
        textVolleyError.setVisibility(View.VISIBLE);
        if (error instanceof TimeoutError || error instanceof NoConnectionError)
            textVolleyError.setText(R.string.error_timeout);

         else if (error instanceof AuthFailureError)
            //TODO:
            textVolleyError.setText(R.string.error_auth_failure);

         else if (error instanceof ServerError)
            //TODO:
            textVolleyError.setText(R.string.error_auth_failure);

         else if (error instanceof NetworkError)
            //TODO:
            textVolleyError.setText(R.string.error_network);

         else if (error instanceof ParseError)
            //TODO:
            textVolleyError.setText(R.string.error_parser);

        
    

    private ArrayList<Movie> parseJSONResponse(JSONObject response) 
        ArrayList<Movie> listMovies = new ArrayList<>();
        if (response != null && response.length() > 0)

            try 

                JSONArray arrayMovies = response.getJSONArray(KEY_MOVIES);
                for (int i = 0; i < arrayMovies.length(); i++)
                    long id = -1;
                    String title = Constants.NA;
                    String releaseDates = Constants.NA;
                    int audienceScore = -1;
                    String synopsis = Constants.NA;
                    String urlThumbnail = Constants.NA;

                    JSONObject currentMovies = arrayMovies.getJSONObject(i);

                    // get the id of the current movie
                    if (currentMovies.has(KEY_ID) && !currentMovies.isNull(KEY_ID))
                        id = currentMovies.getLong(KEY_ID);
                    


                    // get the title of the current movie
                    if (currentMovies.has(KEY_TITLE) && !currentMovies.isNull(KEY_TITLE)) 
                        title = currentMovies.getString(KEY_TITLE);
                    

                    // get the release date of the current movie
                    if (currentMovies.has(KEY_RELEASE_DATES) && !currentMovies.isNull(KEY_RELEASE_DATES)) 
                        JSONObject objectReleaseDates = currentMovies.getJSONObject(KEY_RELEASE_DATES);

                        if (objectReleaseDates !=null && objectReleaseDates.has(KEY_THEATER)
                                && !objectReleaseDates.isNull(KEY_THEATER)) 
                            releaseDates = objectReleaseDates.getString(KEY_THEATER);
                        
                    

                    // get the audience score for the current movie
                    JSONObject objectRatings = currentMovies.getJSONObject(KEY_RATINGS);

                    if (objectRatings.has(KEY_AUDIENCE_SCORE) && !currentMovies.isNull(KEY_RATINGS))
                        if (objectRatings != null && objectRatings.has(KEY_AUDIENCE_SCORE)
                                && !objectRatings.isNull(KEY_AUDIENCE_SCORE))
                            audienceScore = objectRatings.getInt(KEY_AUDIENCE_SCORE);
                        
                    

                    // get the synopsis for the current movie
                    if (currentMovies.has(KEY_SYNOPSIS) && !currentMovies.isNull(KEY_SYNOPSIS)) 
                        synopsis = currentMovies.getString(KEY_SYNOPSIS);
                    

                    // get the posters & thumbnail for the current movie
                    if (currentMovies.has(KEY_POSTERS) && !currentMovies.isNull(KEY_POSTERS)) 
                        JSONObject objectPosters = currentMovies.getJSONObject(KEY_POSTERS);
                        if (objectPosters != null && objectPosters.has(KEY_THUMBNAIL)
                                && !objectPosters.isNull(KEY_THUMBNAIL)) 
                            urlThumbnail = objectPosters.getString(KEY_THUMBNAIL);
                        
                    

                    Movie movie = new Movie();
                    movie.setId(id);
                    movie.setTitle(title);
                    Date date = null;

                    try 
                          date = dateFormat.parse(releaseDates);
                    catch (ParseException e)

                    

                    movie.setReleaseDateTheater(date);
                    movie.setAudienceScore(audienceScore);
                    movie.setSynopsis(synopsis);
                    movie.setUrlThumbnail(urlThumbnail);

                    if (id != -1 && !title.equals(Constants.NA))
                        listMovies.add(movie);
                    
                

                //L.T(getActivity(), listMovies.toString());

             catch (JSONException e)

            

            //L.t(getActivity(), listMovies.size() + " rows fetched");
        

        L.t(getActivity(), listMovies.size() + " rows fetched");
        return listMovies;

    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_box_office, container, false);
        textVolleyError = (TextView) view.findViewById(R.id.textVolleyError);
        listMovieHits = (RecyclerView) view.findViewById(R.id.listMoviesHits);
        listMovieHits.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapterBoxOffice = new AdapterBoxOffice(getActivity());
        listMovieHits.setAdapter(adapterBoxOffice);
        sendJsonRequest();

        return view;

        //return inflater.inflate(R.layout.fragment_box_office, container, false);
    


    @Override
    public void onSortByName() 
        //L.t(getActivity(),"Sort name Box Office");
        movieSorter.sortMoviesByName(listMovies);
        adapterBoxOffice.notifyDataSetChanged();

    

    @Override
    public void onSortByDate() 
        //L.t(getActivity(),"Sort Date Box Office");
        movieSorter.sortMoviesByDate(listMovies);
        adapterBoxOffice.notifyDataSetChanged();

    

    @Override
    public void onSortByRating() 
        //L.t(getActivity(),"Sort Rating Box Office");
        movieSorter.sortMoviesByRating(listMovies);
        adapterBoxOffice.notifyDataSetChanged();

    

提前谢谢..

【问题讨论】:

【参考方案1】:

在 MainActivity.java 第 229 行更改:

Fragment mFragment = (Fragment) mAdapter.instantiateItem(viewPager, viewPager.getCurrentItem());

收件人:

Fragment mFragment = (Fragment) adapter.instantiateItem(viewPager, viewPager.getCurrentItem());

它应该可以正常工作。

【讨论】:

要改进您的回答,请使用代码格式***.com/help/formatting,并且不要使用“Hello”或“Regards”之类的问候语。尽量简短但内容丰富。 谢谢你的代码它工作正常。我忘记在 TabHost 上将适配器的变量名称更改为 mAdapter: // 将所有选项卡添加到 TabHost for (int i = 0; i

以上是关于浮动操作按钮 onClick 上的 viewPagerAdapter 片段 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

浮动操作按钮未显示 - viewpager

如何为可移动的浮动按钮设置 onTouch 和 onClick 功能

浮动操作按钮上的淡入淡出动画

在 iOS 中创建浮动操作按钮

关闭浮动操作按钮 Android

ViewPager 上的菜单按钮敬酒位置错误