2012-07-15 59 views
0

在我的代碼中沒有錯誤,但應用程序強制在開始時自動關閉。以下是代碼。在android應用程序中強制關閉

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 

請問有人能告訴我有什麼錯誤嗎?

+1

請顯示此活動的佈局文件。 – Femaref 2012-07-15 19:47:48

+0

R.layout.main中的XML是什麼? – 2012-07-15 19:52:57

+0

我認爲在'main.xml'中你有一個類似' yourbuttons'的自定義視圖'使它像'yourButtons ' – 2012-07-15 19:53:24

回答

2

我認爲你的代碼有錯誤。在第四行中,它指出「addview在adapterview中不受支持」。它看起來像表示你應該將它包裝在try/catch塊中並捕獲該錯誤,以便它不強制關閉你的應用程序,或者你需要弄清楚爲什麼addView不支持adapterview。 另外,從那個表示「由...引起」的行中,您可以看到重複的錯誤。

+1

我需要爲我正在使用的'GridView'創建適配器。 – 2012-07-15 20:13:38

+0

不是一個愚蠢的錯誤。你找出了錯誤,很好的工作 – 2012-07-15 20:23:00

2

看起來您的XML佈局文件(R.layout.main)中有錯誤。它試圖膨脹視圖時崩潰了。

查看您在那裏定義的適配器視圖。如果你可以發佈這個文件,這將有所幫助。