2012-03-07 4 views
0

listview-> listview-> listview-> table layout -> 등을 설정하기 위해 액티비티를 설정했습니다. 응용 프로그램을 탐색 할 때 멋지게 작동 할 때. 그러나 프로그램이 무작위로 충돌 할 때까지는 단지 시간 문제 일뿐입니다. 이 문제처럼 보이는 로그 캣에서탐색하는 동안 무작위 충돌이 발생했습니다.

03-06 21:12:29.350: W/dalvikvm(7132): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
03-06 21:12:29.350: E/AndroidRuntime(7132): Uncaught handler: thread main exiting due to uncaught exception 
03-06 21:12:29.370: E/AndroidRuntime(7132): java.lang.NullPointerException 
03-06 21:12:29.370: E/AndroidRuntime(7132):  at wanted.pro.madlibs.title.onListItemClick(title.java:55) 
03-06 21:12:29.370: E/AndroidRuntime(7132):  at android.app.ListActivity$2.onItemClick(ListActivity.java:312) 
03-06 21:12:29.370: E/AndroidRuntime(7132):  at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
03-06 21:12:29.370: E/AndroidRuntime(7132):  at android.widget.ListView.performItemClick(ListView.java:3285) 
03-06 21:12:29.370: E/AndroidRuntime(7132):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640) 
03-06 21:12:29.370: E/AndroidRuntime(7132):  at android.os.Handler.handleCallback(Handler.java:587) 
03-06 21:12:29.370: E/AndroidRuntime(7132):  at android.os.Handler.dispatchMessage(Handler.java:92) 
03-06 21:12:29.370: E/AndroidRuntime(7132):  at android.os.Looper.loop(Looper.java:123) 
03-06 21:12:29.370: E/AndroidRuntime(7132):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
03-06 21:12:29.370: E/AndroidRuntime(7132):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-06 21:12:29.370: E/AndroidRuntime(7132):  at java.lang.reflect.Method.invoke(Method.java:521) 
03-06 21:12:29.370: E/AndroidRuntime(7132):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
03-06 21:12:29.370: E/AndroidRuntime(7132):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
03-06 21:12:29.370: E/AndroidRuntime(7132):  at dalvik.system.NativeStart.main(Native Method) 
03-06 21:12:29.380: I/dalvikvm(7132): threadid=7: reacting to signal 3 
03-06 21:12:29.380: E/dalvikvm(7132): Unable to open stack trace file '/data/anr/traces.txt': Permission denied 

내 onlistitemclick입니다. 나는 왜 그것이 잠시 동안 그리고 그 다음에 효과가 없을 지 모르지만. 아래는 내가 listviews 중 하나에 사용하고있는 코드입니다. 충돌의 원인이 시도

public class title extends ListActivity { 
    private PopupWindow pw; 
    public static int titleClick; 
    private dbadapter mydbhelper; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.category_list); 
     mydbhelper = new dbadapter(this); 
     mydbhelper.open(); 
     fillData(); 
    } 
    private void fillData() { 

     Cursor t = mydbhelper.getTitles(); 
      startManagingCursor(t); 

    // Create an array to specify the fields we want to display in the list (TITLE,DATE,NUMBER) 
     String[] from = new String[] {dbadapter.KEY_TITLEDESC}; 

     // an array of the views that we want to bind those fields to (in this case text1,text2,text3) 
     int[] to = new int[] {R.id.text1}; 

     // Now create a simple cursor adapter and set it to display 
     SimpleCursorAdapter adapter = 
      new SimpleCursorAdapter(this, R.layout.cate_row, t, from, to); 
     setListAdapter(adapter); 
     } 
@Override 
protected void onListItemClick(ListView list, View v, int position, long id) 
{ 
    super.onListItemClick(list, v, position, id); 
    titleClick = (int) list.getPositionForView(v); 
    final Intent intent = new Intent(this, inputpage.class); 
    final MediaPlayer titleClickSound = MediaPlayer.create(this, R.raw.button50); 
    titleClickSound.start(); 
    startActivityForResult(intent, position); 
    } 
@Override 
protected void onResume() { 
    if (mydbhelper != null) { 
     mydbhelper.open();}; 
    super.onResume(); 
} 

@Override 
protected void onPause() { 
    mydbhelper.close(); 
    super.onPause(); 
} 
//Menu Items 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.layout.menu, menu); 
    return true; 
} 

public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle item selection 
    switch (item.getItemId()) { 
     case R.id.help: 
      showHelp(); 
      return true; 
     } 
    return false; 
} 

    private void showHelp() { 
     LayoutInflater inflater = (LayoutInflater) 
        this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       pw = new PopupWindow(
        inflater.inflate(R.layout.help, null, false), 
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 
        true); 
       pw.showAtLocation(this.findViewById(R.id.text1), Gravity.CENTER, 0, 0); 
    } 
    public void onClickHelp(View helper){pw.dismiss(); 

    } 
} 

다시 이번에는 나에게 내가이 누출을 중지하려고했는데

03-06 21:52:40.250: E/Database(15818): Leak found 
03-06 21:52:40.250: E/Database(15818): java.lang.IllegalStateException: /data/data/wanted.pro.madlibs/databases/madlib SQLiteDatabase created and never closed 
03-06 21:52:40.250: E/Database(15818): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1695) 
03-06 21:52:40.250: E/Database(15818): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:739) 
03-06 21:52:40.250: E/Database(15818): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:761) 
03-06 21:52:40.250: E/Database(15818): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:754) 
03-06 21:52:40.250: E/Database(15818): at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:473) 
03-06 21:52:40.250: E/Database(15818): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193) 
03-06 21:52:40.250: E/Database(15818): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98) 
03-06 21:52:40.250: E/Database(15818): at wanted.pro.madlibs.dbadapter.open(dbadapter.java:176) 
03-06 21:52:40.250: E/Database(15818): at wanted.pro.madlibs.title.onCreate(title.java:29) 
03-06 21:52:40.250: E/Database(15818): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
03-06 21:52:40.250: E/Database(15818): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 
03-06 21:52:40.250: E/Database(15818): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
03-06 21:52:40.250: E/Database(15818): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
03-06 21:52:40.250: E/Database(15818): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
03-06 21:52:40.250: E/Database(15818): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-06 21:52:40.250: E/Database(15818): at android.os.Looper.loop(Looper.java:123) 
03-06 21:52:40.250: E/Database(15818): at android.app.ActivityThread.main(ActivityThread.java:4363) 
03-06 21:52:40.250: E/Database(15818): at java.lang.reflect.Method.invokeNative(Native Method) 
03-06 21:52:40.250: E/Database(15818): at java.lang.reflect.Method.invoke(Method.java:521) 
03-06 21:52:40.250: E/Database(15818): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
03-06 21:52:40.250: E/Database(15818): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
03-06 21:52:40.250: E/Database(15818): at dalvik.system.NativeStart.main(Native Method) 

다른 오류를 주었다. 내가 가지고 있다고 생각했으나 추측하지는 않았다. 온 클릭의 INT가 dbhelperclass의 하단에 커서에 사용되기 때문에 누출 문제의 원인이 될 수 있는지 궁금

public class dbadapter extends SQLiteOpenHelper { 

    //The Android's default system path of your application database. 
    public static final String DB_PATH = "/data/data/wanted.pro.madlibs/databases/"; 
    public static final String DB_NAME = "madlib"; 
    public static final int DB_VERSION = 39; 


    //database variables 
    public static final String KEY_ID = "_id"; 
    public static final String KEY_CATEGORYDESC = "categorydesc"; 
    public static final String KEY_TITLE = "titlekey"; 
    public static final String KEY_TITLEDESC = "titledesc"; 
    public static final String KEY_TITLESTORY = "titlestory"; 
    public static final String KEY_SOURCEDESC = "sourcedesc"; 
    public static final String KEY_SOURCE = "sourcekey"; 
    public static final String KEY_CATEGORY = "categorykey"; 
    public static final String KEY_USERWORD = "userword"; 
    public static final String KEY_EDITWORD = "editword"; 
    public static final String KEY_QUICK = "quick"; 

    //table variables 
    public static final String CATEGORY_TABLE = "category"; 
    public static final String SOURCE_TABLE = "source"; 
    public static final String TITLE_TABLE = "title"; 
    public static final String SOURCE_CATEGORY_TABLE = "source_category"; 
    public static final String SOURCE_TITLE_TABLE = "source_title"; 
    public static final String CATEGORY_TITLE_TABLE = "category_title"; 
    public static final String USER_WORD_TABLE = "userword"; 

    private dbadapter mydbhelper; 
    public static SQLiteDatabase myDataBase; 

    /** 
    * Constructor 
    * Takes and keeps a reference of the passed context in order to access to the application assets and resources. 
    * @param context 
    */ 

     private final Context mCtx; 
    public dbadapter(Context context) { 
    super(context, DB_NAME, null, DB_VERSION); 
     this.mCtx = 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){ 
     Log.v("DB Exists", "db exists"); 
     this.getWritableDatabase(); 
     //do nothing - database already exist 
    } 
    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 transferring bytestream. 
    * */ 
    private void copyDataBase() throws IOException{ 

    //Open your local db as the input stream 
    InputStream myInput = mCtx.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; 
     this.getWritableDatabase(); 
     myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 

    } 

    @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) { 
     if (newVersion > oldVersion) 
     mCtx.deleteDatabase(DB_NAME); 
     } 

    public dbadapter open() throws SQLException { 
     mydbhelper = new dbadapter (mCtx); 
     myDataBase = mydbhelper.getWritableDatabase(); 
     return this; 
    } 


    // retrieves all the categories 
     public Cursor getAllCategories() 
     { 
      return myDataBase.query(CATEGORY_TABLE, new String[] { 
        KEY_ID, KEY_CATEGORY, 
        KEY_CATEGORYDESC, 
        }, 
        null, null, null, null, KEY_CATEGORYDESC); 

     } 
    // retrieves all the titles 
      public Cursor getTitles() 
      { 
       return myDataBase.query(TITLE_TABLE, new String[] { 
         KEY_ID, 
         KEY_TITLE, 
         KEY_TITLEDESC, 
         KEY_TITLESTORY, KEY_CATEGORY, KEY_SOURCE, 
         }, 
         KEY_CATEGORY+ "=" + categories.categoryClick + " AND " + KEY_SOURCE+ "=" +source.sourceClick, null, null, null, KEY_TITLEDESC); 

      } 
     // retrieves all the descriptions for the edittext fields 
      public Cursor getUserWord() 
      { 
       return myDataBase.query(USER_WORD_TABLE, new String[] { 
         KEY_ID, 
         KEY_CATEGORY, 
         KEY_SOURCE, KEY_TITLE, KEY_USERWORD, KEY_QUICK 
         }, 
         KEY_CATEGORY+ "=" + categories.categoryClick + " AND " + KEY_SOURCE+ "=" 
         +source.sourceClick + " AND " + KEY_TITLE+ "=" + title.titleClick, 
         null, null, null, KEY_ID); 

      } 
      // Quick Start 
      public Cursor getQuickStory() 
      { 
       return myDataBase.query(TITLE_TABLE, new String[] { 
         KEY_ID, 
         KEY_TITLE, 
         KEY_TITLEDESC, 
         KEY_TITLESTORY, KEY_CATEGORY, KEY_SOURCE, 
         }, 
         KEY_ID+ "=" + main.RandomNum, null, null, null, null); 

      } 

      public Cursor getQuickWord() 
      { 
       return myDataBase.query(USER_WORD_TABLE, new String[] { 
         KEY_ID, 
         KEY_CATEGORY, 
         KEY_SOURCE, KEY_TITLE, KEY_USERWORD, KEY_QUICK 
         }, 
         KEY_QUICK+ "=" + main.RandomNum, 
         null, null, null, KEY_ID); 

      } 

      public Cursor getSource() 
      { 
      return myDataBase.query(SOURCE_TABLE, new String[]{ 
        KEY_ID, KEY_SOURCEDESC, KEY_CATEGORY,}, 
        KEY_CATEGORY+ "=" + categories.categoryClick, null, null, null, KEY_SOURCEDESC); 
      } 

      // retrieves story to diplay in last activity 
      public Cursor getStory() 
      { 
       return myDataBase.query(TITLE_TABLE, new String[] { 
         KEY_ID, 
         KEY_TITLE, 
         KEY_TITLEDESC, 
         KEY_TITLESTORY, KEY_CATEGORY, KEY_SOURCE, 
         }, 
         KEY_CATEGORY+ "=" + categories.categoryClick + " AND " + KEY_SOURCE+ "=" +source.sourceClick + " AND " + KEY_TITLE+ "=" + title.titleClick, null, null, null, null); 

      } 


} 
+0

라인을 보면'최종 의도 의도 = 새로운 의도 (이, inputpage.class을)','inputpage 것입니다 '? 정의 된 부분은 어디입니까? – gobernador

+0

예외에 따르면 원인은 wanted.pro.madlibs.title.onListItemClick (title.java:55)입니다. 어떤 코드를 공유 했나요? – elevine

+0

입력 페이지가 클래스이고 onlistitemclick이 있습니다. – maebe

답변

0

나는 이것이 당신의 예외 여부의 원인인지 모르겠지만 다음과 같이 보인다 그것은 당신에게 어떤 문제를 일으킬 수 있습니다. 당신은 정의의 다음

public static int titleClick; 

당신의 onListItemClick 당신이 전화 :

titleClick = (int) list.getPositionForView(v); 

나는이 공용 정적 변수는 응용 프로그램에서 다른 활동에서 액세스되고 있음을 가정한다. Android가 Activity을 관리하는 방식으로 인해 (흐름도 here 참조), 응용 프로그램의 프로세스가 종료되었다가 다시 시작될 경우이 정적 변수가 null/undefined가 될 수 있습니다. 또한 정적 변수가 에 액세스 한 후onListItemClick이 호출되면 null이됩니다. 또한

, 당신은 일을 대신 onListItemClick에서 position 매개 변수를 사용할 수 있어야합니다 : (int) list.getPositionForView(v)

+0

내 생각에 그 변수와 함께 있었고 뭔가를 쓰기/액세스 몇 번 후에 일어난 일입니다. 위치 매개 변수를 대신 사용해 보겠습니다. titleClick을 사용하여 내가 수행하는 작업은 커서를 필터링하여 항목을 데이터베이스에서 가져 와서 목록에서 선택된 항목을 기반으로 결과를 표시하는 것입니다. – maebe

+0

title.java에서 55 번째 줄을 알려주시겠습니까? 그것은 또 다른 단서를 제공해야합니다. – elevine

+0

정적 변수를 사용하지 않고 작업을 수행 할 것을 제안합니다. 대신 putExtra 및 getExtras 메서드를 사용하여 Intent 객체를 통해 정보를 전달할 수 있습니다. – elevine

관련 문제