android视图膨胀异常错误和java空指针异常

时间:2015-04-04 01:15:28

标签: java android android-fragments android-sqlite

我正在使用片段类创建横向布局。但现在我的代码在listMovieActivity文件中显示错误。我尝试从stackoverflow中获得一些答案,但这对我没有帮助。

这是我的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);
        }
    }


}

这是我的DatabaseHandler代码:

    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 custome list id解决了它。但现在问题再次出现,但这次最糟糕的是我的数据处理程序也显示出错误。可以帮助我吗。

1 个答案:

答案 0 :(得分:0)

在DatabaseHandler中:

private final Context context;

这没有任何意义,无法分配最终字段。您如何通过支票?

请提供MovieListFragment代码。