我的应用程序中的NullPointerexception,无法修复它,帮助我

时间:2015-02-04 08:00:25

标签: java android nullpointerexception

我正在Android中创建一个单位转换应用程序,其中一个可以轻松地将单位更改为特定类型,基本单位是TeaSpoons,我正在使用Android Studio IDE,但是当我运行我的项目时,它运行正常没有任何问题,但在我的应用程序内部,当我选择其他测量单位时,它会崩溃并且“不幸的是,单位转换应用程序停止了”对话框弹出并关闭应用程序..

检查Logcat后发现有NullPointerException,我无法修复它 我提供下面的源代码和发生异常的代码行,请帮助我解决这个问题,并建议我更好的解决方案。

package com.hackercoder.samsung.unitconversion;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;




public class MainActivity extends ActionBarActivity {

private Spinner unitTypeSpinner;
private EditText amountTypeText;
private TextView     TeaSpoontv,Cupstv,TableSpoontv,Ouncestv,Kilogramtv,Quarttv,Gallontv,Poundtv,Millilitretv,
Litretv,Milligramtv, Pinttv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addItemsToUnitTypeSpinner();
    addListenerToUnitTypeSpinner();
    amountTypeText=(EditText)findViewById(R.id.amount_entered);
    initializeTextViews();
}

public void initializeTextViews()
{
    TeaSpoontv=(TextView)findViewById(R.id.tsp_text_view);
    Cupstv=(TextView)findViewById(R.id.cups_text_view);
    TableSpoontv=(TextView)findViewById(R.id.tbsp_text_view);
    Ouncestv=(TextView)findViewById(R.id.oz_text_view);
    Kilogramtv=(TextView)findViewById(R.id.kg_text_view);
    Quarttv=(TextView)findViewById(R.id.quart_text_view);
    Gallontv=(TextView)findViewById(R.id.gallon_text_view);
    Poundtv=(TextView)findViewById(R.id.pound_text_view);
    Millilitretv=(TextView)findViewById(R.id.ml_text_view);
    Litretv=(TextView)findViewById(R.id.litre_text_view);
    Milligramtv=(TextView)findViewById(R.id.mlg_text_view);
    Pinttv=(TextView)findViewById(R.id.pint_text_view);
     }
public void addItemsToUnitTypeSpinner()
   {
    unitTypeSpinner=(Spinner)findViewById(R.id.unit_type_spinner);
    ArrayAdapter<CharSequence> unitTypeSpinnerAdapter= ArrayAdapter.createFromResource(this,R.array.conversion_types,android.R.layout.simple_spinner_item);
    unitTypeSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    unitTypeSpinner.setAdapter(unitTypeSpinnerAdapter);
}
public void addListenerToUnitTypeSpinner() {
    unitTypeSpinner = (Spinner) findViewById(R.id.unit_type_spinner);
    unitTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String itemSelectedInSpinner = parent.getItemAtPosition(position).toString();
            checkIfConvertingFromTeaSpoon(itemSelectedInSpinner);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

public void checkIfConvertingFromTeaSpoon(String currentUnit) {
    if (currentUnit!=null)

    {
        switch (currentUnit) {
            case "TeaSpoon":
                updateUnitTypeUsingTSP(Quantity.Unit.tsp);
                break;
            case "TableSpoon":
                updateUnitTypeUsingOther(Quantity.Unit.tbs);
                break;
            case "Cups":
                updateUnitTypeUsingOther(Quantity.Unit.cup);
                break;
            case "Ounces":
                updateUnitTypeUsingOther(Quantity.Unit.oz);
                break;
            case "Kilogram":
                updateUnitTypeUsingOther(Quantity.Unit.kg);
                break;
            case "Quart":
                updateUnitTypeUsingOther(Quantity.Unit.quart);
                break;
            case "Gallon":
                updateUnitTypeUsingOther(Quantity.Unit.gallon);
                break;
            case "Pound":
                updateUnitTypeUsingOther(Quantity.Unit.pound);
                break;
            case "Millilitre":
                updateUnitTypeUsingOther(Quantity.Unit.ml);
                break;
            case "Litre":
                updateUnitTypeUsingOther(Quantity.Unit.liter);
                break;
            case "Milligram":
                updateUnitTypeUsingOther(Quantity.Unit.mg);
                break;
            case "Pint":
                updateUnitTypeUsingOther(Quantity.Unit.pint);
                break;
        }
    }

}


public void updateUnitTypeUsingTSP(Quantity.Unit currentUnit)
    {
        double doubleToConvert= Double.parseDouble(amountTypeText.getText().toString());
        String teaspoonValueAndUnit= doubleToConvert + " " + "TeaSpoon";
        TeaSpoontv.setText(teaspoonValueAndUnit);
        updateUnitTextFieldUsingTSP(doubleToConvert,Quantity.Unit.tbs,TableSpoontv);
        updateUnitTextFieldUsingTSP(doubleToConvert,Quantity.Unit.cup,Cupstv);
        updateUnitTextFieldUsingTSP(doubleToConvert,Quantity.Unit.oz,Ouncestv);
        updateUnitTextFieldUsingTSP(doubleToConvert,Quantity.Unit.kg,Kilogramtv);
        updateUnitTextFieldUsingTSP(doubleToConvert,Quantity.Unit.quart,Quarttv);
        updateUnitTextFieldUsingTSP(doubleToConvert,Quantity.Unit.gallon,Gallontv);
        updateUnitTextFieldUsingTSP(doubleToConvert,Quantity.Unit.pound,Poundtv);
        updateUnitTextFieldUsingTSP(doubleToConvert,Quantity.Unit.ml,Millilitretv);
        updateUnitTextFieldUsingTSP(doubleToConvert,Quantity.Unit.liter,Litretv);
        updateUnitTextFieldUsingTSP(doubleToConvert,Quantity.Unit.mg,Milligramtv);
        updateUnitTextFieldUsingTSP(doubleToConvert,Quantity.Unit.pint,Pinttv);
    }

public void updateUnitTextFieldUsingTSP(double doubleToConvert, Quantity.Unit unitConvertingTo, TextView tv) {

    Quantity unitQuantity= new Quantity(doubleToConvert,Quantity.Unit.tsp);
    String tempUnit= unitQuantity.to(unitConvertingTo).toString();
    tv.setText(tempUnit);
}

public void updateUnitTypeUsingOther(Quantity.Unit currentUnitInSpinner) {
    double doubleToConvert= Double.parseDouble(amountTypeText.getText().toString());
    Quantity currentQuantitySelected= new Quantity(doubleToConvert, currentUnitInSpinner);
    String valueInTSP= currentQuantitySelected.to(Quantity.Unit.tsp).toString();
    TeaSpoontv.setText(valueInTSP);
    updateUnitTextFieldUsingTSP(doubleToConvert,currentUnitInSpinner,Quantity.Unit.tbs, TableSpoontv);
    updateUnitTextFieldUsingTSP(doubleToConvert,currentUnitInSpinner,Quantity.Unit.cup,Cupstv);
    updateUnitTextFieldUsingTSP(doubleToConvert,currentUnitInSpinner,Quantity.Unit.oz,Ouncestv);
    updateUnitTextFieldUsingTSP(doubleToConvert,currentUnitInSpinner,Quantity.Unit.kg,Kilogramtv);
    updateUnitTextFieldUsingTSP(doubleToConvert,currentUnitInSpinner,Quantity.Unit.quart,Quarttv);
    updateUnitTextFieldUsingTSP(doubleToConvert,currentUnitInSpinner,Quantity.Unit.gallon,Gallontv);
    updateUnitTextFieldUsingTSP(doubleToConvert,currentUnitInSpinner,Quantity.Unit.pound,Poundtv);
    updateUnitTextFieldUsingTSP(doubleToConvert,currentUnitInSpinner,Quantity.Unit.ml,Millilitretv);
    updateUnitTextFieldUsingTSP(doubleToConvert,currentUnitInSpinner,Quantity.Unit.liter,Litretv);
    updateUnitTextFieldUsingTSP(doubleToConvert,currentUnitInSpinner,Quantity.Unit.mg,Milligramtv);
    updateUnitTextFieldUsingTSP(doubleToConvert,currentUnitInSpinner,Quantity.Unit.pint,Pinttv);

    if(currentUnitInSpinner.name().equals(currentQuantitySelected.unit.name()))
    {

       String currentSelectedUnitInSpinnerTextInTextView= doubleToConvert + " " + currentQuantitySelected.unit.name();
       String currentTextViewName= currentQuantitySelected.unit.name()+" _text_view";
       int currentID=getResources().getIdentifier(currentTextViewName,"id",MainActivity.this.getPackageName());
       TextView textview=(TextView)findViewById(currentID);
        textview.setText(currentTextViewName);

}


}

public void updateUnitTextFieldUsingTSP(double doubleToConvert, Quantity.Unit currentUnitInSpinner, Quantity.Unit preferredUnit, TextView targetTextView)
{
    Quantity currentQuantitySelected=new Quantity(doubleToConvert, currentUnitInSpinner);
    String updatedTextviewText =currentQuantitySelected.to(Quantity.Unit.tsp).to(preferredUnit).toString();
    targetTextView.setText(updatedTextviewText);

}


@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_main, 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);
    }
    }

和生成的logcat

       02-04 13:16:44.395  18606-18606/com.hackercoder.samsung.unitconversion                                                                                                    D/dalvikvm﹕ Late-enabling CheckJNI
       02-04 13:16:44.395  18606-18606/com.hackercoder.samsung.unitconversion D/dalvikvm﹕ Try to disable coredump for pid 18606
       02-04 13:16:44.395  18606-18606/com.hackercoder.samsung.unitconversion D/dalvikvm﹕ Process 18606 nice name: com.hackercoder.samsung.unitconversion
       02-04 13:16:44.395  18606-18606/com.hackercoder.samsung.unitconversion D/dalvikvm﹕ Extra Options: not specified
       02-04 13:16:44.475  18606-18606/com.hackercoder.samsung.unitconversion I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
       02-04 13:16:44.475  18606-18606/com.hackercoder.samsung.unitconversion W/dalvikvm﹕ VFY: unable to resolve virtual method 11345: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
       02-04 13:16:44.475  18606-18606/com.hackercoder.samsung.unitconversion D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
       02-04 13:16:44.475  18606-18606/com.hackercoder.samsung.unitconversion I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
        02-04 13:16:44.475  18606-18606/com.hackercoder.samsung.unitconversion W/dalvikvm﹕ VFY: unable to resolve virtual method 11351: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
        02-04 13:16:44.475  18606-18606/com.hackercoder.samsung.unitconversion D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
        02-04 13:16:44.475  18606-18606/com.hackercoder.samsung.unitconversion I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
        02-04 13:16:44.475  18606-18606/com.hackercoder.samsung.unitconversion W/dalvikvm﹕ VFY: unable to resolve virtual method 9039: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
        02-04 13:16:44.475  18606-18606/com.hackercoder.samsung.unitconversion D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
        02-04 13:16:44.485  18606-18606/com.hackercoder.samsung.unitconversion I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
        02-04 13:16:44.485  18606-18606/com.hackercoder.samsung.unitconversion W/dalvikvm﹕ VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
        02-04 13:16:44.485  18606-18606/com.hackercoder.samsung.unitconversion  D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
        02-04 13:16:44.485  18606-18606/com.hackercoder.samsung.unitconversion I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
        02-04 13:16:44.485  18606-18606/com.hackercoder.samsung.unitconversion W/dalvikvm﹕ VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
         02-04 13:16:44.485  18606-18606/com.hackercoder.samsung.unitconversion D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
        02-04 13:16:44.525  18606-18606/com.hackercoder.samsung.unitconversion D/TextView﹕ Constructor - Got Res id for appearance for textColorPrimaryInverse
        02-04 13:16:44.525  18606-18606/com.hackercoder.samsung.unitconversion W/ResourceType﹕ Skipping entry 0x7f070035 in package table 0 because it is not complex!
        02-04 13:16:44.525  18606-18606/com.hackercoder.samsung.unitconversion D/TextView﹕ Constructor - Got appearance for textColorPrimaryInverse
        02-04 13:16:44.525  18606-18606/com.hackercoder.samsung.unitconversion D/TextView﹕ Constructor -- Got mEditTextBackgroundColor
        02-04 13:16:44.605  18606-18606/com.hackercoder.samsung.unitconversion D/android.widget.GridLayout﹕ horizontal constraints: x1-x0>=341, x2-x1>=276, x2-x0<=596 are inconsistent; permanently removing: x2-x0<=596.
        02-04 13:16:44.635  18606-18606/com.hackercoder.samsung.unitconversion E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 46
        02-04 13:16:44.635  18606-18606/com.hackercoder.samsung.unitconversion E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 49
        02-04 13:16:44.635  18606-18606/com.hackercoder.samsung.unitconversion E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 50
        02-04 13:16:44.645  18606-18606/com.hackercoder.samsung.unitconversion E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 50
        02-04 13:16:44.645  18606-18606/com.hackercoder.samsung.unitconversion E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 50
        02-04 13:16:44.655  18606-18606/com.hackercoder.samsung.unitconversion E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 52
        02-04 13:16:44.675  18606-18606/com.hackercoder.samsung.unitconversion D/OpenGLRenderer﹕ Enabling debug mode 0
         02-04 13:16:50.035  18606-18606/com.hackercoder.samsung.unitconversion D/AndroidRuntime﹕ Shutting down VM
         02-04 13:16:50.035  18606-18606/com.hackercoder.samsung.unitconversion W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x430ef140)
         **02-04 13:16:50.035  18606-18606/com.hackercoder.samsung.unitconversion E/AndroidRuntime﹕ FATAL EXCEPTION: main
         Process: com.hackercoder.samsung.unitconversion, PID: 18606
         java.lang.NullPointerException
         at   com.hackercoder.samsung.unitconversion.MainActivity.updateUnitTypeUsingOther(MainActivity.java:170)
        at com.hackercoder.samsung.unitconversion.MainActivity.checkIfConvertingFromTeaSpoon(MainActivity.java:86)
        at com.hackercoder.samsung.unitconversion.MainActivity$1.onItemSelected(MainActivity.java:64)
        at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
        at android.widget.AdapterView.access$200(AdapterView.java:48)
        at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:149)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
        at dalvik.system.NativeStart.main(Native Method)**

根据Logcat,updateUnitTypeusingOther()

中发生异常

请帮我解决问题或建议我提供更好的解决方案

0 个答案:

没有答案