我有一个类var.java,其包含声明为静态public static Bundle bresult;
的bundle,如果我想将字符串放入该包var.bresult.putString("h",String.valueOf(h));
应用程序崩溃,则活动片段,但是如果我创建一个新包在activity类中并将值放入其中,然后使静态bundle等于它工作的新bundle:
public class var {
public static boolean u = true ;
public static int ui = 0;
public static Bundle bresult;
}
在这里输入代码
Bundle bresult2 = new Bundle();
bresult2.putString("h",String.valueOf(h));
var.bresult = bresult2;
为什么会发生这种情况,如果我使用此解决方案,可能会导致崩溃? 这是片段代码:
public class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_activity_b, container, false);
final EditText etxh;
final Button buDesign =(Button)rootView.findViewById(R.id.buDesign);
buDesign.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
etxh = (EditText)rootView.findViewById(R.id.editText2);
h = Double.valueOf(etxh.getText().toString());
var.bresult.putString("h",String.valueOf(h));
}
});
return rootView;
}
答案 0 :(得分:0)
您的静态变量没有实例,因为您没有初始化它:
public static Bundle bresult;
必须成为:
public static Bundle bresult = new Bundle();