The problem is that the application will have several diologic windows, and I want to bring them into a separate class. But I also catch nullpointerexception using setContentView (). I'm just starting to get acquainted with android studio and I think that there should be a way to call setContentView () from other classes. Thank you in advance.

public class Dialogs extends MainActivity { public void newLanguageDialog(Context context , final View view){ final AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setView(R.layout.dialog_newblock); builder.setCancelable(false); builder.setTitle("111"); builder.setMessage("222"); final EditText input = new EditText(context); builder.setView(input); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { setContentView(R.layout.main_one); dialog.cancel(); } }); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { String a = input.getText().toString(); if (a != null && a.length() > 0) { MainActivity.s = a; //setContentView(R.layout.activity_main); dialog.cancel(); } else { setContentView(R.layout.main_one); dialog.cancel(); } } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } 

}

  • So far I have thought of doing this: pass an instance of the Main class to the method and call through it a new method in the Main class. New method: public void ssetContent (int value) {setContentView (value); } - Evgeny Lischenko
  • Before you continue in this direction, you should read this and abandon such an idea until too much energy is spent bad solution - pavlofff

1 answer 1

You’ve got it screwed up, it’s impossible to understand what goal you’re trying to achieve, for the builder, the setView method is called twice, and the setContentView is called by the activation class. As for editText, then it would be best to edit the text inside the dialog_newblock.xml, assign it an id (for example, edit_text) and do something like this

 public class Dialogs extends MainActivity { public void newLanguageDialog(Context context , final View view){ View view = LayoutInflater.from(context).inflate(R.layout.dialog_newblock); EditText input = (EditText) view.findViewById(R.id.edit_text); final AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setView(view); builder.setCancelable(false); builder.setTitle("111"); builder.setMessage("222"); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // do smth // dialog.cancel(); - не надо вызывать, вызовется само } }); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { String a = input.getText().toString(); if (a.length() > 0) { MainActivity.s = a; //setContentView(R.layout.activity_main); // dialog.cancel(); } else { // setContentView(R.layout.main_one); // dialog.cancel(); } } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } } 

At what point is the dialogue called? And at what point is setContentView called? To be honest, it seems to me a strange idea to change the contents of an activation by pressing a button (I have never done this, not even sure that it will work), it is better to make an activation container and change fragments inside it

And on your question it is not entirely clear what you are trying to achieve.

UPDATED:

Created a project and tried it - it worked)) Very nice. However, I will try to convince you - this is a bad practice, handling several markup in one activity. The project has a tendency to grow, and for each markup you will have to combine the view binding into a separate logic, which will be within the same class (your activity). In a toga, it will grow into many hundreds and thousands of few manageable lines of code. It is more correct to do several activations, or one activit with a container (FrameLayout, for example) where to add different fragments, into which the logic of each individual page will be iasked. And there is nothing terrible, because you will have several classes, not just one. And what's more, if you go further, the presentation logic is generally better to separate from the business of logic, but you will find out later when you get acquainted with the architectural patterns or such a wonderful thing as Android Data Binding.

  • Thank you very much for the answer. I am ashamed to admit, but I have not yet understood how to create separate Java classes for each activity and to interact between them. This is my first android application. And I would be very grateful if you would advise me good courses on android in Russian and with tasks. Now I am studying Java on JavaRash. - Yevgeny Lishchenko
  • The dialogue is called from MainActivity when switching to another activation and prompts the user to enter a certain string for further use in the application. And if the user clicks cancel or has not entered a single character, then he should again fall into MineActivity, and if entered, then move to a new activation. P.S. In this comment, by activation, I mean the application screen. Sorry, while confusing in terms. - Yevgeny Lishchenko
  • @ Yevgeny Lishchenko In this case, we create a separate class of activations, inherit it from the Activity, in the onCreate via setContentView we slip the second markup. We add activit to the manifest (the intent filter is not needed). Then in the click-to-key dialog we remove everything except if (input.getText (). Length ()> 0) {Intent intent = new Intent (context, SecondActivity.class); context.startActivity (intent); } by clicking on ok. Probably there are a lot of resources, I once used startandroid and developer.alexanderklimov.ru/android, I hope admins will not consider this as an advertisement :) Good luck - iamthevoid
  • Everything that I wrote is written on my knee, but then you yourself will understand for yourself how to and how best. With fragments, everything is also quite simple, but they have their own characteristics, so it’s better to contact them later - iamthevoid
  • @ Yevgeny Lishchenko And it’s very good to understand what you need, divide the task into small components and google. "how to create dialog", "how to add action to dialog", "how to open another activity". Such small things are very easy to find and help better than any textbooks - iamthevoid