私は最近、日食でアンドロイドを使い始めました。Model-View-Controller (MVC) 構造を維持する必要があります。
私の目標は、画面に長方形のボックスを表示することです。現時点で入手したのは、4 つの画面 (メイン メニューとそれにリンクされた他の 3 つの画面) を持つアプリケーションです。ボックスの実装は、これら 3 つの画面 (ボード) のいずれかに対するものです。
board.java コード
package com.example.connectfour;
import com.example.connectfour.model.Gaps;
import com.example.connectfour.view.ConnectFourView;
imports[..]
public class Board extends Activity {
private Button btnExit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
connectfourView = new ConnectFourView(this);
setContentView(R.layout.board);
btnExit = (Button)findViewById(R.id.btn4);
btnExit.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent startMain = new Intent(Board.this, MainActivity.class);
startActivity(startMain);
}
});
}
}
ボード.xml
<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:background="#48D9AB">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Play"
tools:context=".Board" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="Exit" />
</RelativeLayout>
ConnectFourView.java コード
package com.example.connectfour.view;
import com.example.connectfour.model.Gaps;
imports[..]
public class ConnectFourView extends View {
public ConnectFourView(Context context) {
super(context);
setMinimumWidth(100);
setMinimumHeight(20);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = Math.max(getSuggestedMinimumWidth(),widthMeasureSpec);
int height = Math.max(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);
}
protected void onDraw(Canvas canvas){
canvas.drawColor(Color.WHITE);
}
}
私はboard.xmlファイルで次のコードを試しましたが、すべてをまとめると信じていますが、これを挿入すると、アプリケーションは30以上のエラーをlogcatに送信します。
<example.connectfour.view
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
注: これは私の最初の Android 投稿であるため、不足しているコードや追加のコードがある場合は事前に申し訳ありません。ありがとうございます
TanjaV の編集。logcat に表示されるエラー (これらはすべて、上記のコードが board.xml ファイルに追加されたときに表示されます。
11-20 19:14:56.446: E/AndroidRuntime(630): FATAL EXCEPTION: main
11-20 19:14:56.446: E/AndroidRuntime(630): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.connectfour/com.example.connectfour.Board}: android.view.InflateException: Binary XML file line #23: Error inflating class com.example.connectfour.view.ConnectFourView
11-20 19:14:56.446: E/AndroidRuntime(630): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.os.Looper.loop(Looper.java:123)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-20 19:14:56.446: E/AndroidRuntime(630): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 19:14:56.446: E/AndroidRuntime(630): at java.lang.reflect.Method.invoke(Method.java:521)
11-20 19:14:56.446: E/AndroidRuntime(630): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-20 19:14:56.446: E/AndroidRuntime(630): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-20 19:14:56.446: E/AndroidRuntime(630): at dalvik.system.NativeStart.main(Native Method)
11-20 19:14:56.446: E/AndroidRuntime(630): Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.example.connectfour.view.ConnectFourView
11-20 19:14:56.446: E/AndroidRuntime(630): at android.view.LayoutInflater.createView(LayoutInflater.java:503)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-20 19:14:56.446: E/AndroidRuntime(630): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.app.Activity.setContentView(Activity.java:1647)
11-20 19:14:56.446: E/AndroidRuntime(630): at com.example.connectfour.Board.onCreate(Board.java:28)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-20 19:14:56.446: E/AndroidRuntime(630): ... 11 more
11-20 19:14:56.446: E/AndroidRuntime(630): Caused by: java.lang.NoSuchMethodException: ConnectFourView(Context,AttributeSet)
11-20 19:14:56.446: E/AndroidRuntime(630): at java.lang.Class.getMatchingConstructor(Class.java:660)
11-20 19:14:56.446: E/AndroidRuntime(630): at java.lang.Class.getConstructor(Class.java:477)
11-20 19:14:56.446: E/AndroidRuntime(630): at android.view.LayoutInflater.createView(LayoutInflater.java:475)
11-20 19:14:56.446: E/AndroidRuntime(630): ... 21 more