
public Mens(Context context, AttributeSet attrs)
{
super(context);
}
ObjMens = new Mens(this);
ObjMens = new Mens(this, null);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layMain = (LinearLayout)findViewById(R.id.layMain);
Mens ObjMens = (Mens)findViewById(R.id.myView);
ObjMens = new Mens(this, null);
layMain.addView(ObjMens);
[b]layMain.addView(new SampleView(this));[/b]
}
setContentView( new SampleView(this))


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}





.



12-02 11:27:47.172: ERROR/AndroidRuntime(2223): Uncaught handler: thread main exiting due to uncaught exception
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.apis.graphics/com.example.android.apis.graphics.SurfaceViewOverlay}: android.view.InflateException: Binary XML file line #65: Error inflating class com.example.android.apis.graphics.MyCustomButton
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2156)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1580)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.os.Handler.dispatchMessage(Handler.java:88)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.os.Looper.loop(Looper.java:123)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.ActivityThread.main(ActivityThread.java:3742)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at java.lang.reflect.Method.invoke(Method.java:515)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at dalvik.system.NativeStart.main(Native Method)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): Caused by: android.view.InflateException: Binary XML file line #65: Error inflating class com.example.android.apis.graphics.MyCustomButton
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.createView(LayoutInflater.java:502)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:564)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.rInflate(LayoutInflater.java:620)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.rInflate(LayoutInflater.java:620)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:227)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.Activity.setContentView(Activity.java:1569)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at com.example.android.apis.graphics.SurfaceViewOverlay.onCreate(SurfaceViewOverlay.java:37)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1122)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2103)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): ... 11 more
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): Caused by: java.lang.NoSuchMethodException: MyCustomButton
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at java.lang.Class.getMatchingConstructor(Class.java:537)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at java.lang.Class.getConstructor(Class.java:401)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): at android.view.LayoutInflater.createView(LayoutInflater.java:474)
12-02 11:27:47.213: ERROR/AndroidRuntime(2223): ... 23 more
public class SurfaceViewOverlay extends Activity {
View mVictimContainer;
View mVictim1;
View mVictim2;
View v1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.surface_view_overlay);
GLSurfaceView glSurfaceView =
(GLSurfaceView) findViewById(R.id.glsurfaceview);
glSurfaceView.setRenderer(new CubeRenderer(false));
// Find the views whose visibility will change
mVictimContainer = findViewById(R.id.hidecontainer);
mVictim1 = findViewById(R.id.hideme1);
MyCustomButton mcb = (MyCustomButton)findViewById(R.id.myButton);
mcb = new MyCustomButton(this);
mVictim1.setOnClickListener(new HideMeListener(mVictim1));
mVictim2 = findViewById(R.id.hideme2);
mVictim2.setOnClickListener(new HideMeListener(mVictim2));
// Find our buttons
Button visibleButton = (Button) findViewById(R.id.vis);
Button invisibleButton = (Button) findViewById(R.id.invis);
Button goneButton = (Button) findViewById(R.id.gone);
// Wire each button to a click listener
visibleButton.setOnClickListener(mVisibleListener);
invisibleButton.setOnClickListener(mInvisibleListener);
goneButton.setOnClickListener(mGoneListener);
}
@Override
protected void onResume() {
// Ideally a game should implement onResume() and onPause()
// to take appropriate action when the activity looses focus
super.onResume();
}
@Override
protected void onPause() {
// Ideally a game should implement onResume() and onPause()
// to take appropriate action when the activity looses focus
super.onPause();
}
class HideMeListener implements OnClickListener {
final View mTarget;
HideMeListener(View target) {
mTarget = target;
}
public void onClick(View v) {
mTarget.setVisibility(View.INVISIBLE);
}
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim1.setVisibility(View.VISIBLE);
mVictim2.setVisibility(View.VISIBLE);
v1.setVisibility(View.VISIBLE);
mVictimContainer.setVisibility(View.VISIBLE);
}
};
OnClickListener mInvisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim1.setVisibility(View.INVISIBLE);
mVictim2.setVisibility(View.INVISIBLE);
mVictimContainer.setVisibility(View.INVISIBLE);
}
};
OnClickListener mGoneListener = new OnClickListener() {
public void onClick(View v) {
mVictim1.setVisibility(View.GONE);
mVictim2.setVisibility(View.GONE);
mVictimContainer.setVisibility(View.GONE);
}
};
}
public class MyCustomButton extends Button {
static final int StateDefault = 0;
static final int StateFocused = 1;
static final int StatePressed = 2;
private int mState = StateDefault;
private Bitmap mBitmapDefault;
private Bitmap mBitmapFocused;
private Bitmap mBitmapPressed;
private String mCaption;
public MyCustomButton(Context context) {
super(context);
setClickable(true);
mBitmapDefault = Bitmap.createBitmap(900, 500, Config.ARGB_4444);
mBitmapFocused = Bitmap.createBitmap(900, 500, Config.ARGB_4444);
mBitmapPressed = Bitmap.createBitmap(900, 500, Config.ARGB_4444);
// create the Canvas
Canvas canvas = new Canvas();
canvas.setBitmap(mBitmapDefault);
// create the Drawing Tool (Brush)
Paint paint = new Paint();
paint.setAntiAlias(true); // for a nicer paint
paint.setColor(Color.GRAY);
paint.setStrokeWidth(3);
paint.setStyle(Style.FILL);
Path path = new Path();
path.addRoundRect(new RectF(20, 20, 300, 120),
10, 10, Direction.CCW);
canvas.drawPath(path, paint);
Paint paintText = new Paint();
paintText.setAntiAlias(true);
paintText.setTextSize(20);
paintText.setColor(0xffffffff); // white
// draw Text
canvas.drawText("OK", 30, 55, paintText);
// for the Pressed Image
canvas.setBitmap(mBitmapPressed);
paint.setColor(Color.BLACK);
paintText.setColor(Color.WHITE);
canvas.drawPath(path, paint);
canvas.drawText("OK", 30, 55, paintText);
// for the Pressed Image
canvas.setBitmap(mBitmapFocused);
// Blue Color
paint.setColor(Color.BLUE);
paintText.setColor(Color.WHITE); // white Line
canvas.drawPath(path, paint);
canvas.drawText("OK", 30, 55, paintText);
// define OnClickListener for the Button
setOnClickListener(onClickListener);
}
@Override
protected void onDraw(Canvas canvas) {
switch (mState) {
case StateDefault:
canvas.drawBitmap(mBitmapDefault, 0, 0, null);
break;
case StateFocused:
canvas.drawBitmap(mBitmapFocused, 0, 0, null);
break;
case StatePressed:
canvas.drawBitmap(mBitmapPressed, 0, 0, null);
break;
}
}
@Override
protected void drawableStateChanged() {
if (isPressed()) {
mState = StatePressed;
} else if (hasFocus()) {
mState = StateFocused;
} else {
mState = StateDefault;
}
// force the redraw of the Image
// onDraw will be called!
invalidate();
}
private OnClickListener onClickListener =
new OnClickListener() {
public void onClick(View arg0) {
Log.i("GameProfile","text:"+mCaption);
}
};
}
<?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">
<!-- Here is where we put the SurfaceView, in a frame so that we can
stack other views on top of it. -->
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1">
<com.example.android.apis.graphics.GLSurfaceView
android:id="@+id/glsurfaceview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<LinearLayout android:id="@+id/hidecontainer"
android:orientation="vertical"
android:visibility="visible"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/hideme1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible"
android:text="@string/hide_me"/>
<Button android:id="@+id/hideme2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible"
android:text="@string/hide_me"/>
<Button android:id="@+id/hideme3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible"
android:text="@string/hide_me"/>
<com.example.android.apis.graphics.MyCustomButton
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/myButton" />
</LinearLayout>
</FrameLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Button android:id="@+id/vis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/visibility_1_vis"/>
<Button android:id="@+id/invis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/visibility_1_invis"/>
<Button android:id="@+id/gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/visibility_1_gone"/>
</LinearLayout>
</LinearLayout>

plusminus wrote:MrSnowflake wrote:What he sais, or setContentView(layMain);plusminus wrote:The activity needs to know what it display. So in the very beginning (after super.onCreate();) call:Using java Syntax Highlighting
setContentView(R.layout.main);Parsed in 0.031 seconds, using GeSHi 1.0.8.4
Usually you would also add the view in xml before.
Then [font=Courier New]findViewById[/font] would fail :P

Return to View, Layout & Resource Problems
Users browsing this forum: No registered users and 8 guests