【问题标题】:Simple custom control - Force closed简单的自定义控件 - 强制关闭
【发布时间】:2012-11-14 06:58:43
【问题描述】:

我已经使用eclipse创建了一个android项目。系统默认生成的代码如下所示

    package com.rmycustomclass.bengler;

    import android.app.Activity;
    import android.os.Bundle;

    public class RMyCustomClassActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    }

我想尝试一个简单的自定义控件,所以我将代码更改如下。当我在网上搜索以创建一个简单的自定义控件时,会提到“通过子类化视图来创建类”。所以通过修改代码尝试如下。但它没有运行并且总是强制关闭。

package com.rmycustomclass.bengler;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class RMyCustomClassActivity extends View {

    private Paint myPaint;

    public RMyCustomClassActivity(Context cxt, AttributeSet attribute){

        super(cxt,attribute);
        init();

    }

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



    public void init(){

        myPaint = new Paint();
        myPaint.setTextSize(12);
        myPaint.setColor(0xFF668800);
        myPaint.setStyle(Paint.Style.FILL);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawText("TEEEST", 100, 100, myPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        this.setMeasuredDimension(150,200);     
    }



}

下面是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        android:layout_width="fill_parent" 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_height="fill_parent" android:orientation="vertical">

        <TextView 
        android:id="@+id/textView1" 
        android:text="Enter text"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
        </TextView>

        <TableLayout 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:id="@+id/tableLayout1">
            <Button 
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Button"
                 android:layout_weight="1">
            </Button>
            <Button android:layout_width="wrap_content"  android:id="@+id/button1" android:layout_height="40dip" android:text="My button" android:layout_weight="1"></Button>
            <test.control 
              android:id="@+id/control" 
              android:layout_height="match_parent"> </test.control>
        </TableLayout>
    </LinearLayout>

【问题讨论】:

  • 发布您的 LogCat 错误,以便我们了解发生了什么。
  • 您的 View 类与您的 Activity 类同名,您应该为您的 View 使用更具描述性的名称...这很容易导致冲突。

标签: android subclass forceclose


【解决方案1】:

当您在 Eclipse 中创建项目时,它会将您的活动(在您的情况下为 RMyCustomClassActivity)链接为 Android 清单中的启动器...但是由于您已将活动更改为视图 android 运行时找不到要启动的活动...查看您的清单文件,您会发现您的类作为启动器活动

【讨论】:

  • 你能告诉我我应该改变什么
  • 对于您的自定义视图,创建一个不同的类并在您的活动/布局中使用此视图进行测试
【解决方案2】:

添加 android:layout_width="match_parent"

<test.control 
          android:id="@+id/control" 
          android:layout_height="match_parent"
          android:layout_width="match_parent"
> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多