Android Studio Error: Unable to start Activity

Asked

Viewed 46 times

-1

I’m doing a basic CRUD with Realmdb. I made a list, and when some item of this list is selected, it is to open an Update Activity, where you can change the selected data. The problem is that the "secondary" Activity doesn’t even open. Run says the problem is a Nullpointerexception, but I have no idea where it is. I’ve checked every corner of the code

This is the code of the List. At the end I put a Clicklistener that should open the new Activity from an Intent

public class CustomAdapter extends BaseAdapter {
    Context c;
    ArrayList<People> people;

    public CustomAdapter(Context c, ArrayList<People> people) {
        this.c = c;
        this.people = people;
    }

    @Override
    public int getCount() {
        return people.size();
        //tamanho da lista
    }

    @Override
    public Object getItem(int position) {
        return people.get(position);
        //pega o item de uma posição específica
    }

    @Override
    public long getItemId(int position) {
        return position;
        //pega a posição de um item específico
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.list_item, parent, false);

        TextView txtName;
        txtName = view.findViewById(R.id.txt_name);

        People p = (People) this.getItem(position);

        final int numPosition = p.getPeople_id();
        txtName.setText(p.getPeople_name());

        //OnClick
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(c, DetailActivity.class);
                intent.putExtra("numPosition", numPosition);
                c.startActivity(intent);
            }
        });

        return view;
    }
}

And this is the activity code I want you to open but don’t open

public class DetailActivity extends AppCompatActivity {
    private Realm realm;
    private EditText editNameDetail;
    private Button btnUpdate;
    private People people;

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

        realm = Realm.getDefaultInstance();
        editNameDetail = findViewById(R.id.edit_nameDetail);
        btnUpdate = findViewById(R.id.btn_update);

        Intent getIntent = getIntent();
        int position = getIntent.getIntExtra("numPosition", 0);

        people = realm.where(People.class).equalTo("people_id", position).findFirst();
        editNameDetail.setText(people.getPeople_name());

        btnUpdate.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                updateData();
                onBackPressed();
            }
        });
    }

    private void updateData(){
        realm.beginTransaction();
        people.setPeople_name(editNameDetail.getText().toString());
        realm.commitTransaction();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}

And finally, that’s the mistake you’re making

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.realmtest1, PID: 17712
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.realmtest1/com.example.realmtest1.DetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3308)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7562)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
        at com.example.realmtest1.DetailActivity.onCreate(DetailActivity.java:39)
        at android.app.Activity.performCreate(Activity.java:7893)
        at android.app.Activity.performCreate(Activity.java:7880)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3283)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:224) 
        at android.app.ActivityThread.main(ActivityThread.java:7562) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
I/Process: Sending signal. PID: 17712 SIG: 9

Activity XML that is not opening

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".DetailActivity"
    android:padding="15dp">

    <EditText
        android:id="@+id/edit_nameDetail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Name"
        android:layout_marginVertical="20dp"
        />

    <Button
        android:id="@+id/btn_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Update"/>


</LinearLayout>

If someone can help me, please give me a hint. It may be something simple or it may be something complex, I don’t know. Thanks in advance

  • Its layout activity_main really owns a EditText with the id edit_nameDetail? The error seems to be on the line editNameDetail.setText(people.getPeople_name());

  • Yes, yes. The XML of this Activity has an Edittext with that name. I’ll put the XML in the original post tbm, a moment

1 answer

0

I managed to fix it. At the beginning of the project I put "setContentView(R.layout.activity_main);" Which makes no sense, since the activity of this project refers to xml "detail_activity", and not to "activity_main"

It is not possible to search for elements of one Activity being in another!!! Do not make the same mistake as me

Browser other questions tagged

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