Error in Android application NullPointerException

0

I'm trying to create an android application that dynamically generates buttons on the screen, but I'm getting a NullPointerException error and I do not know how to treat it.

Someone who has patience in explaining obvious things to me, because I'm new to the programming world.

Here is MainActivity.java:

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    private ImageButton btn_ir_historico;
    private ImageButton btn_ir_produtos;
    private LinearLayout linearL;
    private Button addBtn;

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

        linearL = (LinearLayout) findViewById(R.id.linearLayout_v);
        btn_ir_historico = (ImageButton) findViewById(R.id.img_b_vendas_d);
        btn_ir_historico.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, HistoricoActivity.class);
                startActivity(intent);
                finish();
            }
        });
        btn_ir_produtos = (ImageButton) findViewById(R.id.img_b_vendas_e);
        btn_ir_produtos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ProdutosActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
    public void criar_btn(){
        addBtn = new Button(this);
        addBtn.setText("luan");
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        addBtn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        linearL.addView(addBtn, lp);
    }
}

Here is ProductsActivty.java:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class ProdutosActivity extends AppCompatActivity {

    private ImageButton btn_ir_main;
    private Button btn_adc;
    /*private Button btn_limpar;
    private EditText nome_produtos;
    private EditText valor_produtos;*/

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

        btn_adc = findViewById(R.id.btn_adc);
        btn_ir_main = findViewById(R.id.img_b_produtos_d);
        btn_ir_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ProdutosActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        btn_adc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chamar();
                finish();
            }
        });
    }
    public static void chamar(){
        MainActivity main_act = new MainActivity();
        main_act.criar_btn();
    }
}

Here is LogCat:

Process: teste.luan.com.projeto_teste, PID: 10555
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
    at android.content.ContextWrapper.getResources(ContextWrapper.java:92)
    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
    at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
    at android.view.View.<init>(View.java:4038)
    at android.view.View.<init>(View.java:4169)
    at android.widget.TextView.<init>(TextView.java:947)
    at android.widget.Button.<init>(Button.java:112)
    at android.widget.Button.<init>(Button.java:105)
    at android.widget.Button.<init>(Button.java:101)
    at android.widget.Button.<init>(Button.java:97)
    at teste.luan.com.projeto_teste.MainActivity.criar_btn(MainActivity.java:46)
    at teste.luan.com.projeto_teste.ProdutosActivity.chamar(ProdutosActivity.java:44)
    at teste.luan.com.projeto_teste.ProdutosActivity$2.onClick(ProdutosActivity.java:37)
    at android.view.View.performClick(View.java:5697)
    at android.widget.TextView.performClick(TextView.java:10826)
    at android.view.View$PerformClick.run(View.java:22526)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
07-20 13:19:26.916 10555-10555/teste.luan.com.projeto_teste I/Process: Sending signal. PID: 10555 SIG: 9'
    
asked by anonymous 20.07.2018 / 18:41

0 answers