我的Android应用程序有问题。它在启动时崩溃,我对LogCat了解不多。任何人都建议我解决问题的最佳方法。我不知道出了什么问题。
这是一个循环音频的MediaPlayer应用程序。
在我写作之前(setOnClickListerner)它没有崩溃。
什么可能导致问题?
MainActivity.java:
package com.jobyreuben.dholakloops;
public class MainActivity extends AppCompatActivity {
Button dholak01;
Button dholak02;
Button dholak03;
Button dholak04;
Button dholak05;
Button dholak06;
Button dholak07;
Button dholak08;
Button dholak09;
Button dholak10;
Button dholak11;
Button dholak12;
Button dholak13;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
dholak01 = (Button) findViewById(R.id.button_1);
final MediaPlayer dk1 = MediaPlayer.create(MainActivity.this, R.raw.dholak01);
dholak02 = (Button) findViewById(R.id.button_2);
final MediaPlayer dk2 = MediaPlayer.create(MainActivity.this, R.raw.dholak02);
dholak03 = (Button) findViewById(R.id.button_3);
final MediaPlayer dk3 = MediaPlayer.create(MainActivity.this, R.raw.dholak03);
dholak04 = (Button) findViewById(R.id.button_4);
final MediaPlayer dk4 = MediaPlayer.create(MainActivity.this, R.raw.dholak04);
dholak05 = (Button) findViewById(R.id.button_5);
final MediaPlayer dk5 = MediaPlayer.create(MainActivity.this, R.raw.dholak05);
dholak07 = (Button) findViewById(R.id.button_6);
final MediaPlayer dk6 = MediaPlayer.create(MainActivity.this, R.raw.dholak06);
dholak08 = (Button) findViewById(R.id.button_7);
final MediaPlayer dk7 = MediaPlayer.create(MainActivity.this, R.raw.dholak07);
dholak09 = (Button) findViewById(R.id.button_8);
final MediaPlayer dk8 = MediaPlayer.create(MainActivity.this, R.raw.dholak08);
dholak10 = (Button) findViewById(R.id.button_9);
final MediaPlayer dk9 = MediaPlayer.create(MainActivity.this, R.raw.dholak09);
dholak11 = (Button) findViewById(R.id.button_10);
final MediaPlayer dk10 = MediaPlayer.create(MainActivity.this, R.raw.dholak10);
dholak12 = (Button) findViewById(R.id.button_11);
final MediaPlayer dk11 = MediaPlayer.create(MainActivity.this, R.raw.dholak11);
dholak13 = (Button) findViewById(R.id.button_12);
final MediaPlayer dk12 = MediaPlayer.create(MainActivity.this, R.raw.dholak12);
dholak01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dk1.isPlaying()) {
dk1.pause();
dholak01.setBackgroundResource(R.drawable.dholak);
} else {
dk1.start();
dholak01.setBackgroundResource(R.drawable.dholakp);
dk1.setLooping(true);
}
}
});
dholak02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dk2.isPlaying()) {
dk2.pause();
dholak02.setBackgroundResource(R.drawable.dholak);
} else {
dk2.start();
dholak02.setBackgroundResource(R.drawable.dholakp);
dk2.setLooping(true);
}
}
});
dholak03.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dk3.isPlaying()) {
dk3.pause();
dholak03.setBackgroundResource(R.drawable.dholak);
} else {
dk3.start();
dholak03.setBackgroundResource(R.drawable.dholakp);
dk3.setLooping(true);
}
}
});
dholak04.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dk4.isPlaying()) {
dk4.pause();
dholak04.setBackgroundResource(R.drawable.dholak);
} else {
dk4.start();
dholak04.setBackgroundResource(R.drawable.dholakp);
dk4.setLooping(true);
}
}
});
dholak05.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dk5.isPlaying()) {
dk5.pause();
dholak05.setBackgroundResource(R.drawable.dholak);
} else {
dk5.start();
dholak05.setBackgroundResource(R.drawable.dholakp);
dk5.setLooping(true);
}
}
});
dholak06.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dk6.isPlaying()) {
dk6.pause();
dholak06.setBackgroundResource(R.drawable.dholak);
} else {
dk6.start();
dholak06.setBackgroundResource(R.drawable.dholakp);
dk6.setLooping(true);
}
}
});
dholak07.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dk7.isPlaying()) {
dk7.pause();
dholak07.setBackgroundResource(R.drawable.dholak);
} else {
dk7.start();
dholak07.setBackgroundResource(R.drawable.dholakp);
dk7.setLooping(true);
}
}
});
dholak08.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dk8.isPlaying()) {
dk8.pause();
dholak08.setBackgroundResource(R.drawable.dholak);
} else {
dk8.start();
dholak08.setBackgroundResource(R.drawable.dholakp);
dk8.setLooping(true);
}
}
});
dholak09.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dk9.isPlaying()) {
dk9.pause();
dholak09.setBackgroundResource(R.drawable.dholak);
} else {
dk9.start();
dholak09.setBackgroundResource(R.drawable.dholakp);
dk9.setLooping(true);
}
}
});
dholak10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dk10.isPlaying()) {
dk10.pause();
dholak10.setBackgroundResource(R.drawable.dholak);
} else {
dk10.start();
dholak10.setBackgroundResource(R.drawable.dholakp);
dk10.setLooping(true);
}
}
});
dholak11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dk11.isPlaying()) {
dk11.pause();
dholak11.setBackgroundResource(R.drawable.dholak);
} else {
dk11.start();
dholak11.setBackgroundResource(R.drawable.dholakp);
dk11.setLooping(true);
}
}
});
dholak12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dk12.isPlaying()) {
dk12.pause();
dholak12.setBackgroundResource(R.drawable.dholak);
} else {
dk12.start();
dholak12.setBackgroundResource(R.drawable.dholakp);
dk12.setLooping(true);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.id_about) {
Intent intentabout = new Intent(MainActivity.this, About.class);
startActivity(intentabout);
return true;
}
if (id == R.id.id_contact)
{
Intent intentabout = new Intent(MainActivity.this, Contact.class);
startActivity(intentabout);
return true;
}
if (id == R.id.id_otherapps) {
return true;
}
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.id_about) {
Intent intentabout = new Intent(MainActivity.this, About.class);
startActivity(intentabout);
return true;
}
if (id == R.id.id_contact)
{
Intent intentabout = new Intent(MainActivity.this, Contact.class);
startActivity(intentabout);
return true;
}
if (id == R.id.id_otherapps) {
return true;
}
return true;
}
}
这是LogCat日志:
--------- beginning of crash
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.jobyreuben.dholakloops.MainActivity.onCreate(MainActivity.java:149)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:0)
你错过了dholak04。将dholak04按钮引用到xml按钮。
更改dholak05 = (Button) findViewById(R.id.button_4);
到
dholak04 = (Button) findViewById(R.id.button_4);
而且你在初始化时犯了几个错误。例如init 5与6