2013-05-26 93 views
0

我已閱讀了很多關於開發Android應用程序的「常見」問題的線索。 我也注意到這一切都取決於各種問題,一些來自清單,一些來自Java類。不幸的是,應用程序已停止(第一次活動)

我會在這裏粘貼我的代碼,如果有人看到奇怪的東西,請告訴我。

正如你所看到的,我使用的是MVC模式。請告訴我你是否也需要Model &控制器java類。

----- ENTER VIEW JAVA CLASS -----

package kalle.jack.gui_tictactoe_android; 

    import android.os.Bundle; 
    import android.app.Activity; 
    import android.view.Menu; 
    import android.widget.Button; 
    import android.widget.TextView; 

    public class EnterView extends Activity { 

private Model _model; 
private Controller _controller; 

public EnterView (Model aModel, Controller aController) { 

    _model = aModel; 
    _controller = aController; 

} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_enter_view); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.enter_view, menu); 
    return true; 
} 

public void startClicked() { 

    TextView s = (TextView) findViewById(R.id.text1); 
    String player1 = (String) s.getText(); 

    TextView t = (TextView) findViewById(R.id.text2); 
    String player2 = (String) t.getText(); 

    Button b = (Button) findViewById(R.id.button1); 
    b.setId(10); 
    int a = b.getId(); 

    _controller.setNames(player1, player2); 
    _controller.buttonClicked(a); 


} 

} 

------ XML清單-------

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="kalle.jack.gui_tictactoe_android" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" 
    android:debuggable="true"> 
    <activity 
     android:name="kalle.jack.gui_tictactoe_android.EnterView" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="kalle.jack.gui_tictactoe_android.PlayView" 
     android:label="@string/title_activity_play_view" > 
    </activity> 
</application> 

</manifest> 

--- ------- XML activity_enter_view -----------

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".EnterView" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="49dp" 
    android:text="Enter your names below..." 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="85dp" 
    android:text="Start Game" 
    android:onClick="startClicked"/> 

<EditText 
    android:id="@+id/text1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="42dp" 
    android:ems="10" 
    android:inputType="textPersonName" 
    android:text="Player 1" /> 

<EditText 
    android:id="@+id/text2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:ems="10" 
    android:inputType="textPersonName" 
    android:text="Player 2" > 

    <requestFocus /> 
</EditText> 

</RelativeLayout> 

-----從logcat的--------

05-26 06:33:24.460: E/AndroidRuntime(943): FATAL EXCEPTION: main 
05-26 06:33:24.460: E/AndroidRuntime(943): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{kalle.jack.gui_tictactoe_android/kalle.jack.gui_tictactoe_android.EnterView}: java.lang.InstantiationException: can't instantiate class kalle.jack.gui_tictactoe_android.EnterView; no empty constructor 
05-26 06:33:24.460: E/AndroidRuntime(943): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106) 
05-26 06:33:24.460: E/AndroidRuntime(943): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
05-26 06:33:24.460: E/AndroidRuntime(943): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
05-26 06:33:24.460: E/AndroidRuntime(943): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
05-26 06:33:24.460: E/AndroidRuntime(943): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-26 06:33:24.460: E/AndroidRuntime(943): at android.os.Looper.loop(Looper.java:137) 
05-26 06:33:24.460: E/AndroidRuntime(943): at android.app.ActivityThread.main(ActivityThread.java:5041) 
05-26 06:33:24.460: E/AndroidRuntime(943): at java.lang.reflect.Method.invokeNative(Native Method) 
05-26 06:33:24.460: E/AndroidRuntime(943): at java.lang.reflect.Method.invoke(Method.java:511) 
05-26 06:33:24.460: E/AndroidRuntime(943): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
05-26 06:33:24.460: E/AndroidRuntime(943): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
05-26 06:33:24.460: E/AndroidRuntime(943): at dalvik.system.NativeStart.main(Native Method) 
05-26 06:33:24.460: E/AndroidRuntime(943): Caused by: java.lang.InstantiationException: can't instantiate class kalle.jack.gui_tictactoe_android.EnterView; no empty constructor 
05-26 06:33:24.460: E/AndroidRuntime(943): at java.lang.Class.newInstanceImpl(Native Method) 
05-26 06:33:24.460: E/AndroidRuntime(943): at java.lang.Class.newInstance(Class.java:1319) 
05-26 06:33:24.460: E/AndroidRuntime(943): at android.app.Instrumentation.newActivity(Instrumentation.java:1054) 
05-26 06:33:24.460: E/AndroidRuntime(943): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 
05-26 06:33:24.460: E/AndroidRuntime(943): ... 11 more 
+2

請使用LogCat檢查與您的「崩潰」相關的Java堆棧跟蹤。 – CommonsWare

+0

@CommonsWare - 啊,我會直接粘貼它! – Jack

+0

請把控制器代碼 –

回答

1

首先,活動必須有空的構造函數。如果系統需要參數,系統無法調用或實例化您的活動。刪除參數或根本不定義構造函數。

刪除:

public EnterView (Model aModel, Controller aController) { 
    _model = aModel; 
    _controller = aController; 
} 

你會遇到下一個錯誤將是startClicked()方法,如praveenLal說。它應該接受一個View參數,該參數表示調用該方法時單擊的按鈕。

+0

哇,這使得應用程序開始!非常感謝,也感謝praveenLal! – Jack

0

請使用公共無效startClicked(視圖v){ ........ .... }

,而不是公共無效startClicked()。

+0

我會嘗試一個!將回答與答案... – Jack

相關問題