运行Android应用程序时出现“java.lang.InstantiationException”

时间:2012-04-08 16:57:44

标签: 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"
/>

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我从谷歌播放开发者控制台收到有关此错误的报告。 该应用程序在我的设备和所有模拟器上运行良好。

到目前为止,我已经收到了大约10份报告,总安装数量达到3000,所以我决定忽略它。