[問題] android 計算平均問題

作者: andyzacks (杏仁)   2015-05-27 10:16:11
各位大大 請問一下我寫了一段程式但只要按下Button計算平均就會錯誤
程式碼在此
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private final static String CREATE_TABLE="CREATE TABLE EngScore(_id INTEGER PRIMARY KEY, studentName TEXT, score INTEGER)";
private Button but1;
private TextView txt1,txt2;
private ListView lv;
private String str,itemdata;
private SQLiteDatabase obj=null;
int n;
int i=0;
private Cursor cursor,cont,avg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but1 = (Button)findViewById(R.id.but1);
txt1 = (TextView)findViewById(R.id.textView1);
lv = (ListView)findViewById(R.id.listView1);
but1.setOnClickListener(lst);
txt2 = (TextView)findViewById(R.id.textView2);
obj = openOrCreateDatabase("db3.db",MODE_PRIVATE,null);
itemdata="the avg score is";
try{
obj.execSQL(CREATE_TABLE);
obj.execSQL("INSERT INTO EngScore (studentName,score) values ('Amy', 70)");
obj.execSQL("INSERT INTO EngScore (studentName,score) values ('Brian', 80)");
obj.execSQL("INSERT INTO EngScore (studentName,score) values ('John', 78)");
obj.execSQL("INSERT INTO EngScore (studentName,score) values ('Tina', 90)");
}catch(Exception e){
UpdateAdapter();
cont =obj.rawQuery("SELECT score FROM EngScore",null);
int d =cont.getCount();
Toast toast=Toast.makeText(MainActivity.this,
"表格已存在"+"共有"+d+"筆資料",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, -50);
toast.show();
};
}
private void UpdateAdapter() {
// TODO Auto-generated method stub
cursor =obj.rawQuery("SELECT * FROM EngScore",null);
if (cursor !=null && cursor.getCount() >=0){
SimpleCursorAdapter adapter =new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor,new String[]{"studentName","score"},new int[]{android.R.id.text1,android.R.id.text2},0);
lv.setAdapter(adapter);
}
}
protected void onDestroy(){
super.onDestroy();
obj.execSQL("DROP TABLE EngScore");
obj.close();
}
private Button.OnClickListener lst = new Button.OnClickListener (){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int total=0;
for(i=0;i<cursor.getCount();i++){
cursor.moveToNext();
total+=cursor.getInt(2);
};
int c=(total)/4;
txt2.setText(itemdata+c);
}
};
}
作者: agreerga (鴨毛)   2015-05-27 10:28:00
錯誤Log訊息也要貼上來
作者: issuemylove (NotLove)   2015-05-27 10:35:00
建議貼在置底程式碼網站上方便閱讀 裡會你的人也容易比較多唷!
作者: andyzacks (杏仁)   2015-05-27 10:57:00
http://codepad.org/1FxbPM5b 這裡 真是不好意思下次會注意的
作者: passli   2015-05-27 11:31:00
因為你create table完 並沒有去query 所以cursor 是null

Links booklink

Contact Us: admin [ a t ] ucptt.com