2016-01-14 42 views
0

我想創建一個Android應用程序,從SQLite數據庫中獲取數據並將其顯示在PageView(Swipe View)中。隨時嘗試運行應用程序崩潰。請幫我看看我的代碼,看看我錯了。使用SQLite與PageView

我的主要活動是:

public class MainActivity extends AppCompatActivity { 

SectionsPagerAdapter mSectionsPagerAdapter; 

ViewPager mViewPager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    DataBaseAccess databaseAccess = DataBaseAccess.getInstance(this); 
    databaseAccess.open(); 
    List<String> quotes = databaseAccess.getQuotes(); 
    databaseAccess.close(); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the activity. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),getApplicationContext()); 


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    private static final String ARG_SECTION_NUMBER = "section_number"; 
    private static final String Name_Key = "Name_Key"; 

    /*public PlaceholderFragment() { 
    }*/ 

    /** 
    * Returns a new instance of this fragment for the given section 
    * number. 
    */ 
    /* 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) { 


     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

     Bundle bundle=getArguments(); 

     if (bundle!=null) 
     { 
      String name=bundle.getString(Name_Key); 
      setValues(rootView,name); 
     } 


     return rootView; 
    } 
    private void setValues(View v, String name) 
    { 
     TextView textview=(TextView) v.findViewById(R.id.name); 
     textview.setText(name); 
    } 
} 

/** 
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
* one of the sections/tabs/pages. 
*/ 
public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    List<String> name; 
    String[] name1; 
    public SectionsPagerAdapter(FragmentManager fm, Context context) { 
     super(fm); 
     DataBaseAccess databaseAccess = DataBaseAccess.getInstance(null); 
     databaseAccess.open(); 
     name=databaseAccess.getQuotes(); 
     for (int i=0; i<=name.size();i++) 
     { 
      name1[i]=name.get(i); 
     } 

    } 

    @Override 
    public Fragment getItem(int position) { 

     Bundle bundle=new Bundle(); 
     bundle.putString(PlaceholderFragment.Name_Key,name1[position]); 

     PlaceholderFragment deviceFragment= new PlaceholderFragment(); 
     deviceFragment.setArguments(bundle); 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a PlaceholderFragment (defined as a static inner class below). 
     return deviceFragment; 


    } 

    @Override 
    public int getCount() { 
     // Show 3 total pages. 
     return name1.length; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return name1[position]; 
    } 
} 
} 

我的班級訪問數據庫,並返回字符串

public class DataBaseAccess { 
private SQLiteOpenHelper openHelper; 
private static SQLiteDatabase database; 
private static DataBaseAccess instance; 

/** 
* Private constructor to aboid object creation from outside classes. 
* 
* @param context 
*/ 
public DataBaseAccess(Context context) { 
    this.openHelper = new DatabaseOpenHelper(context); 
} 

/** 
* Return a singleton instance of DatabaseAccess. 
* 
* @param context the Context 
* @return the instance of DabaseAccess 
*/ 
public static DataBaseAccess getInstance(Context context) { 
    if (instance == null) { 
     instance = new DataBaseAccess(context); 
    } 
    return instance; 
} 

/** 
* Open the database connection. 
*/ 
public void open() { 
    this.database = openHelper.getWritableDatabase(); 
} 

/** 
* Close the database connection. 
*/ 
public void close() { 
    if (database != null) { 
     //this.database.close(); 
    } 
} 

/** 
* Read all quotes from the database. 
* 
* @return a List of quotes 
*/ 
public ArrayList getQuotes() { 
    ArrayList<String> list = new ArrayList<>(); 
    Cursor cursor = database.rawQuery("SELECT * FROM quotes", null); 
    cursor.moveToFirst(); 
    while (!cursor.isAfterLast()) { 
     list.add(cursor.getString(0)); 
     cursor.moveToNext(); 
    } 
    cursor.close(); 
    return list; 
} 
} 

我Activity_main文件

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/main_content" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="org.heywhyconcept.myapplicationscroll.MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="@dimen/appbar_padding_top" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 

    </android.support.v7.widget.Toolbar> 

</android.support.design.widget.AppBarLayout> 

<android.support.v4.view.ViewPager 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="end|bottom" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_dialog_email" /> 

我Fragment_main文件列表

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="org.heywhyconcept.myapplicationscroll.MainActivity$PlaceholderFragment"> 

<TextView 
    android:id="@+id/section_label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="YUSUF AYOMIDE" 
    /> 

<TextView 
    android:id="@+id/name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="text" 
    /> 
</LinearLayout> 

回答

0

從您提供的代碼,似乎您試圖使用SELECT聲明沒有嚴格的定義和第一創建數據庫的表訪問表quotes

您需要明確定義應該出現在數據庫中的表。還有就是如何做到這一點的docs,我將在這裏套用一個很好的例子:

public class FeedReaderDbHelper extends SQLiteOpenHelper { 

    public static final int DATABASE_VERSION = 1; 
    public static final String DATABASE_NAME = "myDatabase.db"; 
    private static final String SQL_CREATE_ENTRIES = 
    "CREATE TABLE quotes (" + 
    "id INTEGER PRIMARY KEY, " + 
    "quote TEXT)"; 

    public FeedReaderDbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    //Create tables here 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(SQL_CREATE_ENTRIES); 
    } 
} 
+0

我有我的計劃,我通過類訪問與此代碼準備數據庫。 'public class DatabaseOpenHelper extends SQLiteAssetHelper {private static static final String DATABASE_NAME =「quotes.db」; private static final int DATABASE_VERSION = 1; public DatabaseOpenHelper(上下文上下文)super(context,DATABASE_NAME,null,DATABASE_VERSION); } }'。當我在列表視圖中顯示它時,它工作正常。請幫助我 –

+0

@AyomideAdekunle然後請提供任何顯示崩潰的日誌的輸出 –

+0

致命例外:main 進程:org.heywhyconcept.myapplicationscroll,PID:15057 java.lang.RuntimeException:無法啓動活動ComponentInfo {org.heywhyconcept.myapplicationscroll/org.heywhyconcept.myapplicationscroll.MainActivity}:java.lang.NullPointerExceptionat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2447) –