问题
There is no error in my code but the application force closes at the start itself. Following is the code.
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;
}
}
This is 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
Could anybody please tell what error is there ?
回答1:
I think there is an error in your code. On the 4th line it states that "addview is not supported in adapterview." It looks like that is indicating you should wrap that in a try/catch block and catch that error so that it is not force closing your app or you need to figure out why that addView is not supported with the adapterview. Also, from that line that says "caused by" you can see the error repeated.
回答2:
It looks like you've got an error in your XML layout file (R.layout.main). It is crashing as it's attempting to inflate the view.
Take a look at the adapter view you have defined in there. It would help if you could post this file.
来源:https://stackoverflow.com/questions/11495023/force-close-in-android-app