Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid correct MVC coding to display a shape
    text
    copied!<p>I have recently started working with android in eclipse. I have to keep within the Model-View-Controller (MVC) structure.</p> <p>My goal is simply to display a rectangle box on screen. What I have got at the minute is a application that has four screens (main menu and the three other screens linked to it). The box implementation is for one of these three screens (board)</p> <p>board.java code</p> <pre><code>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); } }); } } </code></pre> <p>board.xml</p> <pre><code>&lt;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"&gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="Play" tools:context=".Board" /&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>ConnectFourView.java code</p> <pre><code>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); } } </code></pre> <p>I have tried the following code in board.xml file which I believe would bring it all together however when a insert this it my application sends 30+ errors to logcat.</p> <pre><code> &lt;example.connectfour.view android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; </code></pre> <p>Note: As this is my first android post I am sorry in advance for any missing/additional code. Thanks you</p> <p>EDIT for TanjaV. errors i get in logcat (these all appear when the above code is added to the board.xml file.</p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload