2013-03-29 54 views
2

我有創造的Jave 一個單獨的類我有這部分代碼存在問題轉換代碼PICE到

private class Read_from_db extends AsyncTask <String, Long, Void> { 
    private final ProgressDialog dialog = new ProgressDialog(Read.this); 
    // can use UI thread here 
    protected void onPreExecute() { 
    this.dialog.setMessage("Wait\nSome SLOW job is being done..."); 
    this.dialog.show(); 
    } 
    @Override 
    protected Void doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 
     try { 
      //txtMsg.append("\n"); 
      // obtain a list of from DB 
       String TABLE_NAME = "classicpoems__poet_header"; 
       String COLUMN_ID = "_id"; 
      // String _ID = "_id"; 
       String COLUMN_NAME = "poet_name"; 
       String COLUMN_CENTURY = "century_start"; 
       String [] columns ={COLUMN_ID,COLUMN_NAME,COLUMN_CENTURY}; 

      Cursor c = db.query(TABLE_NAME,columns,null, null, null, null, COLUMN_ID); 
      SimpleCursorAdapter adapter = new SimpleCursorAdapter(Read.this, R.layout.list_item, c, 
         new String[] {COLUMN_ID,COLUMN_NAME,COLUMN_CENTURY}, new int[] {R.id.list_item_text_id,R.id.list_item_text_main,R.id.list_item_text_sub}, 0); 

      ListView list = (ListView) findViewById(R.id.list_poet_name); 
      list.setAdapter(adapter); 

      } catch (Exception e) { 
       //Toast.makeText(Read.this, e.getMessage(), 1).show(); 
       Log.i(TAG, e.getMessage()); 
      } 
     db.close(); 
     return null; 
    } 
    // can use UI thread here 
    protected void onPostExecute(final Void unused) { 
    if (this.dialog.isShowing()) { 
    this.dialog.dismiss(); 
    } 
    // cleaning-up, all done 
    this.dialog.setMessage("Done"); 

    } 
} 

那一定每次都重複的活動負載(但也有一些變化,例如TABLE_NAMER.id.list_poet_namecolumns和..) 我認爲重複此代碼是不是一個專業的方式,所以我想將其轉換爲一個單獨的類,並在每個活動中使用它 但我不知道該怎麼做..(我試過,但我總是得到錯誤,例如我不知道如何定義上下文SimpleCursorAdapter 或使T這裏oast工作,你可以幫我如何將此代碼轉換爲一個單獨的類

這裏是我的類代碼

,當我想用​​它,我用這個代碼

enter code here Get_data poet_name=new Get_data(); 
     poet_name.TABLE_NAME="classicpoems__poet_header"; 
     poet_name.COLUMN_ID = "_id"; 
     poet_name.COLUMN_NAME = "poet_name"; 
     poet_name.COLUMN_CENTURY = "century_start"; 
     poet_name.columns =new String[]{"_id","poet_name","century_start"}; 
     poet_name.adapter_list=R.layout.list_item; 
     poet_name.target=R.id.list_poet_name; 
     poet_name.execute(); 

但是我得到錯誤..你能幫我找到我的錯誤嗎?

我改變了最初的代碼Get_data poet_name=new Get_data(this);但我還沒有在這裏得到錯誤的logcat中的一部分

01-06 04:25:32.262: E/AndroidRuntime(3244): FATAL EXCEPTION: main 
01-06 04:25:32.262: E/AndroidRuntime(3244): java.lang.RuntimeException: Unable to start activity ComponentInfo{co.tosca.persianpoem/co.tosca.persianpoem.Read}: java.lang.NullPointerException 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at android.app.ActivityThread.access$600(ActivityThread.java:127) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at android.os.Looper.loop(Looper.java:137) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at android.app.ActivityThread.main(ActivityThread.java:4441) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at dalvik.system.NativeStart.main(Native Method) 
01-06 04:25:32.262: E/AndroidRuntime(3244): Caused by: java.lang.NullPointerException 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at co.tosca.persianpoem.Get_data.onPreExecute(Get_data.java:54) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at android.os.AsyncTask.execute(AsyncTask.java:511) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at co.tosca.persianpoem.Read.onCreate(Read.java:65) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at android.app.Activity.performCreate(Activity.java:4465) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) 
01-06 04:25:32.262: E/AndroidRuntime(3244):  ... 11 more 
+0

您正在呼籲GET_DATA錯誤的初始化。使用此代碼(假設您正在從您的活動調用: Get_data poet_name = new Get_data(this); – HalR

+0

感謝我的朋友,但我仍然得到錯誤..我附上logcat代碼..我認爲我的課有問題..我是對的? – Majid

+0

看起來您在初始化活動時遇到問題,您需要調試您的初始化。除此之外,此代碼從不執行:public Get_data(){ TABLE_NAME =「classicpoems__poet_header」; COLUMN_ID =「_ id」; COLUMN_NAME =「poet_name」; } 因爲您不是調用該方法,而是在啓動應用程序時調用該方法。 – HalR

回答

1

你應該在自己的文件中創建一個單獨的類。使其公開,讓其他類可以看到它,並給它一些創建參數。

將調用Activity傳遞給該類,然後可以將該值用於需要當前Activity的Toast等調用。

public class Read_from_db extends AsyncTask <String, Long, Void> { 
    private final ProgressDialog dialog = new ProgressDialog(Read.this); 
    private Activity callingActivity; 
    public Read_from_db(Activity activity) { 
     callingActivity = activity; 
    } 


    // can use UI thread here 
    protected void onPreExecute() { 
    this.dialog.setMessage("Wait\nSome SLOW job is being done..."); 
    this.dialog.show(); 
    } 
    @Override 
    protected Void doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 
     try { 
      //txtMsg.append("\n"); 
      // obtain a list of from DB 
       String TABLE_NAME = "classicpoems__poet_header"; 
       String COLUMN_ID = "_id"; 
      // String _ID = "_id"; 
       String COLUMN_NAME = "poet_name"; 
       String COLUMN_CENTURY = "century_start"; 
       String [] columns ={COLUMN_ID,COLUMN_NAME,COLUMN_CENTURY}; 

      Cursor c = db.query(TABLE_NAME,columns,null, null, null, null, COLUMN_ID); 
      SimpleCursorAdapter adapter = new SimpleCursorAdapter(Read.this, R.layout.list_item, c, 
         new String[] {COLUMN_ID,COLUMN_NAME,COLUMN_CENTURY}, new int[] {R.id.list_item_text_id,R.id.list_item_text_main,R.id.list_item_text_sub}, 0); 

      ListView list = (ListView) findViewById(R.id.list_poet_name); 
      list.setAdapter(adapter); 

      } catch (Exception e) { 
       Toast.makeText(callingActivity, e.getMessage(), 1).show(); 
       Log.i(TAG, e.getMessage()); 
      } 
     db.close(); 
     return null; 
    } 
    // can use UI thread here 
    protected void onPostExecute(final Void unused) { 
    if (this.dialog.isShowing()) { 
    this.dialog.dismiss(); 
    } 
    // cleaning-up, all done 
    this.dialog.setMessage("Done"); 

    } 
} 
+0

感謝您的幫助..我用承包商和現在我可以使用當前的活動......但我有兩個問題第一'findViewById'是未定義的,第二如果我想改變我的活動'String []列'我可以如何定義它?我可以在doInBackground方法中創建一個新的方法? – Majid

+0

如果你傳遞一個活動到你的類,你可以使用callingActivity.findViewById() – HalR

+0

你也可以添加一個String []參數到你的類的創建 – HalR

0

(我希望我理解正確你的問題。) 這裏是我的建議:

使用具有SimpleCursorAdapter類型作爲參數的構造函數,從上面的代碼創建一個單獨的類。根據需要創建Read_from_db對象,其中SimpleCursorAdapter具有表和列值,並在doInBackground方法中使用相同的對象。

+0

感謝您的幫助,但對不起,我沒有得到你的意思.. :(你能解釋多一點:),謝謝 – Majid

0

您可以創建在的AsyncTask類的構造函數,你會通過上下文:

Context ctx; 
public Read_from_db(Context ctx) { 
    this.ctx = ctx; 
} 

上下文CTX是一個全球性的類變量,你可以在地方使用它Read.this

+0

感謝親愛的朋友,現在我可以使用上下文 – Majid

0

只要讓你的自定義類的構造器是這樣的:

public class Read_from_db extends AsyncTask<String, Long, Void> { 
Context context; 
private ProgressDialog dialog; 

public Read_from_db(Context context){ 
    this.context=context; 

} 

@Override 
protected void onPreExecute() { 
    dialog = ProgressDialog.show(context, "dialog title", "dialog message....."); 
} 



@Override 
protected Void doInBackground(String... arg0) { 
    // TODO Auto-generated method stub 

    //do your work here 


    return null; 
} 


protected void onPostExecute(final Void unused) { 
    dialog.dismiss(); 

} 
} 
+0

謝謝我解決了我的第一個問題,但現在我有'findviewbyid'這個未定義的問題:( – Majid

+0

右鍵單擊您的項目,然後單擊**生成項目**。這應該可以解決問題。 – captaindroid

+0

謝謝我解決了這個問題,但我有我的班級出現了一些問題......你可以再次看看第一篇文章嗎?我在那裏添加了一些日誌..可能你可以幫我解決我的新問題..謝謝 – Majid