threadid = 1:线程退出,未捕获异常(NullPointerException)

时间:2014-05-27 01:59:18

标签: android web-services android-activity spinner argumentnullexception

我需要一些帮助来完成我的工作室,我在Android上有一个带有WebService的应用程序,方法

List<Servicos> servicos = new ServicoReq().getListaServicos();

工作正常,我正在尝试编译此代码,如下:

public class ConsultaServicos extends MainActivity implements OnItemClickListener{
//private static final String CATEGORIA = "cardapio";
private Spinner spinnerServicos;
private long idServico=0;

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

    //Identifica o Spinner no layout
    spinnerServicos = (Spinner) findViewById(R.id.spinnerServicos);
    List<Servicos> servicos = new ServicoReq().getListaServicos();
    List<String> lista = new ArrayList<String>();
    lista.add("um");
    //Cria um ArrayAdapter usando um padrão de layout da classe R do android, passando o ArrayList nome dos serviço
    ArrayAdapter<Servicos> arrayAdapter = new ArrayAdapter<Servicos>(this, android.R.layout.simple_spinner_dropdown_item, servicos);
    spinnerServicos.setAdapter(arrayAdapter);
    //Método do Spinner para capturar o item selecionado
        spinnerServicos.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View v, int posicao, long id) {
                //pega nome pela posição

                Servicos serv = (Servicos)parent.getSelectedItem();
                idServico = serv.getId();

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

            }
        });

    Button btListarEstabelecimentoCidade = (Button) findViewById(R.id.btListarEstabelecimentosCidade);
    btListarEstabelecimentoCidade.setOnClickListener(this); 
}

public void onClick(View v) {
    Intent it;
    switch (v.getId()){
    case R.id.btListarEstabelecimentosCidade:
        it = new Intent("LISTAGEM_ESTABELECIMENTO");
        //Seta o que será passado CIDADE e Reseta TODOS OUTROS!
        it.putExtra("BUSCA_POR_CIDADE", "");
        it.putExtra("BUSCA_POR_SERVICOS", idServico);
        startActivity(it);
    }       
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub

}
}

但这个错误信息令我发疯:

05-27 02:02:39.629: W/dalvikvm(834): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-27 02:02:39.650: E/AndroidRuntime(834): FATAL EXCEPTION: main
05-27 02:02:39.650: E/AndroidRuntime(834): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.projetoandroid/br.projetoandroid.ConsultaServicos}: java.lang.NullPointerException
05-27 02:02:39.650: E/AndroidRuntime(834):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-27 02:02:39.650: E/AndroidRuntime(834):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-27 02:02:39.650: E/AndroidRuntime(834):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-27 02:02:39.650: E/AndroidRuntime(834):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-27 02:02:39.650: E/AndroidRuntime(834):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 02:02:39.650: E/AndroidRuntime(834):  at android.os.Looper.loop(Looper.java:123)
05-27 02:02:39.650: E/AndroidRuntime(834):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-27 02:02:39.650: E/AndroidRuntime(834):  at java.lang.reflect.Method.invokeNative(Native Method)
05-27 02:02:39.650: E/AndroidRuntime(834):  at java.lang.reflect.Method.invoke(Method.java:507)
05-27 02:02:39.650: E/AndroidRuntime(834):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-27 02:02:39.650: E/AndroidRuntime(834):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-27 02:02:39.650: E/AndroidRuntime(834):  at dalvik.system.NativeStart.main(Native Method)
05-27 02:02:39.650: E/AndroidRuntime(834): Caused by: java.lang.NullPointerException
05-27 02:02:39.650: E/AndroidRuntime(834):  at br.projetoandroid.ConsultaServicos.onCreate(ConsultaServicos.java:53)
05-27 02:02:39.650: E/AndroidRuntime(834):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-27 02:02:39.650: E/AndroidRuntime(834):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-27 02:02:39.650: E/AndroidRuntime(834):  ... 11 more

感谢您的理解!

1 个答案:

答案 0 :(得分:0)

我将上面的代码更改为以下代码:

  

公共类ConsultaServicos扩展了Activity实现OnItemClickListener {       // private static final String CATEGORIA =“cardapio”;       私人Spinner spinnerServicos;       private long idServico = 0;

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

    List<Servicos> l = new ServicoReq().getListaServicos();

    spinnerServicos = (Spinner) findViewById(R.id.spinnerServicos);
    ArrayAdapter<Servicos> servicoAdapter = new ArrayAdapter<Servicos>(this, android.R.layout.simple_spinner_item, l);  
    spinnerServicos.setAdapter(servicoAdapter); 
    spinnerServicos.setPrompt("Selecione um serviço");
    spinnerServicos.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View v, int posicao, long id) {
            //pega nome pela posição            
            Servicos serv = (Servicos)parent.getSelectedItem();
            idServico = serv.getId();
            Log.i(">>>>>>>>>>>ID SERVIÇO:", Long.toString(idServico));
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    getListenerButtonListaEstabelecimento();
}

public void getListenerButtonListaEstabelecimento(){
    Button btListarEstabelecimentoServico = (Button) findViewById(R.id.btListarEstabelecimentosServicos);
    btListarEstabelecimentoServico.setOnClickListener(new OnClickListener() { 
    @Override
    public void onClick(View v) {
        Intent it;
        switch (v.getId()){
        case R.id.btListarEstabelecimentosServicos:
            it = new Intent("LISTAGEM_ESTABELECIMENTO");
            //Seta o que será passado CIDADE e Reseta TODOS OUTROS!
            it.putExtra("BUSCA_POR_CIDADE", "");
            it.putExtra("BUSCA_POR_SERVICOS", idServico);
            startActivity(it);
        }       
    }
});
}


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub

}

}

将按钮移出onCreate。