わかりましたので、データをデータベースに挿入する AddStatsForm.Java の 3 ページがあります。それが言う主な問題は、AddStatsForm 内の「onClick」です。送信ボタンを押すと、NullPointerException と言ってアプリケーションがクラッシュします。
AddStatsForm.Java
package com.gofitness.www;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AddStatsForm extends Activity implements OnClickListener {
EditText username, age, weight,height, calories, protein, carbs, fat;
TextView success;
Button submit;
@Override
protected void onCreate(Bundle login) {
// TODO Auto-generated method stub
super.onCreate(login);
setContentView(R.layout.stats);
submit = (Button) findViewById(R.id.bEnter);
username = (EditText) findViewById(R.id.etUsername);
age = (EditText) findViewById(R.id.etAge);
weight = (EditText) findViewById(R.id.etWeight);
height = (EditText) findViewById(R.id.etHeight);
calories = (EditText) findViewById(R.id.etCalories);
protein = (EditText) findViewById(R.id.etProtein);
carbs = (EditText) findViewById(R.id.etCarbs);
fat = (EditText) findViewById(R.id.etFat);
success = (TextView) findViewById(R.id.tvSuccess);
submit.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()){
case R.id.bEnter:
String usernamevar = username.getText().toString();
String agevar = age.getText().toString();
String weightvar = weight.getText().toString();
String heightvar = height.getText().toString();
String caloriesvar = calories.getText().toString();
String proteinvar = protein.getText().toString();
String carbsvar = carbs.getText().toString();
String fatvar = fat.getText().toString();
AddStats entry = new AddStats(AddStatsForm.this);
entry.open();
entry.createEntry(usernamevar ,agevar, weightvar, heightvar, caloriesvar, proteinvar, carbsvar, fatvar);
entry.close();
break;
}}}
AddStats.java
package com.gofitness.www;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class AddStats {
public static final String KEY_ROWID = "userid";
public static final String KEY_NAME = "username";
public static final String KEY_WEIGHT = "weight";
public static final String KEY_HEIGHT = "height";
public static final String KEY_AGE = "age";
public static final String KEY_CALORIES = "calories";
public static final String KEY_PROTEIN = "protein";
public static final String KEY_CARBS = "carbs";
public static final String KEY_FATS = "fats";
private static final String DATABASE_NAME = "gofitness";
private static final String DATABASE_TABLE = "stats";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
Context ourContext;
private SQLiteDatabase ourDatabase;
private 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 + "(" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_AGE + " INT NOT NULL, " +
KEY_CALORIES + " INT NOT NULL, " +
KEY_WEIGHT + " INT NOT NULL, " +
KEY_HEIGHT + " INT NOT NULL, " +
KEY_PROTEIN + " INT NOT NULL, " +
KEY_FATS + " INT NOT NULL, " +
KEY_CARBS + " INT 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 AddStats(Context c)
{
ourContext = c;
}
public AddStats open(){
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String usernamevar, String agevar,String weightvar,String heightvar,String caloriesvar,
String proteinvar, String carbsvar, String fatvar) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME,usernamevar);
cv.put(KEY_AGE,agevar);
cv.put(KEY_WEIGHT,weightvar);
cv.put(KEY_HEIGHT,heightvar);
cv.put(KEY_CALORIES,caloriesvar);
cv.put(KEY_PROTEIN,proteinvar);
cv.put(KEY_CARBS,carbsvar);
cv.put(KEY_FATS,fatvar);
return ourDatabase.insert(DATABASE_TABLE,null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_AGE,KEY_WEIGHT,KEY_HEIGHT, KEY_CALORIES,KEY_PROTEIN,KEY_CARBS,KEY_FATS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iUser = c.getColumnIndex(KEY_NAME);
int iAge = c.getColumnIndex(KEY_AGE);
int iWeight = c.getColumnIndex(KEY_WEIGHT);
int iHeight = c.getColumnIndex(KEY_HEIGHT);
int iCalories = c.getColumnIndex(KEY_CALORIES);
int iProtein = c.getColumnIndex(KEY_PROTEIN);
int iCarb = c.getColumnIndex(KEY_CARBS);
int iFat = c.getColumnIndex(KEY_FATS);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iUser) + " " + c.getString(iAge) + c.getString(iWeight)
+ c.getString(iHeight) + c.getString(iCalories)+ c.getString(iProtein) + c.getString(iCarb) + c.getString(iFat) +"\n";
}
return result;
}
}
そして最後に stats.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
> </EditText>
<EditText
android:id="@+id/etAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
> </EditText>
<EditText
android:id="@+id/etWeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
> </EditText>
<EditText
android:id="@+id/etHeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
> </EditText>
<EditText
android:id="@+id/etCalories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
> </EditText>
<EditText
android:id="@+id/etProtein"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
> </EditText>
<EditText
android:id="@+id/etCarbs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
> </EditText>
<EditText
android:id="@+id/etFat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
> </EditText>
<Button
android:id="@+id/bEnter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/submit"
/>
<TextView
android:id="@+id/tvSuccess"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/submit"
></TextView>
</LinearLayout>