Home > Back-end >  My android app keeps crashing even after checking for errors in debugging and tests
My android app keeps crashing even after checking for errors in debugging and tests

Time:09-26

activity_main.xml:

<?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:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@ id/downloadfile_activity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="25dp"
        android:text="Download File"
        android:textAllCaps="false" />

    <Button
        android:id="@ id/viewfile_activity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="25dp"
        android:text="View File"
        android:textAllCaps="false" />
</LinearLayout>

MainActivity.java:

package com.example.galleryapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button downloadfileactivity = findViewById(R.id.downloadfile_activity);
    Button viewfileactivity = findViewById(R.id.viewfile_activity);

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

        downloadfileactivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, DownloadFileActivity.class));
                finish();
            }
        });

        viewfileactivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, ViewFileActivity.class));
                finish();
            }
        });

    }
}

I've been trying to make a simple layout of an activity using Linear Layout, but whenever I try to implement the onClickListeners to the buttons in the layout and it crashes, I debug and run tests and everything turns out perfect. I try to rerun the app but it still crashes. I've tried to clean project and then rerun the app. NOTHING CHANGES. I have even tried to copy only that part of code, which I 1000% know it would run, to a new project. Even that project is crashing!-_- I am always forced to keep deleting the project that keeps crashing and creating new project. And if that project crashes as well, another one. If that one crashes too, then another one! I know this sounds very childish and such but this is just frustating to me, keeping in mind that I've been working in android development in Android Studio for the past 2 years :((

ps: ive been running projects on my phone(Android 9) through a USB cable.

CodePudding user response:

Set your Button after setContentView

Button downloadfileactivity;
Button viewfileactivity;

After setContentView(R.layout.activity_main);

downloadfileactivity = findViewById(R.id.downloadfile_activity);
viewfileactivity = findViewById(R.id.viewfile_activity);

CodePudding user response:

Move downloadfileactivity & viewfileactivity initialize to onCreate method.

public class MainActivity extends AppCompatActivity {
Button downloadfileactivity;
Button viewfileactivity;

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

    downloadfileactivity = findViewById(R.id.downloadfile_activity);
    viewfileactivity = findViewById(R.id.viewfile_activity);

    downloadfileactivity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, DownloadFileActivity.class));
            finish();
        }
    });

    viewfileactivity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, ViewFileActivity.class));
            finish();
        }
    });

}}

CodePudding user response:

Please move your findViewById(R.id.xyz); into the on create method;

  Button downloadfile_activity;//= findViewById(R.id.downloadfile_activity);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    downloadfile_activity = findViewById(R.id.downloadfile_activity);
    downloadfile_activity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
        }
    });
}
  • Related