The studio does not write anything. When running in the emulator crashes. Something pissed off with buttons. Help. Thank.

MainActivity.java

public class MainActivity extends AppCompatActivity { private SoundPool sp; private int sound1; private int sound2; private int sound3; private int sound4; private int sound5; private int sound6; private int sound7; Button btn1 = (Button) findViewById(R.id.btn1); Button btn2 = (Button) findViewById(R.id.btn2); Button btn3 = (Button) findViewById(R.id.btn3); Button btn4 = (Button) findViewById(R.id.btn4); Button btn5 = (Button) findViewById(R.id.btn5); Button btn6 = (Button) findViewById(R.id.btn6); Button btn7 = (Button) findViewById(R.id.btn7); Button btn8 = (Button) findViewById(R.id.btn8); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sp = new SoundPool(2, AudioManager.STREAM_MUSIC,0); sound1 = sp.load(getApplicationContext(),R.raw.sound1,1); sound2 = sp.load(getApplicationContext(),R.raw.sound2,1); sound3 = sp.load(getApplicationContext(),R.raw.sound3,1); sound4 = sp.load(getApplicationContext(),R.raw.sound4,1); sound5 = sp.load(getApplicationContext(),R.raw.sound5,1); sound6 = sp.load(getApplicationContext(),R.raw.sound6,1); sound7 = sp.load(getApplicationContext(),R.raw.sound7,1); } public void stopbtn (View view) { sp.stop(sound1); sp.stop(sound2); sp.stop(sound3); sp.stop(sound4); sp.stop(sound5); sp.stop(sound6); sp.stop(sound7); btn1.setVisibility(View.VISIBLE); btn2.setVisibility(View.VISIBLE); btn3.setVisibility(View.VISIBLE); btn4.setVisibility(View.VISIBLE); btn5.setVisibility(View.VISIBLE); btn6.setVisibility(View.VISIBLE); btn7.setVisibility(View.VISIBLE); btn8.setVisibility(View.GONE); } public void playsound1 (View view) { btn1.setVisibility(View.GONE); btn2.setVisibility(View.GONE); btn3.setVisibility(View.GONE); btn4.setVisibility(View.GONE); btn5.setVisibility(View.GONE); btn6.setVisibility(View.GONE); btn7.setVisibility(View.GONE); btn8.setVisibility(View.VISIBLE); sp.play(sound1,0.1f,0.1f,0,-1, 0f); } public void playsound2 (View view) { btn1.setVisibility(View.GONE); btn2.setVisibility(View.GONE); btn3.setVisibility(View.GONE); btn4.setVisibility(View.GONE); btn5.setVisibility(View.GONE); btn6.setVisibility(View.GONE); btn7.setVisibility(View.GONE); btn8.setVisibility(View.VISIBLE); sp.play(sound2,0.1f,0.1f,0,-1,0f); } public void playsound3 (View view) { btn1.setVisibility(View.GONE); btn2.setVisibility(View.GONE); btn3.setVisibility(View.GONE); btn4.setVisibility(View.GONE); btn5.setVisibility(View.GONE); btn6.setVisibility(View.GONE); btn7.setVisibility(View.GONE); btn8.setVisibility(View.VISIBLE); sp.play(sound3,0.1f,0.1f,0,-1,0f); } public void playsound4 (View view) { btn1.setVisibility(View.GONE); btn2.setVisibility(View.GONE); btn3.setVisibility(View.GONE); btn4.setVisibility(View.GONE); btn5.setVisibility(View.GONE); btn6.setVisibility(View.GONE); btn7.setVisibility(View.GONE); btn8.setVisibility(View.VISIBLE); sp.play(sound4,0.1f,0.1f,0,-1,0f); } public void playsound5 (View view) { btn1.setVisibility(View.GONE); btn2.setVisibility(View.GONE); btn3.setVisibility(View.GONE); btn4.setVisibility(View.GONE); btn5.setVisibility(View.GONE); btn6.setVisibility(View.GONE); btn7.setVisibility(View.GONE); btn8.setVisibility(View.VISIBLE); sp.play(sound5,0.1f,0.1f,0,-1,0f); } public void playsound6 (View view) { btn1.setVisibility(View.GONE); btn2.setVisibility(View.GONE); btn3.setVisibility(View.GONE); btn4.setVisibility(View.GONE); btn5.setVisibility(View.GONE); btn6.setVisibility(View.GONE); btn7.setVisibility(View.GONE); btn8.setVisibility(View.VISIBLE); sp.play(sound6,0.1f,0.1f,0,-1,0f); } public void playsound7 (View view) { btn1.setVisibility(View.GONE); btn2.setVisibility(View.GONE); btn3.setVisibility(View.GONE); btn4.setVisibility(View.GONE); btn5.setVisibility(View.GONE); btn6.setVisibility(View.GONE); btn7.setVisibility(View.GONE); btn8.setVisibility(View.VISIBLE); sp.play(sound7,0.1f,0.1f,0,-1,0f); } } 

activity_main.xml

 <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="12dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Drum Loops:" android:paddingRight="12dp"/> <Button android:background="@drawable/blue_button" android:onClick="playsound1" android:id="@+id/btn1" android:text="1" android:layout_width="48dp" android:layout_height="48dp" /> <Button android:layout_marginLeft="6dp" android:background="@drawable/blue_button" android:text="2" android:onClick="playsound2" android:id="@+id/btn2" android:layout_width="48dp" android:layout_height="48dp" /> <Button android:layout_marginLeft="6dp" android:background="@drawable/blue_button" android:text="3" android:onClick="playsound3" android:id="@+id/btn3" android:layout_width="48dp" android:layout_height="48dp" /> <Button android:layout_marginLeft="6dp" android:background="@drawable/blue_button" android:text="4" android:onClick="playsound4" android:id="@+id/btn4" android:layout_width="48dp" android:layout_height="48dp" /> <Button android:layout_marginLeft="6dp" android:background="@drawable/blue_button" android:text="5" android:onClick="playsound5" android:id="@+id/btn5" android:layout_width="48dp" android:layout_height="48dp" /> <Button android:layout_marginLeft="6dp" android:background="@drawable/blue_button" android:text="6" android:onClick="playsound6" android:id="@+id/btn6" android:layout_width="48dp" android:layout_height="48dp" /> <Button android:layout_marginLeft="6dp" android:background="@drawable/blue_button" android:text="7" android:onClick="playsound7" android:id="@+id/btn7" android:layout_width="48dp" android:layout_height="48dp" /> <Button android:layout_marginLeft="6dp" android:visibility="gone" android:background="@drawable/blue_button" android:text="STOP" android:onClick="stopbtn" android:id="@+id/btn8" android:layout_width="48dp" android:layout_height="48dp" /> </LinearLayout> 

Closed due to the fact that off-topic participants Denis , 0xdb , user192664, aleksandr barakin , user300000 11 Oct '18 at 9:29 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - 0xdb, Community Spirit, Let's say Pie
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Why removed the code that is relevant to the question? - Enikeyschik pm

1 answer 1

Method findViewById(); looking for views in markup It is loaded by the setContentView method, which is called in the onCreate() method called by the system. And the method you can be called until the class is loaded. And in the process of loading a class, its fields must first be initialized.

Those. You are trying to initialize the fields of your class guaranteed before they can be initialized with a non- null value.

Total:

Simply transfer your calls to findViewById(); after setContentView