我正在创建一个Android应用程序,其中我有一个主要活动有2个片段,fragUp和fragDown。 fragUp有两个编辑文本字段和一个按钮,fragDown有两个文本视图字段。
最初主要活动仅显示fragUp并询问用户输入字段,当用户单击按钮时,触发onClick方法并在fragUp下创建fragDown并显示用户输入的两个字段。登记/>
总结 -
FRAGMENTS- fragUp,fragDown
主要活动 - 主要活动
主要活动有两个线性布局:1.ly1用于fragUp
2.ly2用于fragDown
注意 - 所有片段都在主java文件中动态添加,而不是在main.xml文件中声明。
这是主要活动代码
public class MainActivity extends AppCompatActivity implements fragUp.fragInterface {
FragmentTransaction ft,ft1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ft=getFragmentManager().beginTransaction();
fragUp fu=new fragUp();
ft.add(R.id.ly1,fu); //Fragment fragUp is added dynamically.
ft.commit();
}
public void set(String a,String b){
ft1=getFragmentManager().beginTransaction();
fragDown ob=new fragDown();
ft1.add(R.id.ly2,ob);
ft1.commit();
fragDown fd=(fragDown)getFragmentManager().findFragmentById(R.id.ly2);
fd.seta(a, b);
}
这是我的fragUp java代码 -
public class fragUp extends Fragment {
View v;
public interface fragInterface{
public void set(String a,String b);
}
fragInterface ob;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v=inflater.inflate(R.layout.fragup,container,false);
Button b=(Button)v.findViewById(R.id.button1);
b.setOnClickListener( new OnClickListener(){
public void onClick(View v){
store();
}});
return v;
}
public void onAttach(Activity activity){
super.onAttach(activity);
try{
ob=(fragInterface)activity;
}
catch(ClassCastException e){
throw new ClassCastException();
}
}
public void store(){ with main activity
EditText et1=(EditText)v.findViewById(R.id.editText1);
EditText et2=(EditText)v.findViewById(R.id.editText2);
ob.set(et1.getText().toString(),et2.getText().toString()); //Interface implemented to share data
}
}
这是fragDown java code-
public class fragDown extends Fragment {
View v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v=inflater.inflate(R.layout.fragdown,container,false);
return v;
}
public void seta(String a,String b){
TextView t1=(TextView)v.findViewById(R.id.textView1);
TextView t2=(TextView)v.findViewById(R.id.textView2);
t1.setText(a);
t2.setText(b);
}
}
当我运行应用程序并按下按钮时,我的应用程序崩溃,logcat说 -
08-05 09:37:02.198: E/AndroidRuntime(2866): java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.fragment.fragDown.seta(java.lang.String, java.lang.String)' on a null object reference
08-05 09:37:02.198: E/AndroidRuntime(2866): at com.example.fragment.Second.set(Second.java:30)
08-05 09:37:02.198: E/AndroidRuntime(2866): at com.example.fragment.fragUp.store(fragUp.java:52)
08-05 09:37:02.198: E/AndroidRuntime(2866): at com.example.fragment.fragUp$1.onClick(fragUp.java:25)
如果有人能帮助我,那就太棒了。我花了几个小时调试它找不到我出错的地方。请帮忙。
答案 0 :(得分:2)
为什么不将值作为参数传递给片段?请查看本指南中的“布局”部分:http://developer.android.com/reference/android/app/Fragment.html
以下是该指南的摘录,可以帮助您:
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
然后在设置片段时:
// Prepare args
Bundle arguments = new Bundle();
arguments.putString(key, value);
// Init fragment and pass args
fragment = new DetailsFragment();
fragment.setArguments(arguments);
// Commit to the fragment manager, thus
// inflating the view
getSupportFragmentManager().beginTransaction()
.add(R.id.your_id, fragment).commit();
您可以访问片段的onCreate
方法中的捆绑参数。
具体解决您的代码,您应该:
MainActivity.set
,将args传递给您的片段:
public void set(String a,String b) {
ft1 = getFragmentManager().beginTransaction();
fragDown ob = new fragDown();
// Add args. It is a good practice
// to declare keys as public static final
// variables, accessing them accordingly. Here
// I'm just putting the string, but you should
// avoid that.
Bundle args = new Bundle();
args.putString("key_a", a);
args.putString("key_b", b);
ob.setArguments(args);
ft1.add(R.id.ly2, ob);
ft1.commit();
}
添加FragDown.onCreate
方法:
private String a = "";
private String b = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.a = getArguments().getString("key_a");
this.b = getArguments().getString("key_b");
}
在seta
上致电onCreateView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragdown, container, false);
seta(this.a, this.b)
return v;
}
这是由于Fragment的生命周期:http://developer.android.com/reference/android/app/Fragment.html#Lifecycle
现在,提出一些建议: