2012-11-28 92 views
-1

我編輯過源代碼,但我有錯誤,並且我不明白在哪裏,爲什麼會發生錯誤,這個的源代碼的類LineGraph.java導致:java.lang.InstantiationException,但我不知道如何解決,爲什麼

公共類LineGraph延伸活動{

public static final String ROW_ID = "id"; 
public static final String ROW_NAMA = "nama"; 
public static final String ROW_BERAT = "berat"; 

public static final String DATABASE_NAME = "tes.sqlite"; 
public static final String DATABASE_TABLE = "tis"; 
public static final int DATABASE_VERSION = 1; 

public SQLiteDatabase database; 
public DBHelper dbhelper; 
public final Context kontext; 


public static class DBHelper extends SQLiteOpenHelper { 

    public DBHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + ROW_ID 
       + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ROW_NAMA 
       + " TEXT NOT NULL, " + ROW_BERAT + " TEXT NOT NULL);"); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
     onCreate(db); 
    } 

} 

public LineGraph(Context c) { 
    kontext = c; 
} 

public LineGraph open() { 
    dbhelper = new DBHelper(kontext); 
    database = dbhelper.getWritableDatabase(); 
    return this; 
} 

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

public long createEntry(String data, String date) { 
    // , String date 
    // TODO Auto-generated method stub 
    ContentValues cv = new ContentValues(); 
    cv.put(ROW_NAMA, data); 
    cv.put(ROW_BERAT, date); 
    return database.insert(DATABASE_TABLE, null, cv); 
} 

public String getBMIID() { 
    // TODO Auto-generated method stub 
    String[] column = new String[] { ROW_ID }; 
    Cursor c = database.query(DATABASE_TABLE, column, null, null, null, 
      null, null); 

    String result = ""; 
    int iID = c.getColumnIndex(ROW_ID); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
     result = result + c.getString(iID); 
    } 

    return result; 
} 

public String getBMIDataData() { 

    String[] column = new String[] { ROW_NAMA }; 
    Cursor c = database.query(DATABASE_TABLE, column, null, null, null, 
      null, null); 

    String result = ""; 
    int iData = c.getColumnIndex(ROW_NAMA); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
     result = result + c.getString(iData); 
    } 

    return result; 
} 

public String getBMIDateData() { 
    String[] column = new String[] { ROW_BERAT }; 
    Cursor c = database.query(DATABASE_TABLE, column, null, null, null, 
      null, null); 

    String result = ""; 
    int iDate = c.getColumnIndex(ROW_BERAT); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
     result = result + c.getString(iDate); 
    } 

    return result; 
} 

public void updateEntry(long lId, String mData, String mDate) { 
    // TODO Auto-generated method stub 
    ContentValues cvUpdate = new ContentValues(); 

    cvUpdate.put(ROW_NAMA, mData); 
    cvUpdate.put(ROW_BERAT, mDate); 
    database.update(DATABASE_TABLE, cvUpdate, ROW_ID + " = lId", null); 
} 

public String getData(long l) { 
    // TODO Auto-generated method stub 
    return null; 
} 

public String getDate(long l) { 
    // TODO Auto-generated method stub 
    return null; 
} 

public XYMultipleSeriesDataset getDemoDataset(String title) { 

    String[] column = new String[] { ROW_NAMA }; 
    Cursor c = dbhelper.getWritableDatabase().query(DATABASE_TABLE, 
      column, null, null, null, null, null); 

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); 

    TimeSeries series = new TimeSeries("Line1"); 
    TimeSeries series2 = new TimeSeries(title); 

    getBMIDataData(); 

    while (!c.isAfterLast()) { 
     int date = c.getInt((Integer) c.getColumnIndexOrThrow("bmi_date")); 
     int weight = c 
       .getInt((Integer) c.getColumnIndexOrThrow("bmi_data")); 
     series2.add(weight, date); 
     c.moveToNext(); 
    } 

    c.close(); 

    dataset.addSeries(series); 
    dataset.addSeries(series2); 

    return dataset; 
} 

public Intent getIntent(Context context) { 

    // Lager TimeSeries for den første linja 
    XYMultipleSeriesDataset dataset = getDemoDataset("Line1"); 

    // Kode for render 
    XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); 

    // Optimalisering linje1 
    XYSeriesRenderer renderer = new XYSeriesRenderer(); 
    renderer.setColor(Color.YELLOW); 
    renderer.setPointStyle(PointStyle.CIRCLE); 
    renderer.setFillPoints(true); 

    // Optimalisering linje2 husk rekke følgen 
    XYSeriesRenderer renderer2 = new XYSeriesRenderer(); 
    renderer2.setColor(Color.BLUE); 
    renderer2.setPointStyle(PointStyle.SQUARE); 
    renderer2.setFillPoints(true); 

    // Legger til render seriene 
    mRenderer.addSeriesRenderer(renderer); 

    // Optimalisering grafen 
    mRenderer.setChartTitle("Test"); 
    mRenderer.setZoomEnabled(true); 
    mRenderer.setZoomButtonsVisible(true); 
    mRenderer.setBackgroundColor(Color.BLACK); 
    mRenderer.setApplyBackgroundColor(true); 
    mRenderer.setXTitle("Dager"); 
    mRenderer.setShowGrid(true); 

    mRenderer.addSeriesRenderer(renderer2); 

    Intent intent = ChartFactory.getLineChartIntent(context, dataset, 
      mRenderer, "Line Graph Title"); 

    return intent; 

} 

}

,這是logcat的

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{android.graph/android.graph.LineGraph}: java.lang.InstantiationException: android.graph.LineGraph 

Caused by: java.lang.InstantiationException: android.graph.LineGraph 

有人可以幫助我嗎?

+0

發佈fullstacktrace – njzk2

+0

您是否在AndroidManifest.xml中聲明瞭此活動?如果你這樣做,什麼是'android:name'值? – Wenhui

+0

你已經提供了這個類的構造函數。不要這樣做。這些實例的構建由Android爲您處理。 –

回答

1

請勿替換Activity的構造函數。每個Activity被系統實例化,它需要有一個public無參數的構造函數。

一旦你告訴你要啓動的系統LineGraphActivity它會嘗試做new LineGraph()。在此之後,系統設置Context和所有需要的狀態您Activity。一旦完成,它將調用onCreate方法。

你可以做這樣的

public class LineGraph extends Activity { 
    public LineGraph() { 
     super(); 
     // you could do init here, but you better don't 
    } 
} 

但我會建議不要這樣做,因爲你沒有Context在從Activity這一點,並沒有什麼作品呢。

onCreate中做。

在旁註:將類似DBHelper這樣的類放在自己的文件中通常比將它們包含在大型代碼段中更好。因爲它已經處理了數據庫訪問(或者將數據庫訪問部分提取到它自己的類文件中),所以如果你不使LineGraphActivity也是一個好主意。儘量保持每個對象的責任。

+0

我還是不明白,你可以舉個例子?意味着我必須在DBHelper和線圖之間分開? –

+0

@RinkuTriadi你不能做任何事情。如果每個類都有[單一責任](http://codebetter.com/karlseguin/2008/12/05/get-solid-single-responsibility-principle/)(以及它自己的文件),這只是一種更簡潔的代碼風格。例如,將數據訪問從「Activity」中分離出來,如果您想從另一個「Activity」訪問同一個數據庫,則會更容易。 – zapl

相關問題