尝试在空对象引用上调用虚方法Service.setName(java.lang.String)'

时间:2016-01-11 23:11:51

标签: android arrays

我想用变量概念和proceDo填充此活动中的数组但是当我编译它时会抛出此异常,我不知道为什么到底是因为我在if上初始化数组

public class ConceptsActivity extends AppCompatActivity{
private int i;
private AutoCompleteTextView actv;
private TextView tvConcept;
private TextView tvConcept2;
private TextView warning;
private TextView tvPrice;
private EditText price;
private Button addConcept;
private String concept;
private double priceDo;
private List<AddConceptsListView> myConcept = new ArrayList<AddConceptsListView>();
private Service service[];

public ConceptsActivity(){

}

public ConceptsActivity(Service service[]){
    this.service = service;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTitle(R.string.TitleSer);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.concepts_layout);

    actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextViewConcept);
    tvConcept = (TextView) findViewById(R.id.textViewConcept);
    tvConcept2 = (TextView) findViewById(R.id.textViewConcept2);
    warning = (TextView) findViewById(R.id.textViewWarning);
    tvPrice = (TextView) findViewById(R.id.textViewPrice);
    price = (EditText) findViewById(R.id.editTextPrice);
    addConcept = (Button) findViewById(R.id.buttonAddConcept);

    //Autocomplete Textview
    String[] services = getResources().getStringArray(R.array.services_array);
    ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, services);
    actv.setAdapter(adapter);

    actv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                //make items visible
                tvConcept.setVisibility(View.VISIBLE);
                tvConcept2.setVisibility(View.VISIBLE);
                tvPrice.setVisibility(View.VISIBLE);
                price.setVisibility(View.VISIBLE);
                addConcept.setVisibility(View.VISIBLE);

                concept = actv.getText()+"";
                tvConcept2.setText(concept);
                //To hide the keyboard
                InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                handled = true;
            }
            return handled;
        }
    });
}

public void onClickAddConcept(View view){

    if(service == null){
        service = new Service[1];
        i=0;
    }
    //service = new Service [i];

    if(price.getText().toString().equals("")){
        warning.setVisibility(View.VISIBLE);
    }
    else {
        warning.setVisibility(View.INVISIBLE);
        if(price.getText().toString().contains(".")){
            priceDo = Double.parseDouble(price.getText().toString());
            Toast.makeText(getApplicationContext(),"Precio: " + priceDo+ "" + "\n"
                    + "Concepto: " + concept,Toast.LENGTH_SHORT).show();

            service[i].setName(concept);
            Toast.makeText(getApplicationContext(), "Array nameConcept: " + service[i].getName(), Toast.LENGTH_SHORT);
        }
        else{
            priceDo = Double.parseDouble(price.getText().toString());
            Toast.makeText(getApplicationContext(),"Precio: " + priceDo+ "" + "\n"
                    + "Concepto: " + concept,Toast.LENGTH_SHORT).show();
        }
    }
}

}

这是例外

Process: com.example.ally.helper, PID: 31717
                                                                     java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
                                                                         at android.view.View.performClick(View.java:4761)
                                                                         at android.view.View$PerformClick.run(View.java:19767)
                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:135)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5312)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
                                                                      Caused by: java.lang.reflect.InvocationTargetException
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
                                                                         at android.view.View.performClick(View.java:4761) 
                                                                         at android.view.View$PerformClick.run(View.java:19767) 
                                                                         at android.os.Handler.handleCallback(Handler.java:739) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                         at android.os.Looper.loop(Looper.java:135) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5312) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 
                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.ally.helper.Service.setName(java.lang.String)' on a null object reference
                                                                         at com.example.ally.helper.ConceptsActivity.onClickAddConcept(ConceptsActivity.java:107)
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
                                                                         at android.view.View.performClick(View.java:4761) 
                                                                         at android.view.View$PerformClick.run(View.java:19767) 
                                                                         at android.os.Handler.handleCallback(Handler.java:739) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                         at android.os.Looper.loop(Looper.java:135) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5312) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 

例外是在这一行

服务[I] .setName(概念);

1 个答案:

答案 0 :(得分:0)

致电时

service = new Service[1];

您实际上并未实例化Service对象,而只是实例化Service数组。

要解决此问题,请尝试:

service = new Service[1];
service[0] = new Service(); // or iterate through a loop and create