java.lang.NullPointerException从View Pager中添加的Fragment获取Text值

时间:2016-05-05 07:19:22

标签: java android android-fragments

嗨,我是android片段的新手。我想从ViewPager中添加片段中的值。但我不知道如何正确地做到这一点?它给了我错误java.lang.NullPointerException,我试图看到引用,但我找不到如何以正确的方式执行此操作的示例。谢谢

private void setupViewPager(ViewPager viewPager) {
    adapter = new ViewPagerAdapter(getSupportFragmentManager());

    adapter.addFrag(new Inspection_JTM_Fragment_Inspeksi_Tab(), getString(R.string.inspection_jtm_title_tab_03));
    viewPager.setAdapter(adapter);
}

然后检索值并使用

保存
public boolean onOptionsItemSelected(MenuItem item) {
    final int id = item.getItemId();
    if (id == R.id.action_upload) {

        saveData();
        return true;

    return super.onOptionsItemSelected(item);

}

private void saveData() {

    Saran = Inspection_JTM_Fragment_Inspeksi_Tab.getSaran();
    keterangan = Inspection_JTM_Fragment_Inspeksi_Tab.getketerangan();    

    details.setSaran(Saran);
    details.setketerangan(keterangan);

    if (action_flag.equals("add")) {

        databaseHandler.create(details);

    } 

}


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



static String saran = "";
static String keterangan = "";


private static EditText editTextSaran;
private static EditText editTextKeterangan;


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


public static String getSaran() {
    return saran;
}

public static String getketerangan() {
    return keterangan;
}


@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);

    databaseHandler = new DatabaseHandler(getActivity());

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

    editTextSaran.addTextChangedListener(this);
    editTextKeterangan.addTextChangedListener(this);

    return view;
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    Inspection_JTM_Fragment_Inspeksi_Tab.this.updateValue();
}

private void updateValue() {

    saran = editTextSaran.getText().toString();
    keterangan = editTextKeterangan.getText().toString();

}

@Override
public void afterTextChanged(Editable s) {

}

}

logcat的:

FATAL EXCEPTION: main
                                                                      Process: id.co.bumisentosa.yantek, PID: 23364
                                                                      java.lang.NullPointerException
                                                                          at id.co.bumisentosa.yantek.activity.InspectionMVActivityTabs.saveData(InspectionMVActivityTabs.java:325)
                                                                          at id.co.bumisentosa.yantek.activity.InspectionMVActivityTabs.onOptionsItemSelected(InspectionMVActivityTabs.java:206)
                                                                          at android.app.Activity.onMenuItemSelected(Activity.java:2600)
                                                                          at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
                                                                          at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)
                                                                          at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
                                                                          at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
                                                                          at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:68)
                                                                          at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:172)
                                                                          at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:760)
                                                                          at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
                                                                          at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
                                                                          at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
                                                                          at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
                                                                          at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
                                                                          at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
                                                                          at android.view.View.performClick(View.java:4438)
                                                                          at android.view.View$PerformClick.run(View.java:18422)
                                                                          at android.os.Handler.handleCallback(Handler.java:733)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:136)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:515)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                          at dalvik.system.NativeStart.main(Native Method

0 个答案:

没有答案