0

I am a novice Android Developer. I have created a package-private class which extends Application, and contains the required code for specific functions. I basically want to display if the user-selected button is the correct choice or not, via a toast. Since I have to call this code for many activities, I just created a package-private class for it. However, on clicking the button, the app crashes. Please see the code given below for reference.

I cannot change the onClick method to non-static because if I do that, Android Studio shows an error, and if I change it to static, I am unable to use the method getApplicationContext(), because it is not accessible inside static blocks.

I think that using view.getContext() is causing the crash.

Is there any workaround, or a solution? Your help would be greatly appreciated. Thanks :)

Here is the code for your reference.

activity.java:

public class activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(functions.select);
        functions.makeLayout(expression, buttons);
    }
}

Here is the code which crashes the app.

functions.java:

class functions extends Application {

    private static int idx;

    public static View.OnClickListener select=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int selected_index=(int) view.getTag();
            if(selected_index==idx)
            {
                Toast.makeText(view.getContext(), "Correct.", Toast.LENGTH_LONG).show();
                ((Button) view).setTextColor(Color.GREEN);
            }
            else
            {
                Toast.makeText(view.getContext(), "Wrong.", Toast.LENGTH_LONG).show();
                ((Button) view).setTextColor(Color.RED);
            }
        }
    };
090
  • 97
  • 10
  • 2
    This will help you get the crash: [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Buddy Christ May 14 '20 at 15:34
  • @peprumo sir, the stack trace (in this case, logcat) doesn't give any exceptions, or even line numbers. It only gives an error: `20:36:48.948 21734-21762/com.ex.myproj E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array` – 090 May 14 '20 at 15:42
  • 1
    Omg:D What are you doing. It would be better if you read something about Java language and android SDK before write something. Application class is not a place for such things. What do you want to achieve? – Eugene Troyanskii May 14 '20 at 15:43
  • @EugeneTroyanskii I have written one function to reduce redundant code. Is there any other way to retrieve context? It seems way confusing. I want to display a toast on button Click to see if the user choice is correct or not. – 090 May 14 '20 at 15:46
  • @Sanskar as for context. You can get it for view that is inner param in onClick method view.getContext(). The Application class usually using for some actions that have to be for correct work of your app like initialization of Database etc... Remove your class functions. For your purpose create class CustomOnClick implements View.OnClickListener() and make your Toask inside of implementation. But in real cases usually you have to implement onClick exactly in activity/Fragment where this button exist with specific reaction on user action. – Eugene Troyanskii May 14 '20 at 15:53

1 Answers1

1

Okay, I figured out that it was not view.getContext() but the line int selected_index=(int) view.getTag(); which was causing the crash. I solved it first making it into a string and then int by using the following code:

String selected_index=view.getTag.toString();
int sidx=Integer.parseInt(selected_index);
090
  • 97
  • 10