尝试从Fragment获取EditText字段的值时出现空指针异常

时间:2014-04-14 08:11:30

标签: android android-fragments

我正在尝试构建一个小项目,该项目使用带有两个不同片段的标签,具体取决于选择了哪个标签。这两个片段都包含EditText字段,当我单击按钮时,我想从中保存值。问题是当我单击按钮时,我得到一个空指针异常。 Logcat在尝试获取片段的onClick(View v)方法中的EditText字段的值时抛出异常 与encrypt.setSaveEntry(getActivity(), "uname", edit_username.getText().toString())

我非常感谢提前对这个问题的贡献

这是我的fragment_registry.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
android:id="@+id/edit_username1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="text"
android:text="@string/edit_username" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/edit_username1"
android:layout_below="@+id/edit_username1"
android:layout_marginTop="15dp"
android:text="@string/choose_gender"
android:textAppearance="?android:attr/textAppearanceMedium" />

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="15dp"
android:orientation="vertical" >

<RadioButton
android:id="@+id/radio_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/male" />

<RadioButton
android:id="@+id/radio_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/female" />
</RadioGroup>

<EditText
android:id="@+id/edit_password1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_below="@+id/radioGroup1"
android:layout_marginTop="15dp"
android:ems="10"
android:inputType="text"
android:text="@string/edit_password" />

<EditText
android:id="@+id/edit_confirm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/edit_password2"
android:layout_below="@+id/edit_password2"
android:layout_marginTop="15dp"
android:ems="10"
android:inputType="text"
android:text="@string/confirm_password" />

<Button
android:id="@+id/btn_registry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/edit_confirm"
android:layout_below="@+id/edit_confirm"
android:layout_marginTop="15dp"
android:onClick="register"
android:text="@string/btn_register" />

</RelativeLayout>

这是我的UserAccount_Activity的代码:

public class UserAccount_Activity extends ActionBarActivity implements
    ActionBar.TabListener {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a {@link FragmentPagerAdapter}
 * derivative, which will keep every loaded fragment in memory. If this
 * becomes too memory intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;


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

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.user_account_, 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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            return new RegistryFragment();

        case 1:

            return new LoginFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        }
        return null;
    }
}

public static class RegistryFragment extends Fragment {

    private Encryptor encrypt;

    private EditText edit_username;
    private EditText edit_password;
    private EditText edit_confirm;

    private Button registry;

    public RegistryFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_registry,
                container, false);
                    encrypt=new Encryptor();
        edit_username = (EditText) rootView
                .findViewById(R.id.edit_username1);
        edit_password = (EditText) rootView
                .findViewById(R.id.edit_password1);
        edit_confirm = (EditText) rootView.findViewById(R.id.edit_confirm);

        registry = (Button) rootView.findViewById(R.id.btn_registry);

        registry.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                encrypt.setSaveEntry(getActivity(), "uname", 
edit_username
                        .getText().toString());
                encrypt.setSaveEntry(getActivity(), "upass", 
edit_password
                        .getText().toString());

                Context context  
getActivity().getApplicationContext();
                CharSequence text    
encrypt.getSaveEntry(getActivity(),
                        "uname")
                        + " "
                        +  
encrypt.getSaveEntry(getActivity(), "upass");
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text,     
duration);
                toast.show();
            }
        });

        return rootView;
    }
}

public static class LoginFragment extends Fragment {

    public LoginFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_login,
                container, false);

        return rootView;
    }
}

}

抱歉,我忘记了Logcat输出,所以这里是:

04-14 09:24:56.969: I/InputMethodManager(26111): startInput,    
mServedView=android.widget.EditText{417f4d58 VFED..CL .F.P..ID 0,396-720,475 #7f050044 
app:id/edit_password2}, inputType=0x1
04-14 09:25:00.444: I/InputMethodManager(26111): startInput, 
mServedView=android.widget.EditText{417f69b0 VFED..CL .F.P..ID 0,505-720,584 #7f050045  
app:id/edit_confirm}, inputType=0x1
04-14 09:25:04.479: W/dalvikvm(26111): threadid=1: thread exiting with uncaught 
exception (group=0x413b8a08)
04-14 09:25:04.494: E/AndroidRuntime(26111): FATAL EXCEPTION: main
04-14 09:25:04.494: E/AndroidRuntime(26111): java.lang.NullPointerException
04-14 09:25:04.494: E/AndroidRuntime(26111):    at    
com.mHealth.screeningApp2.activities.UserAccount_Activity$RegistryFragment$1.onClick    
(UserAccount_Activity.java:213)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at   
android.view.View.performClick(View.java:4231)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at   
android.view.View$PerformClick.run(View.java:17537)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at 
android.os.Handler.handleCallback(Handler.java:725)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at  
android.os.Handler.dispatchMessage(Handler.java:92)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at 
android.os.Looper.loop(Looper.java:158)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at   
android.app.ActivityThread.main(ActivityThread.java:5751)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at  
java.lang.reflect.Method.invokeNative(Native Method)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at  
java.lang.reflect.Method.invoke(Method.java:511)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at  
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
04-14 09:25:04.494: E/AndroidRuntime(26111):    at  
dalvik.system.NativeStart.main(Native Method)
04-14 09:25:06.384: D/Process(26111): killProcess, pid=26111

这里也是包含get-和setSaveEntry方法的Encryptor类:

package com.mHealth.screeningApp2.helperClasses;

import android.content.Context;
import android.content.SharedPreferences;


public class Encryptor {


public Encryptor() {

}

/**
 * Save a String value persistent on the smartphone indentified by a key
 * @param context Activity context
 * @param key String identifier for the saved value
 * @param name String value which to save persistent
 */
public void setSaveEntry(Context context, String key, String name) {
    // encrypt values
    String crypt_key = null;
    String crypt_name = null;

    try {
        crypt_key = SimpleCrypto.encrypt(

"das22ist3das1passwort98mit1demallesumgerechnetwird", key);
        crypt_name = SimpleCrypto.encrypt(

"das22ist3das1passwort98mit1demallesumgerechnetwird", name);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // save the entry name as SharedPreference at the Smartphone, in the
    // filename "prefs.xml"
    SharedPreferences entry = context.getSharedPreferences("prefs", 0);
    SharedPreferences.Editor editor = entry.edit();
    editor.putString(crypt_key, crypt_name);
    editor.commit();
}

/**
 * Returns the persistent saved value of an asked key identifier
 * 
 * @param context Activity context
 * @param key String identifier under which name the value was saved
 * @return decrypted String key
 */
public String getSaveEntry(Context context, String key) {
    String crypt_entry = null;
    // encrypt the checked field again to get the encrypted String and
    // retrieve the stored checks
    try {
        crypt_entry = SimpleCrypto.encrypt(

"das22ist3das1passwort98mit1demallesumgerechnetwird", key);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Restore preferences
    SharedPreferences entry;

    // tell the correct preference file to be read
    try {
        entry = context.getSharedPreferences("prefs", 0);
    } catch (Exception e) {
        return "The SharedPreferences File does not exist.";
    }

    // first check whether the Key exist
    if (entry.contains(crypt_entry)) {

        // returns false if there is no saved value
        String entry_name = entry.getString(crypt_entry, "false");

        // abort if no value
        if (entry_name.equals("false")) {
            return "No Entry";
        }

        // decrypt the last saved datas at the smartphone
        String crypt_entry_name = null;
        try {
            crypt_entry_name = SimpleCrypto.decrypt(

"das22ist3das1passwort98mit1demallesumgerechnetwird",
                    entry_name);
        } catch (Exception e) {
            // e.printStackTrace();
            crypt_entry_name = "No Entry";
        }

        return crypt_entry_name;
    } else {
        return "notExist";
    }
}

}

2 个答案:

答案 0 :(得分:1)

您永远不会初始化encrypt变量:

private Encryptor encrypt;

但是在onClick()

中调用了一个方法

如何初始化:

encrypt = new Encryptor();

答案 1 :(得分:0)

检查您的EditText 用户名密码他们有id edit_username2 edit_password2 并且您的RegistryFragment更改了 edit_username1 edit_password1