在你开始回答之前我尝试使用sharedpreferences分享数据...我尝试捆绑的东西,但仍然有空值和应用程序粉碎,我的应用程序是一个绘图菜单当你点击listview项目它带来一个片段在前面我有几个edittext textviews,当我点击一个edittext来改变它完美的文本时,问题是edittext最初是空白的,它必须从主活动加载数据,这里我得到了空白
[MainActivity] onCreate方法
EditText editus_ername = (EditText) findViewById(R.id.editUserName);
editus_ername.setText("Username");
[片段类]
EditText editus_ername;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_whats_hot, container, false);
editus_ername = (EditText)getActivity().findViewById(R.id.editUserName);
return rootView;
}
12-14 12:32:48.699: E/AndroidRuntime(1517): FATAL EXCEPTION: main
12-14 12:32:48.699: E/AndroidRuntime(1517): java.lang.NullPointerException
12-14 12:32:48.699: E/AndroidRuntime(1517): at info.androidhive.slidingmenu.WhatsHotFragment.onCreateView(WhatsHotFragment.java:40)
12-14 12:32:48.699: E/AndroidRuntime(1517): at android.app.Fragment.performCreateView(Fragment.java:1695)
12-14 12:32:48.699: E/AndroidRuntime(1517): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
12-14 12:32:48.699: E/AndroidRuntime(1517): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
12-14 12:32:48.699: E/AndroidRuntime(1517): at android.app.BackStackRecord.run(BackStackRecord.java:682)
12-14 12:32:48.699: E/AndroidRuntime(1517): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
12-14 12:32:48.699: E/AndroidRuntime(1517): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
12-14 12:32:48.699: E/AndroidRuntime(1517): at android.os.Handler.handleCallback(Handler.java:725)
12-14 12:32:48.699: E/AndroidRuntime(1517): at android.os.Handler.dispatchMessage(Handler.java:92)
12-14 12:32:48.699: E/AndroidRuntime(1517): at android.os.Looper.loop(Looper.java:137)
12-14 12:32:48.699: E/AndroidRuntime(1517): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-14 12:32:48.699: E/AndroidRuntime(1517): at java.lang.reflect.Method.invokeNative(Native Method)
12-14 12:32:48.699: E/AndroidRuntime(1517): at java.lang.reflect.Method.invoke(Method.java:511)
12-14 12:32:48.699: E/AndroidRuntime(1517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-14 12:32:48.699: E/AndroidRuntime(1517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-14 12:32:48.699: E/AndroidRuntime(1517): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您使用getactivity调用edittext,将其更改为;
editus_ername = (EditText)getActivity().findViewById(R.id.editUserName);
用;
editus_ername = (EditText)rootView.findViewById(R.id.editUserName);
如果要将活动中的值传递给片段,请在Activity.java中使用;
Bundle bundle = new Bundle();
bundle.putString("value", value);
fragment.setArguments(bundle);
并在fragment.java中使用;
value= getArguments().getString("value");
答案 1 :(得分:0)
在您的片段类中,如果EditText
位于R.layout.fragment_whats_hot
布局中,请执行以下操作:
editus_ername = (EditText) rootView.findViewById(R.id.editUserName);
如果您想从主要活动中将数据传递到您的片段类:
arguments
或者在片段中创建一个方法:
public void test(int value){ // 做东西 }
在主要活动中调用此方法。
答案 2 :(得分:-2)
尝试通过片段构造函数或新的公共方法发送您的String。
例如构造函数:
片段类:
String _userName;
EditText editus_ername;
public FragmentClassName(String userName){
_userName = userName;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_whats_hot, container, false);
editus_ername = (EditText)getActivity().findViewById(R.id.editUserName);
editus_ername.setText(_userName);
return rootView;
}
MainActivity:
new FragmentClassName("Username");