Java 我的应用程序中出现NullPointer异常,无法修复,请帮助我

Java 我的应用程序中出现NullPointer异常,无法修复,请帮助我,java,android,nullpointerexception,Java,Android,Nullpointerexception,我正在android中创建一个单位转换应用程序,可以轻松地将单位更改为特定类型。基本单位是茶匙,我使用的是android Studio IDE,但当我运行项目时,它运行正常,没有任何问题,但在我的应用程序中,当我选择其他计量单位时,它崩溃并“不幸的是,单位转换应用程序已停止”对话框弹出,应用程序关闭 检查Logcat后,我发现存在NullPointerException,无法修复它 我提供了下面的源代码和发生异常的代码行,请帮助我解决这个问题,并向我建议更好的解决方案 package com.h

我正在android中创建一个单位转换应用程序,可以轻松地将单位更改为特定类型。基本单位是茶匙,我使用的是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);
    }
    }
package com.hackercoder.samsung.unitconversion;
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.EditText;
导入android.widget.Spinner;
导入android.widget.TextView;
公共类MainActivity扩展了ActionBarActivity{
专用微调器unitTypeSpinner;
私有EditText amountTypeText;
私人文本视图茶匙电视、杯电视、汤匙电视、盎司电视、千克电视、夸脱电视、加仑电视、磅电视、毫厘电视、,
Litretv、miligramtv、Pinttv;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addItemsToUnitTypeSpinner();
addListenerToUnitTypeSpinner();
amountTypeText=(EditText)findViewById(输入的R.id.amount);
initializeTextViews();
}
public void initializeTextViews()
{
茶匙TV=(文本视图)findViewById(R.id.tsp\u text\u视图);
Cupstv=(TextView)findViewById(R.id.cups\u text\u view);
汤匙TV=(文本视图)findViewById(R.id.tbsp_text_视图);
盎司电视=(文本视图)findViewById(R.id.oz_文本视图);
千克电视=(文本视图)findViewById(R.id.kg\u文本视图);
Quarttv=(文本视图)findViewById(R.id.quart\u文本视图);
Gallontv=(TextView)findViewById(R.id.gallon\u text\u视图);
Poundtv=(文本视图)findViewById(R.id.pound\u文本视图);
millitretv=(TextView)findViewById(R.id.ml\u text\u视图);
Litretv=(TextView)findViewById(R.id.l\u text\u视图);
Milligramtv=(TextView)findViewById(R.id.mlg\u text\u视图);
Pinttv=(TextView)findViewById(R.id.pint\u text\u视图);
}
public void addItemsToUnitTypeSpinner()
{
unitTypeSpinner=(Spinner)findViewById(R.id.unit\u type\u Spinner);
ArrayAdapter unitTypeSpinnerAdapter=ArrayAdapter.createFromResource(这个,R.array.conversion\u types,android.R.layout.simple\u微调器\u项);
unitTypeSpinnerAdapter.setDropDownViewResource(android.R.layout.simple\u dropdown\u item\u 1line);
unitTypeSpinner.setAdapter(unitTypeSpinnerAdapter);
}
public void addListenerToUnitTypeSpinner(){
unitTypeSpinner=(Spinner)findViewById(R.id.unit\u type\u Spinner);
unitTypeSpinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
String itemSelectedInSpinner=parent.getItemAtPosition(position).toString();
检查是否从茶匙中转换(旋转器中选择的项目);
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
}
公共无效检查从茶匙转换(字符串当前单位){
if(currentUnit!=null)
{
开关(电流单位){
案例“茶匙”:
更新UnitTypeUsingTSP(数量单位tsp);
打破
案例“汤匙”:
使用其他(数量、单位、待定)更新单位类型;
打破
案例“杯子”:
使用其他(数量、单位、杯)更新单位类型;
打破
案例“盎司”:
使用其他(数量、单位、盎司)更新单位类型;
打破
“千克”情况:
使用其他设备更新类型(数量单位:kg);
打破
案例“夸脱”:
使用其他(数量单位夸脱)更新单位类型;
打破
案例“加仑”:
更新UnitTypeUsingother(数量、单位、加仑);
打破
“英镑”一案:
使用其他(数量、单位、磅)更新单位类型;
打破
案例“毫升”:
使用其他方式更新UnitTypes(数量.单位.ml);
打破
案例“升”:
使用其他设备更新单位类型(数量、单位、升);
打破
案例“毫克”:
使用其他(数量单位为毫克)更新单位类型;
打破
案例“品脱”:
使用其他(数量、单位、品脱)更新单位类型;
打破
}
}
}
公共void updateUnitTypeUsingTSP(Quantity.Unit currentUnit)
{
double doubleToConvert=double.parseDouble(amountTypeText.getText().toString());
字符串teaspoonValueAndUnit=doubleToConvert+“”+“茶匙”;
茶匙TV.setText(茶匙值和单位);
使用TSP(doubleToConvert,Quantity.Unit.tbs,汤匙TV)更新UnitTextField;
使用TSP(双转换、数量、单位、杯、杯)更新UnitTextField;
使用TSP更新UnitTextField(doubleToConvert,Quantity.Unit.oz,Ouncestv);
使用TSP(双转换,数量。单位。千克,千克TV)更新UnitTextField;
使用TSP(双转换、数量、单位、夸脱、夸脱TV)更新UnitTextField;
使用TSP更新UnitTextField(双转换、数量、单位、加仑、加仑);
使用TSP(doubleToConvert,Quantity.Unit.pound,Poundtv)更新UnitTextField;
使用TSP(doubleToConvert,Quantity.Unit.ml,millitRetv)更新UnitTextField;
使用TSP更新UnitTextField(双转换、数量、单位升、Litretv);
使用TSP更新UnitTextField(双转换,数量.单位.mg,毫格拉姆特);
updateUnitTextFieldUs
       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)**