SurfaceView class and bloat bugs

I tried to create an application with a custom view, and I continued to get the "error blowing class". I must be missing some basics when it comes to custom views, but I'm not sure what. Here is a very simple program with a user view, what else is needed to make it work?

(Notes: For this question, I placed the SurfaceView class inside the Activity class. This situation is not in a larger application. I do not show the AndroidManifest.xml file here, but this is exactly what the wizard created in eclipse.)

Here is java:

package com.mypackage; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.AttributeSet; import android.util.Log; import android.view.SurfaceView; public class SimpleCustomViewActivity extends Activity { class TheView extends SurfaceView{ private static final String TAG = "TheView"; public TheView(Context context, AttributeSet attrs) { super(context, attrs); Log.i(TAG,"TheView(" + context + "," + attrs + ")"); } } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_layout); TheView v = (TheView) findViewById(R.id.myview); } } 

Here is the res / layout / simple_layout.xml file:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.mypackage.SimpleCustomView.TheView android:id="@+id/myview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> 
+4
source share
4 answers

in xml it should be:

  <com.mypackage.SimpleCustomView.TheView android:id="@+id/myview" android:layout_width="fill_parent" android:layout_height="fill_parent"> </com.mypackage.SimpleCustomView.TheView> 
+2
source

When you call your own surfaceView class from an xml file, you need to add the following methods for creating publiclookView:

 public GameView(Context context) { super(context); init(context); } public GameView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public GameView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } 

If u uses the setContentView (gv) function, you only need the first one.

+5
source

I believe something like this might work, although I have not tested it:

 <View class="com.mypackage.SimpleCustomView$TheView" id="@+id/myview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> 
0
source
 : declare two methods and it should be public!! 

public TheView (context context)

public TheView (context context, AttributeSet attrs)

0
source

Source: https://habr.com/ru/post/1369110/


All Articles