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条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算