本文介绍了logcat的消息:错误打开跟踪文件:没有这样的文件或目录(2)谁能帮助我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java的code:
    公共类SplashActivity扩展活动实现OnClickListener {

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.welcome);    ////////////////////////////////////////////////// ////////////////////
    ////////游戏菜单//////////////////////////////////////// /////////
    按钮playBtn =(按钮)findViewById(R.id.playBtn);
    playBtn.setOnClickListener(本);
    按钮settingsBtn =(按钮)findViewById(R.id.settingsBtn);
    settingsBtn.setOnClickListener(本);
    按钮rulesBtn =(按钮)findViewById(R.id.rulesBtn);
    rulesBtn.setOnClickListener(本);
    按钮exitBtn =(按钮)findViewById(R.id.exitBtn);
    exitBtn.setOnClickListener(本);
}
/ **
 *监听器为游戏菜单
 * /
@覆盖
公共无效的onClick(视图v){
    我的意图;    开关(v.getId()){
    案例R.id.playBtn:
        //登录后,加载主页
        //Log.d(\"LOGIN,用户已经开始游戏);        //获取问题集//
        清单<问题>问题= getQuestionSetFromDb();        //初始化游戏与检索问题集///
        游戏C =新的游戏();
        c.setQuestions(问题);
        c.setNumRounds(getNumQuestions());
        ((ChuckApplication)getApplication())setCurrentGame(c)中;        //开始游戏现在.. //
        I =新意图(这一点,QuestionActivity.class);
        startActivityForResult(ⅰ,Constants.PLAYBUTTON);
        打破;    案例R.id.rulesBtn:
        I =新意图(这一点,RulesActivity.class);
        startActivityForResult(ⅰ,Constants.RULESBUTTON);
        打破;    案例R.id.settingsBtn:
        I =新意图(这一点,SettingsActivity.class);
        startActivityForResult(ⅰ,Constants.SETTINGSBUTTON);
        打破;    案例R.id.exitBtn:
        完();
        打破;
    }}
/ **
 *方法检索一组随机从问题
 *数据库对于给定的难度
 * @返回
 *抛出:错误
 * /
私人列表<问题> getQuestionSetFromDb()抛出错误{
    INT的diff = getDifficultySettings();
    INT numQuestions = getNumQuestions();
    DBHelper myDbHelper =新DBHelper(本);
    尝试{
        myDbHelper.createDataBase();
    }赶上(IOException异常IOE){
        抛出新的错误(无法创建数据库);
    }
    尝试{
        myDbHelper.openDataBase();
    }赶上(的SQLException SQLE){
        扔SQLE;
    }
    清单<问题>问题= myDbHelper.getQuestionSet(差异,numQuestions);
    myDbHelper.close();
    返回的问题;
}
/ **
 *方法返回的难度设置
 * @返回
 * /
私人诠释getDifficultySettings(){
    共享preferences设置= getShared preferences(Constants.SETTINGS,0);
    INT差异= settings.getInt(Constants.DIFFICULTY,Constants.MEDIUM);
    返回差异;
}/ **
 *方法返回的问题数量的游戏
 * @返回
 * /
私人诠释getNumQuestions(){
    共享preferences设置= getShared preferences(Constants.SETTINGS,0);
    INT numRounds = settings.getInt(Constants.NUM_ROUNDS,20);
    返回numRounds;
}

}

当我打的播放按钮,应用程序停止。此按钮有采取新的活动打开连接到SQLite数据库测验qith 4选项单选按钮。

logcat的messaege:

 `9月5日至14日:57:07.126:D / dalvikvm(19541):GC_CONCURRENT释放189K,12%免费2676K / 3016K,暂停19ms + 7毫秒,总108ms
九月五日至14日:57:07.126:D / dalvikvm(19541):WAIT_FOR_CONCURRENT_GC受阻47ms
九月五日至14日:57:07.486:I /编舞(19541):跳过55帧!该应用程序可能会做它的主线程的工作太多了。
九月五日至14日:57:07.516:D / gralloc_goldfish(19541):无仿真模拟GPU检测。
九月五日至14日:57:10.116:D / AndroidRuntime(19541):关闭VM
九月五日至14日:57:10.116:W / dalvikvm(19541):主题ID = 1:螺纹未捕获的异常退出(组= 0x40a71930)
九月五日至14日:57:10.146:E / AndroidRuntime(19541):致命异常:主要
九月五日至14日:57:10.146:E / AndroidRuntime(19541):java.lang.ClassCastException:android.app.Application不能转换为com.pixy.quiz.ChuckApplication
九月五日至14日:57:10.146:E / AndroidRuntime(19541):在com.pixy.quiz.SplashActivity.onClick(SplashActivity.java:61)
九月五日至14日:57:10.146:E / AndroidRuntime(19541):在android.view.View.performClick(View.java:4204)
九月五日至14日:57:10.146:E / AndroidRuntime(19541):在android.view.View $ PerformClick.run(View.java:17355)
九月五日至14日:57:10.146:E / AndroidRuntime(19541):在android.os.Handler.handleCallback(Handler.java:725)
九月五日至14日:57:10.146:E / AndroidRuntime(19541):在android.os.Handler.dispatchMessage(Handler.java:92)
九月五日至14日:57:10.146:E / AndroidRuntime(19541):在android.os.Looper.loop(Looper.java:137)
九月五日至14日:57:10.146:E / AndroidRuntime(19541):在android.app.ActivityThread.main(ActivityThread.java:5041)
九月五日至14日:57:10.146:E / AndroidRuntime(19541):在java.lang.reflect.Method.invokeNative(本机方法)
九月五日至14日:57:10.146:E / AndroidRuntime(19541):在java.lang.reflect.Method.invoke(Method.java:511)
九月五日至14日:57:10.146:E / AndroidRuntime(19541):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
九月五日至14日:57:10.146:E / AndroidRuntime(19541):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
九月五日至14日:57:10.146:E / AndroidRuntime(19541):在dalvik.system.NativeStart.main(本机方法)
九月五日至14日:57:13.797:I /流程(19541):发送信号。 PID:19541 SIG:9`

我BDHelper类:

公共类DBHelper扩展SQLiteOpenHelper {

您的应用程序的数据库

  //而Android的默认系统路径。
私人静态字符串DB_PATH =/data/data/com.pixy.quiz/databases/;
私人静态字符串DB_NAME =questionsDb;
私人SQLiteDatabase MYDATABASE;
私人最终上下文myContext;/ **
 *构造
 *注意到并保持以访问该应用程序的资产和资源所传递的上下文的引用。
 * @参数方面
 * /
公共DBHelper(上下文的背景下){
    超级(上下文,DB_NAME,空,1);
    this.myContext =背景;
}/ **
 *在系统上创建一个空数据库,并与自己的数据库重写它。
 * * /
公共无效的CreateDatabase()抛出IOException    布尔dbExist = checkDataBase();
    如果(!dbExist)
    {
        //通过调用此方法与空的数据库将被创建到默认的系统路径
        //你的应用程序,所以我们要能够覆盖该数据库与我们的数据库。
        this.getReadableDatabase();        尝试{
            copyDataBase();
        }赶上(IOException异常五){
            抛出新的错误(错误复制数据库);
        }
    }
}/ **
 *检查是否已存在于数据库,以避免重新复制每次打开应用程序时的文件。
 *如果存在返回:真的,假的,如果它不
 * /
私人布尔checkDataBase(){
    SQLiteDatabase CHECKDB = NULL;
    尝试{
        字符串mypath中= DB_PATH + DB_NAME;
        CHECKDB = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);
    }赶上(SQLiteException E){
        //数据库开不存在。
    }
    如果(CHECKDB!= NULL){
        checkDB.close();
    }    返回CHECKDB!= NULL?真假;
}/ **
 *副本数据库从本地资产文件夹在刚才创建的空数据库
 *系统文件夹,从那里可以访问和处理。
 *这是通过转流的字节流进行。
 * * /
私人无效copyDataBase()抛出IOException    //打开本地数据库的输入流
    InputStream的myInput = myContext.getAssets()开(DB_NAME)。    //路径刚刚创建的空分贝
    字符串outFileName = DB_PATH + DB_NAME;    //打开空分贝的输出流
    的OutputStream myOutput =新的FileOutputStream(outFileName);    //传递从inputfile中字节到OUTPUTFILE
    字节[]缓冲区=新的字节[1024];
    INT长;
    而((长度= myInput.read(缓冲液))大于0){
        myOutput.write(缓冲液,0,长度);
    }    //关闭流
    myOutput.flush();
    myOutput.close();
    myInput.close();}公共无效的openDatabase()抛出的SQLException {
    //打开数据库
    字符串mypath中= DB_PATH + DB_NAME;
    MYDATABASE = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);
}@覆盖
公共同步无效的close(){
    如果(MYDATABASE!= NULL)
        myDataBase.close();
    super.close();
}@覆盖
公共无效的onCreate(SQLiteDatabase DB){
}@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
}//添加您的公开辅助方法访问并从数据库中获取的内容。
//你可以做返回游标返回myDataBase.query(....),因此这将会是很容易
//你创建你的意见适配器。
公开名单<问题> getQuestionSet(INT困难,诠释numQ){
    清单<问题> questionSet =新的ArrayList<问题>();
    光标C = myDataBase.rawQuery(SELECT * FROM WHERE问题难度=+ +困难
            ORDER BY RANDOM()LIMIT+ numQ,NULL);
    而(c.moveToNext()){
        //Log.d(\"QUESTION,问题DB中找到:+ c.getString(1));
        问Q =新问题();
        q.setQuestion(c.getString(1));
        q.setAnswer(c.getString(2));
        q.setOption1(c.getString(3));
        q.setOption2(c.getString(4));
        q.setOption3(c.getString(5));
        q.setRating(困难);
        questionSet.add(Q);
    }
    返回questionSet;
}

}

清单

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=com.pixy.quiz
安卓版code =1
机器人:=的versionName1.0><用途-SDK
    安卓的minSdkVersion =8
    机器人:targetSdkVersion =8/><应用
    机器人:allowBackup =真
    机器人:图标=@绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>
    <活动
        机器人:名字=com.pixy.quiz.SplashActivity
        机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
    <活动机器人:名字=。QuestionActivity/>
    <活动机器人:RulesActivityNAME = />
    <活动机器人:EndgameActivityNAME = />
    <活动机器人:SettingsActivityNAME = />
    <活动机器人:AnswersActivityNAME = />
< /用途>
<应用
机器人:allowBackup =真
机器人:名字=com.pixy.quiz.ChuckApplication/>
< /清单>


解决方案

请问您ChuckApplication扩展应用?

我认为你应该使用Singleton模式,如 ChuckApplication.getInstance()setCurrentGame(C);

Java Code: public class SplashActivity extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome);

    //////////////////////////////////////////////////////////////////////
    //////// GAME MENU  /////////////////////////////////////////////////
    Button playBtn = (Button) findViewById(R.id.playBtn);
    playBtn.setOnClickListener(this);
    Button settingsBtn = (Button) findViewById(R.id.settingsBtn);
    settingsBtn.setOnClickListener(this);
    Button rulesBtn = (Button) findViewById(R.id.rulesBtn);
    rulesBtn.setOnClickListener(this);
    Button exitBtn = (Button) findViewById(R.id.exitBtn);
    exitBtn.setOnClickListener(this);
}


/**
 * Listener for game menu
 */
@Override
public void onClick(View v) {
    Intent i;

    switch (v.getId()){
    case R.id.playBtn :
        //once logged in, load the main page
        //Log.d("LOGIN", "User has started the game");

        //Get Question set //
        List<Question> questions = getQuestionSetFromDb();

        //Initialise Game with retrieved question set ///
        GamePlay c = new GamePlay();
        c.setQuestions(questions);
        c.setNumRounds(getNumQuestions());
        ((ChuckApplication)getApplication()).setCurrentGame(c);

        //Start Game Now.. //
        i = new Intent(this, QuestionActivity.class);
        startActivityForResult(i, Constants.PLAYBUTTON);
        break;

    case R.id.rulesBtn :
        i = new Intent(this, RulesActivity.class);
        startActivityForResult(i, Constants.RULESBUTTON);
        break;

    case R.id.settingsBtn :
        i = new Intent(this, SettingsActivity.class);
        startActivityForResult(i, Constants.SETTINGSBUTTON);
        break;

    case R.id.exitBtn :
        finish();
        break;
    }

}


/**
 * Method that retrieves a random set of questions from
 * the database for the given difficulty
 * @return
 * @throws Error
 */
private List<Question> getQuestionSetFromDb() throws Error {
    int diff = getDifficultySettings();
    int numQuestions = getNumQuestions();
    DBHelper myDbHelper = new DBHelper(this);
    try {
        myDbHelper.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }
    try {
        myDbHelper.openDataBase();
    }catch(SQLException sqle){
        throw sqle;
    }
    List<Question> questions = myDbHelper.getQuestionSet(diff, numQuestions);
    myDbHelper.close();
    return questions;
}


/**
 * Method to return the difficulty settings
 * @return
 */
private int getDifficultySettings() {
    SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
    int diff = settings.getInt(Constants.DIFFICULTY, Constants.MEDIUM);
    return diff;
}

/**
 * Method to return the number of questions for the game
 * @return
 */
private int getNumQuestions() {
    SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
    int numRounds = settings.getInt(Constants.NUM_ROUNDS, 20);
    return numRounds;
}

}

when i hit the play button, the application stops. This button has to take to new activity to open a quiz qith 4 option radio button connected to a sqlite database.

Logcat messaege:

 `05-14 09:57:07.126: D/dalvikvm(19541): GC_CONCURRENT freed 189K, 12% free 2676K/3016K, paused 19ms+7ms, total 108ms
05-14 09:57:07.126: D/dalvikvm(19541): WAIT_FOR_CONCURRENT_GC blocked 47ms
05-14 09:57:07.486: I/Choreographer(19541): Skipped 55 frames!  The application may be doing too much work on its main thread.
05-14 09:57:07.516: D/gralloc_goldfish(19541): Emulator without GPU emulation detected.
05-14 09:57:10.116: D/AndroidRuntime(19541): Shutting down VM
05-14 09:57:10.116: W/dalvikvm(19541): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-14 09:57:10.146: E/AndroidRuntime(19541): FATAL EXCEPTION: main
05-14 09:57:10.146: E/AndroidRuntime(19541): java.lang.ClassCastException: android.app.Application cannot be cast to com.pixy.quiz.ChuckApplication
05-14 09:57:10.146: E/AndroidRuntime(19541):    at com.pixy.quiz.SplashActivity.onClick(SplashActivity.java:61)
05-14 09:57:10.146: E/AndroidRuntime(19541):    at android.view.View.performClick(View.java:4204)
05-14 09:57:10.146: E/AndroidRuntime(19541):    at android.view.View$PerformClick.run(View.java:17355)
05-14 09:57:10.146: E/AndroidRuntime(19541):    at android.os.Handler.handleCallback(Handler.java:725)
05-14 09:57:10.146: E/AndroidRuntime(19541):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-14 09:57:10.146: E/AndroidRuntime(19541):    at android.os.Looper.loop(Looper.java:137)
05-14 09:57:10.146: E/AndroidRuntime(19541):    at android.app.ActivityThread.main(ActivityThread.java:5041)
05-14 09:57:10.146: E/AndroidRuntime(19541):    at java.lang.reflect.Method.invokeNative(Native Method)
05-14 09:57:10.146: E/AndroidRuntime(19541):    at java.lang.reflect.Method.invoke(Method.java:511)
05-14 09:57:10.146: E/AndroidRuntime(19541):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-14 09:57:10.146: E/AndroidRuntime(19541):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-14 09:57:10.146: E/AndroidRuntime(19541):    at dalvik.system.NativeStart.main(Native Method)
05-14 09:57:13.797: I/Process(19541): Sending signal. PID: 19541 SIG: 9

`

My BDHelper Class:

public class DBHelper extends SQLiteOpenHelper{

//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.pixy.quiz/databases/";
private static String DB_NAME = "questionsDb";
private SQLiteDatabase myDataBase;
private final Context myContext;

/**
 * Constructor
 * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
 * @param context
 */
public DBHelper(Context context) {
    super(context, DB_NAME, null, 1);
    this.myContext = context;
}

/**
 * Creates a empty database on the system and rewrites it with your own database.
 * */
public void createDataBase() throws IOException{

    boolean dbExist = checkDataBase();
    if(!dbExist)
    {
        //By calling this method and empty database will be created into the default system path
        //of your application so we are gonna be able to overwrite that database with our database.
        this.getReadableDatabase();

        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

/**
 * Check if the database already exist to avoid re-copying the file each time you open the application.
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase(){
    SQLiteDatabase checkDB = null;
    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }catch(SQLiteException e){
        //database does't exist yet.
    }
    if(checkDB != null){
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

/**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException{
    //Open the database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}

@Override
public synchronized void close() {
    if(myDataBase != null)
        myDataBase.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.




public List<Question> getQuestionSet(int difficulty, int numQ){
    List<Question> questionSet = new ArrayList<Question>();
    Cursor c = myDataBase.rawQuery("SELECT * FROM question WHERE DIFFICULTY=" + difficulty +
            " ORDER BY RANDOM() LIMIT " + numQ, null);
    while (c.moveToNext()){
        //Log.d("QUESTION", "Question Found in DB: " + c.getString(1));
        Question q = new Question();
        q.setQuestion(c.getString(1));
        q.setAnswer(c.getString(2));
        q.setOption1(c.getString(3));
        q.setOption2(c.getString(4));
        q.setOption3(c.getString(5));
        q.setRating(difficulty);
        questionSet.add(q);
    }
    return questionSet;
}

}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pixy.quiz"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.pixy.quiz.SplashActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".QuestionActivity" />
    <activity android:name=".RulesActivity" />
    <activity android:name=".EndgameActivity" />
    <activity android:name=".SettingsActivity" />
    <activity android:name=".AnswersActivity" />
</application>
<application
android:allowBackup="true"
android:name="com.pixy.quiz.ChuckApplication"/>
</manifest>
解决方案

Does your ChuckApplication extends Application?

I think you should use a singleton pattern, as in ChuckApplication.getInstance().setCurrentGame(c);

这篇关于logcat的消息:错误打开跟踪文件:没有这样的文件或目录(2)谁能帮助我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 00:50