我希望你们能帮助我。如何正确初始化片段上的值?我可以使用下面的语法用我的数据库中的值初始化int和string吗?
public class AccountFragment extends Fragment {
private LoginDataBaseAdapter loginDataBaseAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// create a instance of SQLite Database
loginDataBaseAdapter = new LoginDataBaseAdapter(getActivity());
loginDataBaseAdapter=loginDataBaseAdapter.open();
View rootView = inflater.inflate(R.layout.fragment_account, container, false);
TextView a = (TextView) rootView.findViewById(R.id.secsummview);
a.setText(secRows);
TextView b = (TextView) rootView.findViewById(R.id.studsummview);
b.setText(subRows);
TextView c = (TextView) rootView.findViewById(R.id.subsummview);
c.setText(unitSum);
TextView d = (TextView) rootView.findViewById(R.id.nameview);
d.setText(name);
return rootView;
}
int secRows = loginDataBaseAdapter.countsecRows();
int subRows = loginDataBaseAdapter.countsubRows();
int unitSum = loginDataBaseAdapter.countunitsSum();
String name=loginDataBaseAdapter.getUsername();
}
我尝试在下面,上面甚至是oncreate中初始化这些值,但它不起作用。这是logcat:
08-28 14:40:59.184: E/AndroidRuntime(29974): FATAL EXCEPTION: main
08-28 14:40:59.184: E/AndroidRuntime(29974): java.lang.NullPointerException
08-28 14:40:59.184: E/AndroidRuntime(29974): at com.csu.eclassrecord.AccountFragment.<init>(AccountFragment.java:41)
08-28 14:40:59.184: E/AndroidRuntime(29974): at com.eclassrecord.tabsswipe.adapter.TabsPagerAdapter.getItem(TabsPagerAdapter.java:22)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.View.measure(View.java:15609)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.View.measure(View.java:15609)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.View.measure(View.java:15609)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-28 14:40:59.184: E/AndroidRuntime(29974): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2221)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.View.measure(View.java:15609)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4906)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.Choreographer.doCallbacks(Choreographer.java:579)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.Choreographer.doFrame(Choreographer.java:548)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.os.Handler.handleCallback(Handler.java:725)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.os.Handler.dispatchMessage(Handler.java:92)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.os.Looper.loop(Looper.java:153)
08-28 14:40:59.184: E/AndroidRuntime(29974): at android.app.ActivityThread.main(ActivityThread.java:5331)
08-28 14:40:59.184: E/AndroidRuntime(29974): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 14:40:59.184: E/AndroidRuntime(29974): at java.lang.reflect.Method.invoke(Method.java:511)
08-28 14:40:59.184: E/AndroidRuntime(29974): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
08-28 14:40:59.184: E/AndroidRuntime(29974): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
08-28 14:40:59.184: E/AndroidRuntime(29974): at dalvik.system.NativeStart.main(Native Method)
我是Android开发的新手,我们将不胜感激。提前谢谢。
答案 0 :(得分:2)
您正在尝试访问未初始化的变量。试试这个
public class AccountFragment extends Fragment {
private LoginDataBaseAdapter loginDataBaseAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// create a instance of SQLite Database
loginDataBaseAdapter = new LoginDataBaseAdapter(getActivity());
loginDataBaseAdapter=loginDataBaseAdapter.open();
//intialize variables
int secRows = loginDataBaseAdapter.countsecRows();
int subRows = loginDataBaseAdapter.countsubRows();
int unitSum = loginDataBaseAdapter.countunitsSum();
String name=loginDataBaseAdapter.getUsername();
View rootView = inflater.inflate(R.layout.fragment_account, container, false);
TextView a = (TextView) rootView.findViewById(R.id.secsummview);
a.setText(secRows); //might have to cast as String like. String.valueOf(secRows);
TextView b = (TextView) rootView.findViewById(R.id.studsummview);
b.setText(subRows);
TextView c = (TextView) rootView.findViewById(R.id.subsummview);
c.setText(unitSum);
TextView d = (TextView) rootView.findViewById(R.id.nameview);
d.setText(name);
return rootView;
}
}
答案 1 :(得分:2)
public class AccountFragment extends Fragment {
private LoginDataBaseAdapter loginDataBaseAdapter;
int secRows;
int subRows;
int unitSum;
String name;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_account, container,
false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// create a instance of SQLite Database
loginDataBaseAdapter = new LoginDataBaseAdapter(getActivity());
loginDataBaseAdapter = loginDataBaseAdapter.open();
secRows = loginDataBaseAdapter.countsecRows();
subRows = loginDataBaseAdapter.countsubRows();
unitSum = loginDataBaseAdapter.countunitsSum();
name = loginDataBaseAdapter.getUsername();
View rootView = getView();
TextView a = (TextView) rootView.findViewById(R.id.secsummview);
a.setText(secRows);
TextView b = (TextView) rootView.findViewById(R.id.studsummview);
b.setText(subRows);
TextView c = (TextView) rootView.findViewById(R.id.subsummview);
c.setText(unitSum);
TextView d = (TextView) rootView.findViewById(R.id.nameview);
d.setText(name);
}
}