android - 运行 Android 应用程序时为 "java.lang.InstantiationException"

标签 android android-listview

下面是我的示例应用 TodoListItemView 的日志。我很抱歉写了这么多代码,但我不知道为什么它不起作用。

04-08 16:46:23.023: D/AndroidRuntime(3167): Shutting down VM
04-08 16:46:23.023: W/dalvikvm(3167): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-08 16:46:23.134: E/AndroidRuntime(3167): FATAL EXCEPTION: main
04-08 16:46:23.134: E/AndroidRuntime(3167): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.rasi.todolist/com.rasi.todolist.TodoListItemView}: java.lang.InstantiationException: com.rasi.todolist.TodoListItemView
04-08 16:46:23.134: E/AndroidRuntime(3167):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
04-08 16:46:23.134: E/AndroidRuntime(3167):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-08 16:46:23.134: E/AndroidRuntime(3167):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-08 16:46:23.134: E/AndroidRuntime(3167):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-08 16:46:23.134: E/AndroidRuntime(3167):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 16:46:23.134: E/AndroidRuntime(3167):     at android.os.Looper.loop(Looper.java:123)
04-08 16:46:23.134: E/AndroidRuntime(3167):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-08 16:46:23.134: E/AndroidRuntime(3167):     at java.lang.reflect.Method.invokeNative(Native Method)
04-08 16:46:23.134: E/AndroidRuntime(3167):     at java.lang.reflect.Method.invoke(Method.java:507)
04-08 16:46:23.134: E/AndroidRuntime(3167):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-08 16:46:23.134: E/AndroidRuntime(3167):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-08 16:46:23.134: E/AndroidRuntime(3167):     at dalvik.system.NativeStart.main(Native Method)
04-08 16:46:23.134: E/AndroidRuntime(3167): Caused by: java.lang.InstantiationException: com.rasi.todolist.TodoListItemView
04-08 16:46:23.134: E/AndroidRuntime(3167):     at java.lang.Class.newInstanceImpl(Native Method)
04-08 16:46:23.134: E/AndroidRuntime(3167):     at java.lang.Class.newInstance(Class.java:1409)
04-08 16:46:23.134: E/AndroidRuntime(3167):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-08 16:46:23.134: E/AndroidRuntime(3167):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
04-08 16:46:23.134: E/AndroidRuntime(3167):     ... 11 more

这是TodoList的代码:

public class ToDoList extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{   
    Log.d("debug", "En TodoList");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView myListView = (ListView)findViewById(R.id.myListView);
    final EditText myEditText = (EditText)findViewById(R.id.myEditText);

    final ArrayList<String> todoItems = new ArrayList<String>();
    int resID = R.layout.todolist_item;
    final ArrayAdapter<String> aa;

    aa = new ArrayAdapter<String>(this,
                                  resID, 
                                  todoItems);
    myListView.setAdapter(aa);

    myEditText.setOnKeyListener(new OnKeyListener()
    { 
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
            {
                if(keyCode == KeyEvent.KEYCODE_ENTER)   
                {
                    todoItems.add(0, myEditText.getText().toString());
                    aa.notifyDataSetChanged();
                    return true;
                }
            }
            return false;   
        }
    }
    );
}
}

这是 TodoListItemView 的代码:

public class TodoListItemView extends TextView
{   
    public TodoListItemView(Context context, AttributeSet ats, int ds) 
    {
        super(context, ats, ds);
        Log.d("debug", "TodoListItemView 3");
        init();
    }

    public TodoListItemView(Context context)
    {
        super(context);
        Log.d("debug", "TodoListItemView 2");
        init();
    }

    public TodoListItemView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        Log.d("debug", "TodoListItemView 1");
        init();
    }

    private Paint marginPaint;
    private Paint linePaint;
    private int paperColor;
    private float margin;

    private void init()
    {   
        Log.d("debug", "init inicio");
        Resources myResources = getResources();
        marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        marginPaint.setColor(myResources.getColor(R.color.notepad_margin));
        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setColor(myResources.getColor(R.color.notepad_lines));

        paperColor = myResources.getColor(R.color.notepad_paper);
        margin = myResources.getDimension(R.dimen.notepad_margin);
        Log.d("debug", "init fin");
    }

    public void onDraw(Canvas canvas)
    {
        canvas.drawColor(paperColor);

        canvas.drawLine(0, 0, getMeasuredHeight(), 0, linePaint);
        canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), 
                        linePaint);

        canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);

        canvas.save();
        canvas.translate(margin, 0);

        super.onDraw(canvas);
        canvas.restore();
    }
}

这是 main.xml:

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

<EditText
    android:id = "@+id/myEditText"
    android:inputType = "text"
    android:layout_width = "fill_parent"
    android:layout_height = "wrap_content"
    android:text = "@string/stringEditText"
 />

<ListView 
    android:id = "@+id/myListView"
    android:layout_width = "fill_parent"
    android:layout_height = "wrap_content"
/>

todolist_item.xml:

<?xml version="1.0" encoding="UTF-8"?>
<com.rasi.todolist.TodoListItemView
    xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:scrollbars="vertical"
android:textColor="@color/notepad_text"
android:fadingEdge="vertical"
/>

最佳答案

我已经测试了你的代码,它没有任何异常。尝试清理项目,然后再次运行。

关于android - 运行 Android 应用程序时为 "java.lang.InstantiationException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10064583/

相关文章:

java - TextView 中的编程文本换行

android - ListView 标题和第一项之间的空白

java - 具有水平滚动的 ListView

android - 如何正确实现ListView的适配器

安卓工作室 Windows 10 (64)

android - 如何在 android 的自定义对话框中显示 webview?

android - Android 上的相机方向

android - 无法正确地将我的 LinearLayout 更改为 ListView

java - 无法解析我用来初始化适配器的方法 'getActivity()'

java - 在 fragment 中填充 JSON ListView