Activity onCreate()期间Android代码中的NullPointerException

时间:2015-02-13 20:35:07

标签: android android-activity nullpointerexception

以下是我的Android代码:

package com.example.to_dolsit;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
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 MainActivity extends Activity {
    int KeyCode;
    ListView myListView = (ListView) findViewById(R.id.listView1);
    final EditText myEditText = (EditText) findViewById(R.id.editText1);
    String tag = "Events";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        Log.d( tag,"ohkkkkkkkkkkkkkkkkkkkkkkkkkkkkk.......................");
        // create the list of to do items
        final ArrayList<String> todoItems = new ArrayList<String>();
        // craeate the array adapter to bind the aray to the list
        final ArrayAdapter<String> aa;
        aa = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, todoItems);
        myListView.setAdapter(aa);

        myEditText.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                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;
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}

布局代码为: -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:drawableLeft="@drawable/ic_launcher"

        android:text="@string/EditText" >

    </EditText>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="54dp" >
    </ListView>



</RelativeLayout>

它给了我以下错误:


02-14 01:31:31.601: E/AndroidRuntime(10689): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.to_dolsit/com.example.to_dolsit.MainActivity}: java.lang.NullPointerException
02-14 01:31:31.601: E/AndroidRuntime(10689):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
02-14 01:31:31.601: E/AndroidRuntime(10689):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)

1 个答案:

答案 0 :(得分:0)

您的ListView最有可能导致nullpointer。在致电findViewById()之前,请勿致电setContentView()。将您的代码更改为以下内容。

public class MainActivity extends Activity {

    int KeyCode;
    String tag = "Events";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        // create the list of to do items
        final ArrayList<String> todoItems = new ArrayList<String>();
        // craeate the array adapter to bind the aray to the list
        final ArrayAdapter<String> aa;
        aa = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, todoItems);
        myListView.setAdapter(aa);

        myEditText.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                 // TODO Auto-generated method stub
                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;

                }

            });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;

    }

}