ljwbupt 2015-05-04 17:32 采纳率: 90%
浏览 1691
已采纳

Android自定义view失败

安卓编程时想在xml布局中自定义一个view,但当把自定义的view写在xml里面时,就会报错说The following classes could not be instantiated:- com.example.wifidetection.MyPaint.MyView 。
其中MyView就是我自定义的view,前面是它的路径,里面wifideection是包名,MyPaint是MyView所在的类,代码如下:

<?xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.example.wifidetection"
android:id="@+id/mScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >

android:orientation = "vertical"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/start"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="开始绘图" />

<com.example.wifidetection.MyPaint.MyView  
    android:layout_width="200dp"  
    android:layout_height="100dp"  
   />  



MyPaint中是这样的

public class MyPaint extends ActionBarActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ·····
    setContentView(R.layout.paint_layout);//显示MyView布局
}

class MyView extends View
{
    public MyView(Context context, AttributeSet attrs)  
    {  
        this(context, attrs, 0);  
    }  

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

一直搞不懂,网上也没找到什么好的解释,求论坛里大神解答

  • 写回答

5条回答 默认 最新

  • danielinbiti 2015-05-05 01:07
    关注

    MyView 是内部类,把myview独立一个.java文件,定义成public类

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败