我无法运行我的Android应用程序进行调试。这是我第一次构建一个android应用程序,所以我不熟悉这个过程。我从LogCat
收到以下错误消息07-30 22:48:35.626: E/Trace(744): error opening trace file: No such file or directory (2)
07-30 22:49:42.285: E/AndroidRuntime(744): FATAL EXCEPTION: main
07-30 22:49:42.285: E/AndroidRuntime(744): java.lang.IllegalStateException: Could not execute method of the activity
07-30 22:49:42.285: E/AndroidRuntime(744): at android.view.View$1.onClick(View.java:3599)
07-30 22:49:42.285: E/AndroidRuntime(744): at android.view.View.performClick(View.java:4204)
07-30 22:49:42.285: E/AndroidRuntime(744): at android.view.View$PerformClick.run(View.java:17355)
07-30 22:49:42.285: E/AndroidRuntime(744): at android.os.Handler.handleCallback(Handler.java:725)
07-30 22:49:42.285: E/AndroidRuntime(744): at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 22:49:42.285: E/AndroidRuntime(744): at android.os.Looper.loop(Looper.java:137)
07-30 22:49:42.285: E/AndroidRuntime(744): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-30 22:49:42.285: E/AndroidRuntime(744): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 22:49:42.285: E/AndroidRuntime(744): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 22:49:42.285: E/AndroidRuntime(744): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-30 22:49:42.285: E/AndroidRuntime(744): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-30 22:49:42.285: E/AndroidRuntime(744): at dalvik.system.NativeStart.main(Native Method)
07-30 22:49:42.285: E/AndroidRuntime(744): Caused by: java.lang.reflect.InvocationTargetException
07-30 22:49:42.285: E/AndroidRuntime(744): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 22:49:42.285: E/AndroidRuntime(744): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 22:49:42.285: E/AndroidRuntime(744): at android.view.View$1.onClick(View.java:3594)
07-30 22:49:42.285: E/AndroidRuntime(744): ... 11 more
07-30 22:49:42.285: E/AndroidRuntime(744): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.wadi3mrad/com.example.wadi3mrad.biog}; have you declared this activity in your AndroidManifest.xml?
07-30 22:49:42.285: E/AndroidRuntime(744): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
07-30 22:49:42.285: E/AndroidRuntime(744): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
07-30 22:49:42.285: E/AndroidRuntime(744): at android.app.Activity.startActivityForResult(Activity.java:3370)
07-30 22:49:42.285: E/AndroidRuntime(744): at android.app.Activity.startActivityForResult(Activity.java:3331)
07-30 22:49:42.285: E/AndroidRuntime(744): at android.app.Activity.startActivity(Activity.java:3566)
07-30 22:49:42.285: E/AndroidRuntime(744): at android.app.Activity.startActivity(Activity.java:3534)
07-30 22:49:42.285: E/AndroidRuntime(744): at com.example.wadi3mrad.MainActivity.someFunction(MainActivity.java:55)
07-30 22:49:42.285: E/AndroidRuntime(744): ... 14 more
代码
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
public void someFunction(View v)
{
Intent myIntentd= new Intent();
switch (v.getId()) // get id of Button clicked
{
case (R.id.music):
myIntentd = new Intent(getApplicationContext(),
Albums.class);
break;
case (R.id.bio):
myIntentd = new Intent(getApplicationContext(),
biog.class);
break;
case (R.id.face):
myIntentd = new Intent(getApplicationContext(),
faceboom.class);
break;
case (R.id.info_but):
myIntentd = new Intent(getApplicationContext(),
info.class);
break;
}
startActivity(myIntentd);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:2)
没有任何代码,除了Jon Skeet之外,没有人可以告诉你问题出在哪里。但是从查看logcat开始,您尝试访问Array
的元素,而不是Array
这一行
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
给出了一个例外,说你试图访问索引3,但是数组的大小是3,所以最高可能的索引是2,因为Array
的基数为0。
此行(引用您的项目的异常之后的第一行)说明Activity
中哪一行以及该行所用的方法
at com.example.wadi3mrad.MainActivity.addListenerOnButton(MainActivity.java:81)
MainActivity
的第81行向listener
添加Button
。
我意识到这不是一个答案,但你知道他们说的是什么......“教一个人钓鱼......”
修改强>
让我们看看这是怎么回事。由于所有这些按钮几乎都做了一件事,更容易和更简洁的方法是在onClick
函数声明xml中的所有这些按钮然后你不需要所有的监听器和按钮阵列。
在你的xml中,对于每个按钮,添加类似 然后你只需要Java中的一个函数,而不需要所有其他的东西。所以你的Java代码看起来像android:onClick="someFunction"
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
public void someFunction(View v)
{
Intent myIntent = new Intent();
switch (v.getId()) // get id of Button clicked
{
case (R.id.music):
myIntent = new Intent(MainActivity.this,
Albums.class);
break;
case (R.id.bio):
myIntent = new Intent(MainActivity.this,
biog.class);
break;
case (R.id.face):
myIntent = new Intent(MainActivity.this,
faceboom.class);
break;
case (R.id.info_but):
myIntent = new Intent(MainActivity.this,
info.class);
break;
}
startActivity(myIntent);
}