我已经阅读了许多关于开发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 MANIFEST -------
<?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
答案 0 :(得分:1)
首先,活动必须有空构造函数。如果需要参数,系统无法调用或实例化您的活动。删除参数或根本不定义构造函数。
卸下:
public EnterView (Model aModel, Controller aController) {
_model = aModel;
_controller = aController;
}
您将遇到的下一个错误是startClicked()
方法,正如praveenLal所说。它应该接受View
参数,该参数将代表调用方法时单击的按钮。
答案 1 :(得分:0)
请使用public void startClicked(View v){ ........ .... }
而不是public void startClicked()。