简体   繁体   English

android数据库活动中的java.lang.NullPointerException

[英]java.lang.NullPointerException in android database activity

I'm new in Android and I am creating an application. 我是Android的新手,正在创建一个应用程序。 But my I am receiving an error through logcat which says 但是我通过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

Any suggestions on what should I do? 有什么建议我该怎么办? I appreciate your help. 我感谢您的帮助。 Thank you. 谢谢。

XML file 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 file 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();
            }
        }
    }
    );
}
}

Manifest 表现

<?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>

The issue is that btnSaveCustomer is not initialized before usage. 问题是使用前未初始化btnSaveCustomer You should do something like 你应该做类似的事情

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

before calling 打电话之前

btnSaveCustomer.setOnClickListener();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM