如何通过单击按钮

时间:2018-03-10 04:00:46

标签: java android android-fragments

我正在尝试用点击按钮将当前片段替换为视图寻呼机中的新片段。

我尝试过各种方法,但却出现以下错误:

  

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)'                                                                                at in.tvac.akshaye.lapitchat.PersonalDetailsFragment $ 1.onClick(PersonalDetailsFragment.java:92)

任何有上述错误解决方案的人都可以通过点击按钮来调用片段吗?

这是当前的片段代码

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;


public class PersonalDetailsFragment extends Fragment {

    private TextInputLayout mFirstName;
    private TextInputLayout mInitials;
    private TextInputLayout mLastName;
    private Button mContinueBtn;
    private RadioButton mFemale;
    private RadioButton mMale;

    private DatabaseReference mDatabase;

    //ProgressDialog
    private ProgressDialog mRegProgress;

    private ViewPager mViewPager;
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private TabLayout mTabLayout;

    //Firebase Auth
    private FirebaseAuth mAuth;

    View view;

    boolean male = true;

    // private RadioGroup radioGroup;

    public PersonalDetailsFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_personal_details, container, false);

        mRegProgress = new ProgressDialog(this.getActivity().getApplicationContext());

        // Firebase Auth
        mAuth = FirebaseAuth.getInstance();

        mContinueBtn = (Button) view.findViewById(R.id.btnContinue);

        mViewPager = (ViewPager) view.findViewById(R.id.main_tabPager);

        mFirstName = (TextInputLayout) view.findViewById(R.id.inpuutCellNumber);
        mInitials = (TextInputLayout) view.findViewById(R.id.inputHomeNumber);
        mLastName = (TextInputLayout) view.findViewById(R.id.inputWorkNumber);

        mContinueBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                String first_Name = mFirstName.getEditText().getText().toString();
                String initials = mInitials.getEditText().getText().toString();
                String lastName = mLastName.getEditText().getText().toString();


                if ( TextUtils.isEmpty( first_Name ) || TextUtils.isEmpty( initials ) || TextUtils.isEmpty( lastName ) )
                {
                    Toast.makeText( getActivity().getApplicationContext(), "Make sure all fields are filled in.", Toast.LENGTH_LONG ).show();
                }
                //else {
                    //Tabs

                    mViewPager = (ViewPager) view.findViewById(R.id.main_tabPager);
                    mSectionsPagerAdapter = new SectionsPagerAdapter(new PersonalInforamtionFragment().getFragmentManager());

                    mViewPager.setAdapter(mSectionsPagerAdapter);

                    mTabLayout = (TabLayout) view.findViewById(R.id.main_tabs);
                    mTabLayout.setupWithViewPager(mViewPager);
                // }

            }
        });

        final RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.gender);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radioMale:
                        male = true;
                        break;
                    case R.id.radioFemale:
                        male = false;
                        break;
                }
            }
        });

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // save views as variables in this method
        // "view" is the one returned from onCreateView
        // Android Fields

    }

}

这是BankRegisterActivity

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ServerValue;

public class BankRegisterActivity extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private Toolbar mToolbar;

    private ViewPager mViewPager;
    private SectionsPagerAdapter mSectionsPagerAdapter;

    private DatabaseReference mUserRef;

    private TabLayout mTabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_register);

        mAuth = FirebaseAuth.getInstance();

        mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle("ABSA Banking");

        if (mAuth.getCurrentUser() != null) {


            mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());

        }


        //Tabs
        mViewPager = (ViewPager) findViewById(R.id.main_tabPager);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager.setAdapter(mSectionsPagerAdapter);

        mTabLayout = (TabLayout) findViewById(R.id.main_tabs);
        mTabLayout.setupWithViewPager(mViewPager);

    }

    @Override
    public void onStart() {
        super.onStart();
        // Check if user is signed in (non-null) and update UI accordingly.
        FirebaseUser currentUser = mAuth.getCurrentUser();

        if(currentUser == null){

            sendToStart();

        } else {

            mUserRef.child("online").setValue("true");

        }

    }

    @Override
    protected void onStop() {
        super.onStop();

        FirebaseUser currentUser = mAuth.getCurrentUser();

        if(currentUser != null) {

            mUserRef.child("online").setValue(ServerValue.TIMESTAMP);

        }

    }

    private void sendToStart() {

        Intent startIntent = new Intent(BankRegisterActivity.this, StartActivity.class);
        startActivity(startIntent);
        finish();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        getMenuInflater().inflate(R.menu.main_menu, menu);


        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        if(item.getItemId() == R.id.main_logout_btn){

            mUserRef.child("online").setValue(ServerValue.TIMESTAMP);

            FirebaseAuth.getInstance().signOut();
            sendToStart();

        }

        if(item.getItemId() == R.id.main_settings_btn){


        }

        if(item.getItemId() == R.id.main_all_btn){


        }

        return true;
    }
}

这是SectionsPageAdapter代码:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.widget.Toolbar;

class SectionsPagerAdapter extends FragmentPagerAdapter{

    private Toolbar mToolbar;

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {


        switch(position) {
            case 0:
                PersonalDetailsFragment personalDetailsFragment = new PersonalDetailsFragment ();
                return personalDetailsFragment;

            case 1:
                ContactDeatilsFragment contactDetailsFragment = new ContactDeatilsFragment ();
                return  contactDetailsFragment;

            case 2:
                PersonalInforamtionFragment personalInforamtionFragment = new PersonalInforamtionFragment ();
                return personalInforamtionFragment;
            case 3:
                AccountInformationFragment accInformationFragment = new AccountInformationFragment();
                return accInformationFragment;
            case 4:
                TCsFragment TCsFragment = new TCsFragment();
                return TCsFragment;

            default:
                return  null;
        }

    }

    @Override
    public int getCount() {
        return 5;
    }

    public CharSequence getPageTitle(int position){

        switch (position) {
            case 0:
                return "1";

            case 1:
                return "2";

            case 2:
                return "3";

            case 3:
                return "4";

            case 4:
                return "5";

            default:
                return null;
        }

    }

}

这是BankRegisterActivity的布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="in.tvac.akshaye.lapitchat.BankRegisterActivity">


        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/appBarLayout">

            <include layout="@layout/app_bar_layout" android:id="@+id/main_page_toolbar" />

            <android.support.design.widget.TabLayout
                android:id="@+id/main_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"></android.support.design.widget.TabLayout>

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/main_tabPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/appBarLayout">

        </android.support.v4.view.ViewPager>



</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

试试这个片段:

 public class PersonalDetailsFragment extends Fragment implements View.OnClickListener {

        private TextInputLayout mFirstName;
        private TextInputLayout mInitials;
        private TextInputLayout mLastName;
        private Button mContinueBtn;
        private RadioButton mFemale;
        private RadioButton mMale;

        private DatabaseReference mDatabase;

        //ProgressDialog
        private ProgressDialog mRegProgress;

        private ViewPager mViewPager;
        private SectionsPagerAdapter mSectionsPagerAdapter;
        private TabLayout mTabLayout;

        //Firebase Auth
        private FirebaseAuth mAuth;

        View v;

        boolean male = true;

        // private RadioGroup radioGroup;

        public PersonalDetailsFragment() {
            // Required empty public constructor
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            v = inflater.inflate(R.layout.fragment_personal_details, container, false);

            mRegProgress = new ProgressDialog(this.getActivity().getApplicationContext());

            // Firebase Auth
            mAuth = FirebaseAuth.getInstance();

            mContinueBtn = (Button) v.findViewById(R.id.btnContinue);
            mContinueBtn.setOnClickListener(this);

            mViewPager = (ViewPager) getActivity().findViewById(R.id.main_tabPager);

            mFirstName = (TextInputLayout) v.findViewById(R.id.inpuutCellNumber);
            mInitials = (TextInputLayout) v.findViewById(R.id.inputHomeNumber);
            mLastName = (TextInputLayout) v.findViewById(R.id.inputWorkNumber);


            final RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.gender);
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    switch (checkedId) {
                        case R.id.radioMale:
                            male = true;
                            break;
                        case R.id.radioFemale:
                            male = false;
                            break;
                    }
                }
            });

            return v;
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            // save views as variables in this method
            // "view" is the one returned from onCreateView
            // Android Fields

        }

        @Override
        public void onClick(View v) {
        if (v == mContinueBtn) {

                    String first_Name = mFirstName.getEditText().getText().toString();
                    String initials = mInitials.getEditText().getText().toString();
                    String lastName = mLastName.getEditText().getText().toString();


                    if ( TextUtils.isEmpty( first_Name ) || TextUtils.isEmpty( initials ) || TextUtils.isEmpty( lastName ) )
                    {
                        Toast.makeText( getActivity().getApplicationContext(), "Make sure all fields are filled in.", Toast.LENGTH_LONG ).show();
                    } else {
                        mViewPager.setCurrentItem(1);
                    }


              }
         }

    }