我的代码中没有错误,但应用程序强制在启动时关闭。以下是代码。
package com.amit.wozzle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private Button b6;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button) findViewById(R.id.g1);
b2 = (Button) findViewById(R.id.g2);
b3 = (Button) findViewById(R.id.g3);
b4 = (Button) findViewById(R.id.g4);
b5 = (Button) findViewById(R.id.g5);
b6 = (Button) findViewById(R.id.g6);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent r1= new Intent("com.amit.wozzle.GAMEPLAY");
r1.putExtra("choice", 1);
startActivity(r1);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent r1= new Intent("com.amit.wozzle.GAMEPLAY");
r1.putExtra("choice", 2);
startActivity(r1);
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent r1= new Intent("com.amit.wozzle.GAMEPLAY");
r1.putExtra("choice", 3);
startActivity(r1);
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent r1= new Intent("com.amit.wozzle.GAMEPLAY");
r1.putExtra("choice", 4);
startActivity(r1);
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent r1= new Intent("com.amit.wozzle.GAMEPLAY");
r1.putExtra("choice", 5);
startActivity(r1);
}
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent r1= new Intent("com.amit.wozzle.GAMEPLAY");
r1.putExtra("choice", 6);
startActivity(r1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是logcat。
07-15 12:57:37.632: D/AndroidRuntime(666): Shutting down VM
07-15 12:57:37.632: W/dalvikvm(666): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-15 12:57:37.642: E/AndroidRuntime(666): FATAL EXCEPTION: main
07-15 12:57:37.642: E/AndroidRuntime(666): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amit.wozzle/com.amit.wozzle.MainActivity}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
07-15 12:57:37.642: E/AndroidRuntime(666): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-15 12:57:37.642: E/AndroidRuntime(666): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-15 12:57:37.642: E/AndroidRuntime(666): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-15 12:57:37.642: E/AndroidRuntime(666): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-15 12:57:37.642: E/AndroidRuntime(666): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 12:57:37.642: E/AndroidRuntime(666): at android.os.Looper.loop(Looper.java:123)
07-15 12:57:37.642: E/AndroidRuntime(666): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-15 12:57:37.642: E/AndroidRuntime(666): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 12:57:37.642: E/AndroidRuntime(666): at java.lang.reflect.Method.invoke(Method.java:521)
07-15 12:57:37.642: E/AndroidRuntime(666): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-15 12:57:37.642: E/AndroidRuntime(666): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-15 12:57:37.642: E/AndroidRuntime(666): at dalvik.system.NativeStart.main(Native Method)
07-15 12:57:37.642: E/AndroidRuntime(666): Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
07-15 12:57:37.642: E/AndroidRuntime(666): at android.widget.AdapterView.addView(AdapterView.java:461)
07-15 12:57:37.642: E/AndroidRuntime(666): at android.view.LayoutInflater.rInflate(LayoutInflater.java:622)
07-15 12:57:37.642: E/AndroidRuntime(666): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
07-15 12:57:37.642: E/AndroidRuntime(666): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
07-15 12:57:37.642: E/AndroidRuntime(666): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-15 12:57:37.642: E/AndroidRuntime(666): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
07-15 12:57:37.642: E/AndroidRuntime(666): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
07-15 12:57:37.642: E/AndroidRuntime(666): at android.app.Activity.setContentView(Activity.java:1647)
07-15 12:57:37.642: E/AndroidRuntime(666): at com.amit.wozzle.MainActivity.onCreate(MainActivity.java:23)
07-15 12:57:37.642: E/AndroidRuntime(666): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-15 12:57:37.642: E/AndroidRuntime(666): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-15 12:57:37.642: E/AndroidRuntime(666): ... 11 more
有人可以告诉我们有什么错误吗?
答案 0 :(得分:2)
我认为您的代码中存在错误。在第4行,它声明“适配器视图中不支持addview”。看起来这表明你应该将它包装在try / catch块中并捕获该错误,以便它不会强制关闭你的应用程序,或者你需要找出为什么addView不支持adapterview。 此外,从“由...引起”的那一行中,您可以看到错误重复。
答案 1 :(得分:2)
您的XML布局文件(R.layout.main)中出现错误。当它试图给视图充气时,它正在崩溃。
查看您在那里定义的适配器视图。如果您可以发布此文件,将会有所帮助。