2013-05-26 68 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

相关问题