java.lang.NullPointerException:println需要一个消息android数据库错误

时间:2015-02-19 13:45:36

标签: java android error-handling

我创建了一个数据库,它给了我这个错误。

这是logcat。

02-19 15:33:38.057    1273-1273/tubapps.dbtutorial E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{tubapps.dbtutorial/tubapps.dbtutorial.EditIncome}: java.lang.NullPointerException: println needs a message
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException: println needs a message
            at android.util.Log.println_native(Native Method)
            at android.util.Log.d(Log.java:138)
            at tubapps.dbtutorial.EditIncome.onCreate(EditIncome.java:66)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

这是我的活动。

public class EditIncome extends ActionBarActivity {

    DBHelper helper = new DBHelper(this);
    private Calendar calendar;
    private TextView dateView;
    private int year, month, day;

    TextView payer;
    TextView amount;
    Spinner payments;
    Spinner category;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_income);

        dateView = (TextView) findViewById(R.id.input_date);
        calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        showDate(year, month + 1, day);

        payer = (TextView) findViewById(R.id.input_payer);
        amount = (TextView) findViewById(R.id.input_amount);
        dateView = (TextView) findViewById(R.id.input_date);

        payments = (Spinner) findViewById(R.id.incomeNumber);
        ArrayAdapter<CharSequence> payments_adapter = ArrayAdapter.createFromResource(this,
                R.array.recurring_income, android.R.layout.simple_spinner_item);
        payments_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        payments.setAdapter(payments_adapter);

        category = (Spinner) findViewById(R.id.categoryIncome);
        ArrayAdapter<CharSequence> income_adapter = ArrayAdapter.createFromResource(this,
                R.array.category_income, android.R.layout.simple_spinner_item);
        income_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        category.setAdapter(income_adapter);

        Intent objIntent = getIntent();
        String incomeId = objIntent.getStringExtra("incomeId");
        Log.d("Reading: ", "Reading all incomes..");
        HashMap<String, String> incomeList = helper.getIncomeInfo(incomeId);
        Log.d("incomeAmount",incomeList.get("incomeAmount"));
        Log.d("incomePayer",incomeList.get("incomePayer"));
        Log.d("incomeDate",incomeList.get("incomeDate"));
        Log.d("incomeCategory",incomeList.get("incomeCategory"));
        Log.d("incomePayments",incomeList.get("incomePayments"));
        if(incomeList.size()!=0) {
            amount.setText(incomeList.get("incomeAmount"));
            payer.setText(incomeList.get("incomePayer"));
            dateView.setText(incomeList.get("incomeDate"));
            payments.setSelection(getIndex(payments, "incomePayments"));
            category.setSelection(getIndex(category, "incomeCategory"));
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_edit_income, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void setDate(View view) {
        showDialog(999);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        if (id == 999) {
            return new DatePickerDialog(this, myDateListener, year, month, day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener myDateListener
            = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
            // arg1 = year
            // arg2 = month
            // arg3 = day
            showDate(arg1, arg2 + 1, arg3);
        }
    };

    private void showDate(int year, int month, int day) {
        dateView.setText(new StringBuilder().append(day).append("/")
                .append(month).append("/").append(year));
    }

    public void editIncome(View view) {
        HashMap<String, String> queryValues = new HashMap<String, String>();
        payer = (TextView) findViewById(R.id.input_payer);
        amount = (TextView) findViewById(R.id.input_amount);
        dateView = (TextView) findViewById(R.id.input_date);

        payments = (Spinner) findViewById(R.id.incomeNumber);
        ArrayAdapter<CharSequence> payments_adapter = ArrayAdapter.createFromResource(this,
                R.array.recurring_income, android.R.layout.simple_spinner_item);
        payments_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        payments.setAdapter(payments_adapter);

        category = (Spinner) findViewById(R.id.categoryIncome);
        ArrayAdapter<CharSequence> income_adapter = ArrayAdapter.createFromResource(this,
                R.array.category_income, android.R.layout.simple_spinner_item);
        income_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        category.setAdapter(income_adapter);
        Intent objIntent = getIntent();
        String incomeId = objIntent.getStringExtra("incomeId");
        queryValues.put("incomeId", incomeId);
        queryValues.put("incomeAmount", amount.getText().toString());
        queryValues.put("incomePayer", payer.getText().toString());
        queryValues.put("incomeDate", dateView.getText().toString());
        queryValues.put("incomeCategory", category.getSelectedItem().toString());
        queryValues.put("incomePayments", payments.getSelectedItem().toString());

        helper.updateIncome(queryValues);
        this.callHomeActivity(view);
    }

    public void removeIncome(View view) {
        Intent objIntent = getIntent();
        String incomeId = objIntent.getStringExtra("incomeId");
        helper.deleteIncome(incomeId);
        this.callHomeActivity(view);

    }
    public void callHomeActivity(View view) {
        Intent objIntent = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(objIntent);
    }

    private int getIndex(Spinner spinner, String myString) {

        int index = 0;

        for (int i = 0; i < spinner.getCount(); i++) {
            if (spinner.getItemAtPosition(i).equals(myString)) {
                index = i;
            }
        }
        return index;
    }
}

P.S。 66行是Log.d("incomeAmount",incomeList.get("incomeAmount"));

我在互联网上找到了一些东西,但它并没有解决我的问题。我也是初学者,所以请不要过度反应。

1 个答案:

答案 0 :(得分:0)

incomeList.get(“incomeAmount”)返回null。因此,如果字段填写正确,则必须检查填充HashTable的函数。