0

好的,所以我有 3 页 AddStatsForm.Java 将数据插入数据库。它所说的主要问题是 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>
4

1 回答 1

0

一般来说,如果您希望从 XML 文件本身运行特定的 onClick。

它将在调用活动中查找具有该名称的函数。

在活动中,您可以拥有以下功能:

public void submitButtonClicked(View view) {
    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();
}

至于你的崩溃,它有点像多项选择题,因为我们没有崩溃日志。但是,如果您的任何 EditText 名称错误,例如这个:

username = (EditText) findViewById(R.id.etUsername); (SHOULD BE R.id.etName)

你会崩溃。您应该设置一个断点并逐步检查它们以确保它们都被 findViewById 找到。

于 2013-04-27T21:59:53.787 回答