2014-08-28 35 views
0

我希望你們能幫助我。如何正確初始化片段上的值?我可以使用下面的語法從我的數據庫中初始化int和string值嗎?初始化android片段上的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開發和任何幫助將不勝感激。提前致謝。

回答

1

您正試圖訪問未初始化的變量。試試這個

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; 



     } 



     } 
+0

太謝謝你了!你是一個拯救生命的人。 – DEADPOOL 2014-08-28 07:26:33

+0

@DEADPOOL威爾士歡呼! – ZInj 2014-08-28 09:28:40

1
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); 
} 

}