Android Activity does not open

Asked

Viewed 498 times

0

I’m making a retrofit connection app, but I’m not understanding why my Activity is not opening.

My code:

public class CotacaoAlteraActivity  extends AppCompatActivity {
private ListView listView;
ProgressBar pbar;
EditText custoalterar, embalagemalterar;

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

    EnviarAlteracao = (Button) findViewById(R.id.enviaralteracao);
    EnviarAlteracao.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

    String cnpjfornecedor = getSharedPreferences(PREFS_USER, Context.MODE_PRIVATE).getString("PrefCnpj", "");
    String codigoproduto = getSharedPreferences(PREFS_COD, Context.MODE_PRIVATE).getString("PrefCod", "");

    loadCotacaoAlterar(cnpjfornecedor, codigoproduto);

    pbar = (ProgressBar) findViewById(R.id.progressBar4);
    listView = (ListView) findViewById(R.id.listView4);

}

public void loadEnviarCotacaoNova(Cotacao cotacao){ //Função POST para enviar os produtos para próxima página pra alterar.

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://192.168.25.212:8080/CotacaoWebService/webresources/cotacao/")
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();

    CotacaoEnviarAlteracaoClient client = retrofit.create(CotacaoEnviarAlteracaoClient.class);
    Call<Cotacao> call = client.reposForUser(cotacao);

    call.enqueue(new Callback<Cotacao>() {
        @Override
        public void onResponse(Call<Cotacao> call, Response<Cotacao> response) {
            Cotacao shop = response.body();
            Toast.makeText(getApplicationContext(), "Código: " + shop.getCodigoProduto(), Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onFailure(Call<Cotacao> call, Throwable t) {
            Toast.makeText(CotacaoAlteraActivity.this, "Produto inserido na lista", Toast.LENGTH_SHORT).show();
        }
    });
}

public void loadCotacaoAlterar(String buscar, String codigo) {
    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://192.168.25.212:8080/CotacaoWebService/webresources/cotacao/")
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();

    CotacaoAlterarGETClient client = retrofit.create(CotacaoAlterarGETClient.class);
    Call<List<Cotacao>> call = client.reposForUser(buscar, codigo);

    call.enqueue(new Callback<List<Cotacao>>() {
        @Override
        public void onResponse(Call<List<Cotacao>> call, Response<List<Cotacao>> response) {
            pbar.setVisibility(View.GONE);
            List<Cotacao> repos = response.body();
            listView.setAdapter(new CotacaoAlterarAdapter(CotacaoAlteraActivity.this, repos));
            if (listView.getCount() == 0) {
                Toast.makeText(getApplicationContext(), "Nenhum produto localizado.", Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFailure(Call<List<Cotacao>> call, Throwable t) {
            pbar.setVisibility(View.INVISIBLE);
            Toast.makeText(getApplicationContext(), "         Erro ao estabelecer conexão" + "\n"
                    + "            Verifique o host inserido" + "\n"
                    + "Por favor tente novamente mais tarde!", Toast.LENGTH_SHORT).show();
        }
    });
}
}

Error:

10-25 15:49:09.696 13243-13243/br.com.cotacaovolpix.cotacaovolpix E/Androidruntime: FATAL EXCEPTION: main Process: br.com.cotacaovolpix.cotacaovolpix, PID: 13243 java.lang.Runtimeexception: Unable to start Activity
Componentinfo{br.com.cotacaovolpix.cotacaovolpix/br.com.cotacaovolpix.cotacaovolpix.Cotacaoalteraactivity}: java.lang.Nullpointerexception: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$Onclicklistener)' on a null Object Reference at android.app.Activitythread.performLaunchActivity(Activitythread.java:2678) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2743) at android.app.Activitythread. -wrap12(Activitythread.java) at android.app.Activitythread$H.handleMessage(Activitythread.java:1490) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.Activitythread.main(Activitythread.java:6165) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:888) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:778) Error is here>>>>> Caused by: java.lang.Nullpointerexception: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$Onclicklistener)' on a null Object Reference at br.com.cotacaovolpix.cotacaovolpix.Cotacaoalteraactivity.onCreate(Cotacaoalteraactivity.java:39) at android.app.Activity.performCreate(Activity.java:6687) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140) at android.app.Activitythread.performLaunchActivity(Activitythread.java:2631) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2743)  at android.app.Activitythread. -wrap12(Activitythread.java)  at android.app.Activitythread$H.handleMessage(Activitythread.java:1490)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.Activitythread.main(Activitythread.java:6165)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:888)  at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:778)

  • What happens ? What is the error in logcat ?

  • You registered Activity on Androidmanifest.xml?

  • I registered yes, it only gives the normal error, closes, so I saw the error of when I make the editor give ok and call the retrofit function. If I take the function of the text editor it works...

  • Take a look at the code inside the embalagemalterar.setOnEditorActionListener you are instantiating the Cotacao and soon after you start to define the values of that instance to itself, and no value has been assigned to these. for example: cotacao.setCodigoProduto(cotacao.getCodigoProduto()); apparently that doesn’t make sense.

  • embalagemalterar or custoalterar are being recovered normally? Have instance?

  • Darlei happens that even if only I leave the click function it already error, need not even call the webservice, or anything, may not have anything within the function, but he still gives the error and closes the app. I put a button instead of setOnEditorActionListener, and even using the Onclicklistener, it closes tbm, I don’t know what it might be, but regardless of what I put it closes, I just put the Onclicklistener function, with nothing inside it, as if when clicking the button was not going to do anything, but still it closes the app.

  • I’m almost 100% sure the error is in the variable EnviarAlteracao because the mistake is a NullPointException ,this means that the variable has value null ,ie ,you are calling a method for the null ,example , null.foo().

Show 2 more comments

2 answers

1

You are calling the method loadCotaçãoAlterar() and then calling the findViewById() of listView and progressBar.... There is your mistake!

** Solution **

  • Call first the findViewByIds() and then call the methods...
  • Quick cut! knife is Tramontina! kkk

0

One reason may be that you are incorrectly referencing a Textview ID or other element in your Activity. If this is not the case, try changing the element ID and try to DEBUG your code with the help of breakpoints, and send us the line where the error occurred.

Make sure your Activity is declared in the Manifest of your application, as in the example below:

<application...

<activity android:name="fiscale.vc.android.dialog_novidade" android:label="Novidade no Fiscale.vc">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
        </intent-filter>
    </activity> 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.