Show information on Activity androd studio

Asked

Viewed 40 times

1

I’m making a screen "Notification" in the application , but will only show the database information without notifying user. However the information is not showing on the screen, is giving that the application stopped.

Class:

import static com.example.tcc.tcc.ResultadoONGActivity.lerListViewId;

public class Notificacao extends AppCompatActivity {

    public static NotificacoesEncontradas notificacaoStatica;
    TextView NotONG;
    private ListView listView;
    notificacoesEncontradasAdapter notificacoesEncontradasAdapter;

    ArrayList id_user = new ArrayList();

    public static String lerListViewIdUser;

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

        NotONG = (TextView) findViewById(R.id.NotNaoEncontradas);
        NotONG.setVisibility(View.INVISIBLE);

        Boolean TemDado = BuscarContas();
        if (TemDado == false) {
            NotONG.setVisibility(View.VISIBLE);
            //Toast.makeText(this, "Nenhuma Conta Encontrada", Toast.LENGTH_SHORT).show();

        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ///de acordo com o "position" irá ler as variaveis atuais abaixo: ///
                lerListViewIdUser = id_user.get(position).toString();
            }
        });
        /*
        Boolean TemDado = VisualizarNotificacoes();
        if (TemDado == false) {
            NotONG.setVisibility(View.VISIBLE);
            //Toast.makeText(this, "Nenhuma Conta Encontrada", Toast.LENGTH_SHORT).show();

        }*/

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {// esse metodo que manda na action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_tela_inicial, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_close) {
            //ao clicar em sair irá sair do aplicativo, apenas o finish irá sair apenas da tela atual e não do aplicativo.
            startActivity(new Intent(this,MainActivity.class));
            finish();
            return true;

        }if (id ==R.id.action_perfil){
            startActivity(new Intent(Notificacao.this,EditarPerfilActivity.class));
        }if (id==R.id.action_alterar_senha){
            startActivity(new Intent(Notificacao.this,AlterarSenhaActivity.class));
        }if(id==R.id.action_notificacoes){
            startActivity(new Intent(Notificacao.this,Notificacao.class));
        }if(id==R.id.logo_maos){
            startActivity(new Intent(this, TelaInicialActivity.class));
        }
        return super.onOptionsItemSelected(item);
    }

    public Boolean BuscarContas() {

        ArrayList<NotificacoesEncontradas> fim = new ArrayList<NotificacoesEncontradas>();
        Boolean TemDado = false;
        try {

            String retorno = new HTTPService("notificacoes", "telefone=" + lerListViewIdUser).execute().get();


            JSONArray json = new JSONArray(retorno);

            for(int i = 0; i< json.length();i++){
               NotificacoesEncontradas NotificacoesEncontradas = new NotificacoesEncontradas();

                //atividadesEncontradas.setId_user(json.getJSONObject(i).getString("razao_social"));
                NotificacoesEncontradas.setTelefone(json.getJSONObject(i).getString("telefone"));
                NotificacoesEncontradas.setTipo(json.getJSONObject(i).getString("tipo"));
               // NotificacoesEncontradas.setArea_interesse(json.getJSONObject(i).getString("conta"));



                TemDado = true;
                fim.add(NotificacoesEncontradas);
            }

            carregaResultadoLista(fim);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return TemDado;
    }

    public void carregaResultadoLista(ArrayList<NotificacoesEncontradas> lista){
        notificacoesEncontradasAdapter = new notificacoesEncontradasAdapter(Notificacao.this, lista);
        listView.setAdapter(notificacoesEncontradasAdapter);//preenchido dinamicamente
    }
}

Php:

<?php
    include_once 'includes/conexao.php';

    /* check connection */
    if(mysqli_connect_errno()){
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    $user_id = $_GET["user_id"];

    if($user_id != ""){
        // $dbcon->query("SET NAMES 'utf8'");
        $sql    = "SELECT id,
                        user_id,
                        ong_id,
                        tipo_id,
                        tipo,
                        descricao,
                        telefone,
                        visualizada,
                        data_criacao
                    FROM app.notificacao
                    WHERE user_id = '$user_id'
                        AND tipo = 'InteresseObjeto'
                    ORDER BY data_criacao DESC";

        $result = $dbcon->query($sql);

        while($e = mysqli_fetch_assoc($result)){
            $output[] = $e; 
        }   

        print(json_encode($output)); 
        $dbcon->close();
    }
?>

logcat:

E/Androidruntime: FATAL EXCEPTION: main Process: com.example.tcc.tcc, PID: 32315 java.lang.Runtimeexception: Unable to start Activity Componentinfo{com.example.tcc.tcc/com.example.tcc.tcc.Notification}: java.lang.Nullpointerexception: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null Object Reference at android.app.Activitythread.performLaunchActivity(Activitythread.java:2984) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:3045) at android.app.Activitythread. -wrap14(Activitythread.java) at android.app.Activitythread$H.handleMessage(Activitythread.java:1642) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.Activitythread.main(Activitythread.java:6776) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:1496) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:1386) Caused by: java.lang.Nullpointerexception: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null Object Reference at com.example.tcc.tcc.Notification.onCreate(Notification.java:35) at android.app.Activity.performCreate(Activity.java:6955) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) at android.app.Activitythread.performLaunchActivity(Activitythread.java:2927) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:3045)  at android.app.Activitythread. -wrap14(Activitythread.java)  at android.app.Activitythread$H.handleMessage(Activitythread.java:1642)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.Activitythread.main(Activitythread.java:6776)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:1496)  at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:1386)

1 answer

1

Your Textview NotONG = (TextView) findViewById(R.id.NotNaoEncontradas); not being found in layout R.layout.activity_notificacao

Make sure you set the correct layout or if there is a Textview in the layout

Browser other questions tagged

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