0

我是 Android 新手,我正在创建一个应用程序。但是我通过 logcat 收到一个错误,上面写着

03-16 09:11:52.372: E/AndroidRuntime(283): FATAL EXCEPTION: main
03-16 09:11:52.372: E/AndroidRuntime(283): java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.example.workoutbuddy/com.example.workoutbuddy.DatabaseAddClient}: java.lang.NullPointerException

关于我应该怎么做的任何建议?我感谢您的帮助。谢谢你。

XML 文件

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/full_name"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="15dp"
        android:text="Full Name:" />

    <EditText
        android:id="@+id/fullname"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="text"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/txtGender"
                 android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="15dp"
        android:text="Gender" />

  <RadioGroup 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id ="@+id/gender">
         <RadioButton android:id="@+id/genderMale"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Male"
                 />
        <RadioButton android:id="@+id/genderFemale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female"
                />
    </RadioGroup>

    <TextView
        android:id="@+id/age"
                 android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="15dp"
        android:text="Age" />

    <EditText
        android:id="@+id/age_client"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="number" />

    <TextView
        android:id="@+id/address"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="15dp"
        android:text="Address" />

    <EditText
       android:id="@+id/address_client"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ems="10"
        android:lineSpacingExtra="15dp"
        android:textSize="15sp"
        android:paddingLeft="10dp"
        android:inputType="textMultiLine" />

    <TextView
        android:id="@+id/contactNum"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="15dp"
        android:text="Contact No." />

    <EditText
        android:id="@+id/contactNum_client"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="phone"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/height"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="15dp"
        android:text="Height (cm)" />

    <EditText
        android:id="@+id/height_client"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="number"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/weight"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="15dp"
        android:text="Weight (kg)" />

    <EditText
        android:id="@+id/weight_client"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="number"
        android:textSize="15sp" 
       />

       <TextView
        android:id="@+id/txtWorkoutType"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="15dp"
        android:text="Choose type of workout plan:" />

    <Spinner
        android:id="@+id/workout_type"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="15dp" />

    <TextView
        android:id="@+id/txtWorkoutDay"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="15dp"
        android:text="Choose workout days:" />

    <Spinner
        android:id="@+id/workout_day"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="15dp" />


    <Button
        android:id="@+id/next"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Next" 
        android:onClick = "workout_type"/>

</LinearLayout>

 </ScrollView>

Java 文件

package com.example.workoutbuddy;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
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;
import android.widget.Toast; 
import android.widget.RadioButton;
import android.widget.RadioGroup;

@SuppressWarnings("unused")
public class DatabaseAddClient extends Activity {

private SQLiteDatabase db;
private ClientDBHelper dh;

private EditText fullname;
private EditText address_client;
private RadioGroup gender;
private RadioButton tbxClientGender;
private EditText contactNum_client;
private EditText height_client;
private EditText weight_client;
private EditText age_client;

private Button btnSaveCustomer;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addclient);

    fullname = (EditText) findViewById(R.id.fullname);

    gender = (RadioGroup) findViewById(R.id.gender);
    int selectedId = gender.getCheckedRadioButtonId();
    tbxClientGender = (RadioButton) findViewById(selectedId);

    age_client = (EditText) findViewById(R.id.age_client);
    address_client = (EditText) findViewById(R.id.address_client);
    contactNum_client = (EditText) findViewById(R.id.contactNum_client);
    height_client = (EditText) findViewById(R.id.height_client);
    weight_client = (EditText) findViewById(R.id.weight_client);
    dh = new ClientDBHelper(this);

   btnSaveCustomer.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

        //edited portion starts here----    
        String heightString = height_client.getText().toString();
            final double heightDouble = Double.parseDouble(heightString);

            String weightString = weight_client.getText().toString();
            final double weightDouble = Double.parseDouble(heightString);

            final double body_mass = heightDouble / weightDouble;

            String status = null;

            if (body_mass < 18.5){
                status = "UNDERWEIGHT";
            } else if (body_mass == 18.5 && body_mass <=24.9){
                status = "NORMAL";
            }else if (body_mass == 25 && body_mass <=29.9){
                status = "OVERWEIGHT";
            }else if (body_mass >=30){
                status = "OBESE";
            }

            final String finalStatus = status;
        // ---- edited portion ends here

            if (fullname.getText().length() == 0) {
                Toast.makeText(DatabaseAddClient.this, "Name is empty", Toast.LENGTH_LONG).show();
                fullname.requestFocus();

            } else if (age_client.getText().length() == 0) {
                Toast.makeText(DatabaseAddClient.this, "Age is empty", Toast.LENGTH_LONG).show();
                age_client.requestFocus();

            } else if (address_client.getText().length() == 0) {
                Toast.makeText(DatabaseAddClient.this, "Address is empty", Toast.LENGTH_LONG).show();
                address_client.requestFocus();

            } else if (contactNum_client.getText().length() == 0) {
                Toast.makeText(DatabaseAddClient.this, "Contact number is empty", Toast.LENGTH_LONG).show();
                contactNum_client.requestFocus();

            } else if (height_client.getText().length() == 0) {
                Toast.makeText(DatabaseAddClient.this, "Height is empty", Toast.LENGTH_LONG).show();
                height_client.requestFocus();

            } else if (weight_client.getText().length() == 0) {
                Toast.makeText(DatabaseAddClient.this, "Weight is empty", Toast.LENGTH_LONG).show();
                weight_client.requestFocus();

            } else {
                ProgressDialog progDialog = ProgressDialog.show(DatabaseAddClient.this, 
                     "Saving Customer", "Saving customer to database...", true);



                String new_client_fullname = fullname.getText().toString();
                String new_client_age = age_client.getText().toString();
                String new_client_address = address_client.getText().toString();
                String new_client_contactnumber = contactNum_client.getText().toString();
                String new_client_height = height_client.getText().toString();
                String new_client_weight = weight_client.getText().toString();
                String new_client_gender = tbxClientGender.getText().toString();
                String new_client_bmi = String.valueOf(body_mass);
    String new_client_status = String.valueOf(finalStatus);



                db = dh.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put(ClientDBHelper.FULLNAME, new_client_fullname);
                values.put(ClientDBHelper.GENDER, new_client_gender);
                values.put(ClientDBHelper.AGE, new_client_age);
                values.put(ClientDBHelper.ADDRESS, new_client_address);
                values.put(ClientDBHelper.CONTACT_NUM, new_client_contactnumber);
                values.put(ClientDBHelper.HEIGHT, new_client_height);
                values.put(ClientDBHelper.WEIGHT, new_client_weight);
                values.put(ClientDBHelper.BMI, new_client_bmi);
                values.put(ClientDBHelper.STATUS, finalStatus);


                db.insert(ClientDBHelper.TABLE_NAME, null, values);
                db.close();

                progDialog.dismiss();

                Toast.makeText(DatabaseAddClient.this, "Customer " 
                        + new_client_fullname + " has been added successfully", 
                        Toast.LENGTH_LONG).show();
                finish();
            }
        }
    }
    );
}
}

显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.workoutbuddy"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="10" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.workoutbuddy.DatabaseActivity"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
   <activity
        android:name="com.example.workoutbuddy.DatabaseAddClient"
        android:label="@string/app_name">

    </activity>
     <activity
        android:name="com.example.workoutbuddy.DatabaseEditClient"
        android:label="@string/app_name">

    </activity>
    <activity
        android:name="com.example.workoutbuddy.DatabaseViewClient"
        android:label="@string/app_name">

    </activity>
<activity
        android:name="com.example.workoutbuddy.OwnCalendar"
        android:label="@string/app_name"
        >

    </activity>
</application>

</manifest>
4

1 回答 1

0

问题是btnSaveCustomer在使用前没有初始化。你应该做类似的事情

btnSaveCustomer = (Button)findViewById(R.id.next);

打电话之前

btnSaveCustomer.setOnClickListener();
于 2013-03-16T03:00:18.320 回答