0
package org.profdev.todo_list;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ToDoList extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    //Inflates your view
    setContentView(R.layout.activity_to_do_list);

    //Get references to UI Widgets
    ListView myListView = (ListView) findViewById(R.id.myListView);
    final EditText myEditText = (EditText) findViewById(R.id.myEditText);

    //Create ArrayList<> of To Do items
    final ArrayList<String> todoItems = new ArrayList<String>();
    //Create ArrayAdapter to bind the Array to ListView
    final ArrayAdapter<String> aa;
    aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
    //Bind ArrayAdapter to ListView
    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_DPAD_CENTER)
                {
                    todoItems.add(0, myEditText.getText().toString());
                    aa.notifyDataSetChanged();
                    myEditText.setText("");
                    return true;
                }

            return false;   
            }           
        });
    }
}

activity_to_do_list.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText 
        android:id="@+id/myEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/new_to_do" />

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


</LinearLayout>

我是一名新的 android 开发人员,我正在编写“Professional Development 4 Android”一书中的教程,并且具有与书中相同的代码。我的代码由于某种原因无法正常工作。我没有编译器错误,但在模拟器中,启动应用程序后出现“不幸的是,Todo_List 已停止”对话框。

11-13 23:03:06.124: D/AndroidRuntime(844): Shutting down VM
11-13 23:03:06.124: W/dalvikvm(844): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-13 23:03:06.253: E/AndroidRuntime(844): FATAL EXCEPTION: main
11-13 23:03:06.253: E/AndroidRuntime(844): android.app.SuperNotCalledException: Activity {org.profdev.todo_list/org.profdev.todo_list.ToDoList} did not call through to super.onCreate()
11-13 23:03:06.253: E/AndroidRuntime(844):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2177)
11-13 23:03:06.253: E/AndroidRuntime(844):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-13 23:03:06.253: E/AndroidRuntime(844):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-13 23:03:06.253: E/AndroidRuntime(844):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-13 23:03:06.253: E/AndroidRuntime(844):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 23:03:06.253: E/AndroidRuntime(844):  at android.os.Looper.loop(Looper.java:137)
11-13 23:03:06.253: E/AndroidRuntime(844):  at android.app.ActivityThread.main(ActivityThread.java:5103)
11-13 23:03:06.253: E/AndroidRuntime(844):  at java.lang.reflect.Method.invokeNative(Native Method)
11-13 23:03:06.253: E/AndroidRuntime(844):  at java.lang.reflect.Method.invoke(Method.java:525)
11-13 23:03:06.253: E/AndroidRuntime(844):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-13 23:03:06.253: E/AndroidRuntime(844):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 23:03:06.253: E/AndroidRuntime(844):  at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

3

添加

super.onCreate(savedInstanceState);

在你的onCreate()

喜欢:

@Override
protected void onCreate(Bundle savedInstanceState) {
    //Inflates your view
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_list);
于 2013-11-14T04:00:58.650 回答
2

在 onCreate 中做任何事情之前,您应该调用超类的 onCreate。onCreate() 方法的第一行应该是,

super.onCreate(savedInstanceState);

它将暂时解决问题。您可以阅读这个问题及其答案,了解为什么必须这样做。

于 2013-11-14T04:00:34.723 回答
0

您需要调用super.onCreate(savedInstanceState)onCreate 作为异常状态。

于 2013-11-14T04:23:15.413 回答