2015-04-04 141 views
0

我正在使用片段類創建一個橫向佈局。但現在我的代碼在我的listMovieActivity文件中顯示錯誤。我嘗試了一些從stackoverflow的答案,但沒有幫助我。android view充氣異常錯誤和java空指針異常

這是我的listMovieActivity代碼:

package com.example.moviemanager; 

import android.content.Intent; 
import android.content.res.Configuration; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 


public class listMovieActivity extends FragmentActivity { 
    private MovieDetailFragment detailFragment = null; 
    private MovieListFragment listFragment = null; 
    private FragmentManager manager; 
    private int selectedItemIndex = -1; 
    private String title=null; 


    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listmovie); 

     manager = getSupportFragmentManager(); 
     detailFragment = (MovieDetailFragment) manager.findFragmentById(R.id.detailmovie_fragment); 
     listFragment = (MovieListFragment) manager.findFragmentById(R.id.listmovie_fragment); 
    } 

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

    protected void onResume() { 
     super.onResume(); 
     int orientation = getResources().getConfiguration().orientation; 

     if (orientation == Configuration.ORIENTATION_LANDSCAPE) 
      showDetails(selectedItemIndex,title); 

    } 

    public void showDetails(int selectedItem, String ttl) { 
     //Log.d("FRAGMENT", "Selected item " + selectedItem); 
     selectedItemIndex = selectedItem; 
     title = ttl; 
     int orientation = getResources().getConfiguration().orientation; 

     if (orientation == Configuration.ORIENTATION_LANDSCAPE) { 

      if (detailFragment != null) { 
       // update entry 
       detailFragment.updateDetails(selectedItem,ttl); 
      } 
     } else { 
      // show DetailsActivity 
      Intent intent = new Intent(this, DetailMovie.class); 
      intent.putExtra("POSITION", selectedItem); 
      intent.putExtra("TITLE", ttl); 
      startActivity(intent); 
     } 
    } 


} 

這是我的數據庫處理器代碼:

public class DatabaseHandler { 
    String cmd; 
    private static final String DBTAG = "DatabaseHandler"; 

    public static final String KEY_ROWID = "_id"; 
    public static final int COL_ROWID = 0; 

    public static final String KEY_KEY = "key"; 
    public static final String KEY_TITLE = "title"; 
    public static final String KEY_TYPE = "type"; 
    public static final String KEY_STORY = "story"; 
    public static final String KEY_RATING = "rating"; 
    public static final String KEY_LANGUAGE = "language"; 
    public static final String KEY_RUNTIME = "runtime"; 

    public static final int COL_KEY = 1; 
    public static final int COL_TITLE = 2; 
    public static final int COL_TYPE = 3; 
    public static final int COL_STORY = 4; 
    public static final int COL_RATING = 5; 
    public static final int COL_LANGUAGE = 6; 
    public static final int COL_RUNTIME = 7; 

    public static final String[] ALL_KEYS = new String[] { 
     KEY_ROWID, KEY_KEY, KEY_TITLE, KEY_TYPE, KEY_STORY, 
     KEY_RATING, KEY_LANGUAGE, KEY_RUNTIME}; 

    public static final String DATABASE_NAME = "movieDb"; 
    public static final String DATABASE_TABLE = "movieTable"; 

    public static final int DATABASE_VERSION = 1; 

    private static final String DATABASE_CREATE_SQL = 
      "create table " + DATABASE_TABLE 
      + " (" + KEY_ROWID + " integer primary key autoincrement," 
      + KEY_KEY + " text not null," 
      + KEY_TITLE + " text not null," 
      + KEY_TYPE + " text not null," 
      + KEY_STORY + " text not null," 
      + KEY_RATING + " text not null," 
      + KEY_LANGUAGE + " text not null," 
      + KEY_RUNTIME + " integer not null" 
      + ");"; 

    private final Context context; 

    private DatabaseHelper dbHelper; 
    private SQLiteDatabase db; 

    public DatabaseHandler(Context ctx){ 
     this.context = ctx; 
     dbHelper = new DatabaseHelper(context); 
    } 

    public DatabaseHandler open(){ 
     db = dbHelper.getWritableDatabase(); 
     return this; 
    } 

    public void close(){ 
     dbHelper.close(); 
    } 

    public void insertRow(String mvkey, String mvtitle, String mvtype, 
      String mvstory, String mvrating, String mvlanguage, int mvruntime){ 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_KEY, mvkey); 
     initialValues.put(KEY_TITLE, mvtitle); 
     initialValues.put(KEY_TYPE, mvtype); 
     initialValues.put(KEY_STORY, mvstory); 
     initialValues.put(KEY_RATING, mvrating); 
     initialValues.put(KEY_LANGUAGE, mvlanguage); 
     initialValues.put(KEY_RUNTIME, mvruntime); 

     open(); 
     db.insert(DATABASE_TABLE, null, initialValues); 
     close(); 
    } 

    public void deleteRow(long rowId){ 
     open(); 
     cmd = new String ("DELETE FROM " + DATABASE_TABLE + " WHERE ("+KEY_ROWID+" =" + rowId + ");"); 
     db.execSQL(cmd); 
     close(); 
//  String where = KEY_ROWID + "=" + rowId; 
//  return db.delete(DATABASE_TABLE, where, null) != 0; 
    } 

    public void deleteAll(){ 
     Cursor c = getAllRows(); 
     long rowId = c.getColumnIndexOrThrow(KEY_ROWID); 
     if(c.moveToFirst()){ 
      do{ 
       deleteRow(c.getLong((int)rowId)); 
      }while(c.moveToNext()); 
     } 
     c.close(); 
    } 

    public Cursor getAllRows(){ 
     String where = null; 
     Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null); 
     if(c!= null){ 
      c.moveToFirst(); 

     } 
     return c; 
    } 

    public Cursor getRow(long rowId){ 
     String where = KEY_ROWID + "=" + rowId; 
     Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null); 
     if(c!= null){ 
      c.moveToFirst(); 

     } 
     return c; 
    } 

    public boolean updateRow(long rowId, String mvkey, String mvtitle, 
      String mvtype, String mvstory, String mvrating, 
      String mvlanguage, int mvruntime){ 
     String where = KEY_ROWID + "=" + rowId; 

     ContentValues newValues = new ContentValues(); 
     newValues.put(KEY_KEY, mvkey); 
     newValues.put(KEY_TITLE, mvtitle); 
     newValues.put(KEY_TYPE, mvtype); 
     newValues.put(KEY_STORY, mvstory); 
     newValues.put(KEY_RATING, mvrating); 
     newValues.put(KEY_LANGUAGE, mvlanguage); 
     newValues.put(KEY_RUNTIME, mvruntime); 

     return db.update(DATABASE_TABLE, newValues, where, null) != 0; 

    } 

    public Cursor listMovieTitle(){ 
     return db.rawQuery("SELECT "+ KEY_TITLE + " FROM " + DATABASE_TABLE,null); 

    } 

    public Cursor get_id(String ttl){ 
     return db.rawQuery("SELECT "+ KEY_ROWID + " FROM " + DATABASE_TABLE + " WHERE (" + KEY_TITLE + " = " + ttl + ");",null); 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper { 

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

     @Override 
     public void onCreate(SQLiteDatabase _db) { 
      _db.execSQL(DATABASE_CREATE_SQL); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) { 
      Log.w(DBTAG, "Upgrading application's database from version " + oldVersion 
        + " to " + newVersion + ", which will destroy all old data!"); 

      // Destroy old database: 
      _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 

      // Recreate new database: 
      onCreate(_db); 
     } 

    } 

} 

這是錯誤的堆棧跟蹤我我每次我點擊我的顯示按鈕接收:

04-04 01:00:15.314  811-811/com.example.moviemanager E/Trace﹕ error opening trace file: No such file or directory (2) 
04-04 01:01:15.098  859-859/com.example.moviemanager E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.moviemanager/com.example.moviemanager.listMovieActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
      at android.app.ActivityThread.access$600(ActivityThread.java:141) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:5041) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment 
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
      at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270) 
      at android.app.Activity.setContentView(Activity.java:1881) 
      at com.example.moviemanager.listMovieActivity.onCreate(listMovieActivity.java:20) 
      at android.app.Activity.performCreate(Activity.java:5104) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
            at android.app.ActivityThread.access$600(ActivityThread.java:141) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:5041) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
            at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 
      at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 
      at com.example.moviemanager.DatabaseHandler.open(DatabaseHandler.java:65) 
      at com.example.moviemanager.MovieListFragment.onCreateView(MovieListFragment.java:29) 
      at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478) 
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900) 
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082) 
      at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184) 
      at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285) 
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676) 
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270) 
            at android.app.Activity.setContentView(Activity.java:1881) 
            at com.example.moviemanager.listMovieActivity.onCreate(listMovieActivity.java:20) 
            at android.app.Activity.performCreate(Activity.java:5104) 
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
            at android.app.ActivityThread.access$600(ActivityThread.java:141) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:5041) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
            at dalvik.system.NativeStart.main(Native Method) 
04-04 01:01:19.447  879-879/com.example.moviemanager E/Trace﹕ error opening trace file: No such file or directory (2) 

此前,我有同樣的問題錯誤膨脹類的片段,但後來我這樣通過更改我的android listview id android android custome list id來實現它。但現在問題再次出現,但這次是最差的我的數據庫處理程序也顯示出錯誤。任何人都可以幫助我。

回答

0

在數據庫處理器:

private final Context context; 

這是沒有意義的,最後一個字段不能assigned.And你怎麼可以把它通過檢查?請使用MovieListFragment代碼。

+0

沒關係我已經解決了謝謝 – user3291870 2015-04-04 02:40:34