NullPointerException将Frament中的值editText传递给MainActivity

时间:2016-02-21 01:51:37

标签: java android android-fragments

大家好请帮助我,我是片段新手,我在viewPager中有一个editText实现Fragment,我想要的是将String值传递给MainActivity,使用onOptionsItemSelected保存到数据库中,如何以正确的方式做到这一点,我需要使用界面Communicator吗?,谢谢

  

E / AndroidRuntime:致命异常:主要                                                                             流程:id.co.bumisentosa.yantek,PID:26979                                                                              显示java.lang.NullPointerException                                                                                 在   id.co.bumisentosa.yantek.activity.InspectionMVActivityTabs.onOptionsItemSelected(InspectionMVActivityTabs.java:97)                                                                                 在android.app.Activity.onMenuItemSelected(Activity.java:2804)                                                                                 在

public class Inspection_JTM_Fragment_Inspeksi_Tab extends Fragment implements LocationListener, View.OnClickListener {

private static View view;

static String saran = "";
private static EditText editTextSaran;


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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getActivity();
}

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

    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.inspection_jtm_fragment_tab_inspeksi, container, false);

    editTextSaran = (EditText) view.findViewById(R.id.et_saran_saran_jtm_inspeksi);

    editTextSaran.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode,KeyEvent event){
            if((event.getAction()==KeyEvent.ACTION_DOWN)&&
                    (keyCode==KeyEvent.KEYCODE_ENTER)){
                saran = editTextSaran.getText().toString();
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editTextSaran.getWindowToken(),0);

                Toast.makeText(getActivity().getBaseContext(), saran, Toast.LENGTH_SHORT).show();
                return true;
            }
            return false;
        }
    });   
    }
    return view;
}

public static String getSaran() {
    return saran;
    }
}

这是我的主要活动

public class InspectionMVActivityTabs extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int unit;
private int jaringan;
private String date;
private int tipe_inspeksi;
private String newItemDatabaseID = null;
private DatabaseHandler databaseHandler;
private String test ="";
private Inspection_JTM_Fragment_Inspeksi_Tab Tab;

private Fragment TabInspeksi;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    //getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);


    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.general, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    final int id = item.getItemId();
    if (id == R.id.action_upload) {
        // Upload data ke server
        test = Inspection_JTM_Fragment_Inspeksi_Tab.getSaran();

        databaseHandler.saveTest(new ItemsDetails(
                test));


        // Showing selected spinner item


        Toast.makeText(getApplicationContext(), "Save Activity Clicked ",
                Toast.LENGTH_LONG).show();
    }
    return super.onOptionsItemSelected(item);

}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new Inspection_JTM_Fragment_Awal_Tab(), getString(R.string.inspection_jtm_title_tab_01));
    adapter.addFrag(new Inspection_JTM_Fragment_Foto_Tab(), getString(R.string.inspection_jtm_title_tab_02));
    adapter.addFrag(new Inspection_JTM_Fragment_Inspeksi_Tab(), getString(R.string.inspection_jtm_title_tab_03));
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
        }
    }
}

0 个答案:

没有答案