我的分段類中爲空指針異常錯誤。我在我的程序中使用選項卡活動,列表視圖位於片段xml文件中。代碼:空指針異常錯誤android
package com.example.timetracker;
import ///
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener, LoaderCallbacks<Cursor> {
private static String TAG = "MainActivity";
private static final int CM_DELETE_ID = 1;
private static final int RESET_STOPWATCH = 2;
static ListView lvData;
static DB db;
static SimpleCursorAdapter scAdapter;
static Activity act;
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
//create my tabs...
}
}
//some code...
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
if(position == 0){
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
if (position == 1)
{
Fragment fragment2 = new DummySectionFragment2();
Bundle args = new Bundle();
args.putInt(DummySectionFragment2.ARG_SECTION_NUMBER, position + 2);
fragment2.setArguments(args);
return fragment2;
}
return null;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
}
public static class DummySectionFragment extends Fragment{
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
View v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_tracker,
container, false);
v = inflater.inflate(R.layout.activity_statistic,
container, false);
createList();
return rootView;
}
public void createList(){
db = new DB(act);Log.d(TAG, "1");
db.open();Log.d(TAG, "2");
String[] from = new String[] {DB.COLUMN_NAME, DB.COLUMN_TIME };Log.d(TAG, "3");
int[] to = new int[] { R.id.tvTextName, R.id.tvText_time};Log.d(TAG, "4");
scAdapter = new SimpleCursorAdapter(act, R.layout.item, null, from, to, 0);Log.d(TAG, "5");
lvData = (ListView) v.findViewById(R.id.lvData);Log.d(TAG, "6 ");
//after thap i have error
lvData.setAdapter(scAdapter);Log.d(TAG, "7");
}
}
public static class DummySectionFragment2 extends Fragment implements LoaderCallbacks<Cursor> {
//some code....
}
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
}
}
//Data base class
static class MyCursorLoader extends CursorLoader {
DB db;
public MyCursorLoader(Context context, DB db) {
super(context);
this.db = db;
}
@Override
public Cursor loadInBackground() {
Cursor cursor = db.getAllData();
return cursor;
}
}
}
請勿使用代碼「lvData.setAdapter(scAdapter); Log.d(TAG,」7「);」日誌:
04-03 20:11:54.830: D/dalvikvm(1684): GC_FOR_ALLOC freed 97K, 6% free 2883K/3040K, paused 9ms, total 9ms
04-03 20:11:54.850: D/MainActivity(1684): 1
04-03 20:11:54.850: D/Data Base(1684): Открытие Подключения
04-03 20:11:54.858: D/Data Base(1684): Подключение открыто
04-03 20:11:54.858: D/MainActivity(1684): 2
04-03 20:11:54.858: D/MainActivity(1684): 3
04-03 20:11:54.858: D/MainActivity(1684): 4
04-03 20:11:54.858: D/MainActivity(1684): 6
04-03 20:11:54.858: D/MainActivity(1684): 5
04-03 20:11:54.858: D/AndroidRuntime(1684): Shutting down VM
04-03 20:11:54.858: W/dalvikvm(1684): threadid=1: thread exiting with uncaught exception (group=0xa4c41648)
04-03 20:11:54.858: E/AndroidRuntime(1684): FATAL EXCEPTION: main
04-03 20:11:54.858: E/AndroidRuntime(1684): java.lang.NullPointerException
04-03 20:11:54.858: E/AndroidRuntime(1684): at com.example.timetracker.MainActivity$DummySectionFragment.createList(MainActivity.java:200)
04-03 20:11:54.858: E/AndroidRuntime(1684): at com.example.timetracker.MainActivity$DummySectionFragment.onCreateView(MainActivity.java:187)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.View.measure(View.java:15848)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.View.measure(View.java:15848)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
04-03 20:11:54.858: E/AndroidRuntime(1684): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.View.measure(View.java:15848)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-03 20:11:54.858: E/AndroidRuntime(1684): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.View.measure(View.java:15848)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.Choreographer.doFrame(Choreographer.java:532)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.os.Handler.handleCallback(Handler.java:730)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.os.Looper.loop(Looper.java:137)
04-03 20:11:54.858: E/AndroidRuntime(1684): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-03 20:11:54.858: E/AndroidRuntime(1684): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 20:11:54.858: E/AndroidRuntime(1684): at java.lang.reflect.Method.invoke(Method.java:525)
04-03 20:11:54.858: E/AndroidRuntime(1684): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-03 20:11:54.858: E/AndroidRuntime(1684): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-03 20:11:54.858: E/AndroidRuntime(1684): at dalvik.system.NativeStart.main(Native Method)
這行代碼是什麼:'MainActivity.java:187'? –
我認爲'行爲'不會在任何地方初始化。對?所以,'db = new DB(act);''db' null –
是的,我忘記刪除那個var) – kolodach