空指针异常错误:android中的数据库

时间:2011-04-04 04:04:08

标签: android

package com.andrd.gps;

import com.android.util.Utils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class EditUserDetailActivity extends Activity{

    String strTruckType, strTruckPermit, strEmploymentType;
    EditText driverNameEdtxt,ageEdtxt,addressEdtxt,liecenceNoEdtxt,contactNoEdtxt,truckNoEdtxt,fromLocationEdtxt,toLocationEdtxt,longitudeEdtxt,latitudeEdtxt;
    LinearLayout addUserLayout, updateUserLayout;
    Button updateBtn,cancelBtn;
    UserDetail user;
    TransportData database;
    String strTruckNo;
    Spinner employmentTypeSpn, truckPermitSpn, truckTypeSpn;
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_user);

        user=new UserDetail();
        database=new TransportData(this);

        addUserLayout = (LinearLayout) findViewById(R.id.addUserlayout);
        addUserLayout.setVisibility(View.GONE);

        driverNameEdtxt = (EditText) findViewById(R.id.driverNameedtx);
        ageEdtxt = (EditText) findViewById(R.id.ageEdtxt);
        addressEdtxt = (EditText) findViewById(R.id.addressEdtxt);
        liecenceNoEdtxt = (EditText) findViewById(R.id.licenceNoEdtxt);
        contactNoEdtxt = (EditText) findViewById(R.id.contactNoEdtxt);
        truckNoEdtxt = (EditText) findViewById(R.id.truckNoEdtxt);
        fromLocationEdtxt = (EditText) findViewById(R.id.fromLocationEdtxt);
        toLocationEdtxt = (EditText) findViewById(R.id.toLocationEdtxt);
        longitudeEdtxt = (EditText) findViewById(R.id.longitudeEdtxt);
        latitudeEdtxt = (EditText) findViewById(R.id.latitudeEdtxt);






        Spinner employmentType = (Spinner) findViewById(R.id.DriverSpnr);
        employmentType.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item,Utils.driver_type));

        Spinner truckType = (Spinner)findViewById(R.id.TruckTypespnr);
        truckType.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item,Utils.truck_type));

        Spinner truckPermit = (Spinner) findViewById(R.id.TruckPermitSpnr);
        truckPermit.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item,Utils.truck_permit));
        truckPermit.setSelection(0);

        truckPermit.setOnItemSelectedListener(new OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view,
                    int position, long arg3) {
                strTruckPermit = adapterView.getItemAtPosition(position).toString();
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });


        getUserData();

        updateBtn.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                boolean ok = false;
                try {
                    user.driverName = driverNameEdtxt.getText().toString();
                    user.age = ageEdtxt.getText().toString();
                    user.liecenceNo = liecenceNoEdtxt.getText().toString();
                    user.address = addressEdtxt.getText().toString();
                    user.contactNo = contactNoEdtxt.getText().toString();
                    user.driverType = strEmploymentType;
                    user.truckNo = truckNoEdtxt.getText().toString();
                    user.truckPermit = strTruckPermit;
                    user.truckType = strTruckType;
                    user.fromLocation= fromLocationEdtxt.getText().toString();
                    user.toLocation = toLocationEdtxt.getText().toString();
                    user.latitude = latitudeEdtxt.getText().toString();
                    user.longitude = longitudeEdtxt.getText().toString();
                    int n = database.updateUser(user);
                    if(n>0)
                    {
                        ok = true;
                    }

                } catch (Exception e) {
                    ok= false;

                }
                finally{
                    if(ok)
                    {

                        Toast.makeText(getApplicationContext(), "Successful update data", Toast.LENGTH_LONG).show();
                    }
                    else {
                        Toast.makeText(getApplicationContext(), "database Error", Toast.LENGTH_LONG).show();
                    }
                }

            }            
        });

        cancelBtn.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                finish();

            }

        });
}
    private void getUserData() {

        user.truckNo = strTruckNo;
        try {
            database.getUserDetail(user);

            driverNameEdtxt.setText(user.driverName);
            ageEdtxt.setText(user.age);
            addressEdtxt.setText(user.address);
            liecenceNoEdtxt.setText(user.liecenceNo);
            contactNoEdtxt.setText(user.contactNo);
            employmentTypeSpn.setTag(user.driverType);
            truckNoEdtxt.setText(user.truckNo);
            truckTypeSpn.setTag(user.truckType);
            truckPermitSpn.setTag(user.truckPermit);
            fromLocationEdtxt.setText(user.fromLocation);
            toLocationEdtxt.setText(user.toLocation); 


        }
        catch (Exception e) {

            Toast.makeText(getApplicationContext(), "database Error", Toast.LENGTH_LONG).show();
        }

    }


    }

//第83行中的错误(错误名称空指针异常)

1 个答案:

答案 0 :(得分:0)

您没有设置Spinner个实例变量。您正在将您的微调器分配给局部变量。

而不是:

Spinner employmentType = (Spinner) findViewById(R.id.DriverSpnr);
Spinner truckType = (Spinner)findViewById(R.id.TruckTypespnr);
Spinner truckPermit = (Spinner) findViewById(R.id.TruckPermitSpnr);

你需要:

employmentTypeSpn = (Spinner) findViewById(R.id.DriverSpnr);
truckTypeSpn = ...
truckPermitSpn = ...

您还需要以某种方式获取updateBtn

updateBtn = (Button) findViewById(R.id.update_button);