不能在android的Listactivity中使用edittext

时间:2016-07-07 17:44:56

标签: android nullpointerexception listactivity

我写了一个程序但是当我运行它时不起作用。 我在网上搜索,发现我必须使用listview或edittext但我需要他们两个我能做什么?

这些是我的代码:

message.xml:

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.amir133.getdatafromserver.MainActivity">

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="300dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true" />

<EditText
    android:id="@+id/messages_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="71dp"
    android:ems="10"
    android:inputType="textMultiLine" />

<ImageButton
    android:id="@+id/messages_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/messages_message"
    android:layout_marginBottom="28dp"
    android:onClick="Itemclick"
    android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/messages_send"
    android:layout_toLeftOf="@+id/messages_send"
    android:src="@drawable/abc_ic_clear_normal" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/imageView1"
    android:layout_toLeftOf="@+id/imageView1"
    android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

myactivty.java:

public class Messages extends ListActivity{

ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;

ImageButton send=(ImageButton)findViewById(R.id.messages_send);
EditText message=(EditText)findViewById(R.id.messages_message);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.messages);
    adapter=new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            listItems);
        setListAdapter(adapter);

}
public void Itemclick(View v)
{
    listItems.add(message.getText().toString());
    adapter.notifyDataSetChanged();
}

}

它有这个错误:

07-08 11:48:16.710: E/AndroidRuntime(26754): FATAL EXCEPTION: main
07-08 11:48:16.710: E/AndroidRuntime(26754): Process: com.example.connection, PID: 26754
07-08 11:48:16.710: E/AndroidRuntime(26754): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.connection/com.example.connection.Messages}: java.lang.NullPointerException
07-08 11:48:16.710: E/AndroidRuntime(26754):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2264)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at android.app.ActivityThread.access$800(ActivityThread.java:151)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at android.os.Handler.dispatchMessage(Handler.java:110)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at android.os.Looper.loop(Looper.java:193)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at android.app.ActivityThread.main(ActivityThread.java:5292)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at java.lang.reflect.Method.invokeNative(Native Method)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at java.lang.reflect.Method.invoke(Method.java:515)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at dalvik.system.NativeStart.main(Native Method)
07-08 11:48:16.710: E/AndroidRuntime(26754): Caused by: java.lang.NullPointerException
07-08 11:48:16.710: E/AndroidRuntime(26754):    at android.app.Activity.findViewById(Activity.java:1899)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at com.example.connection.Messages.<init>(Messages.java:23)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at java.lang.Class.newInstanceImpl(Native Method)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at java.lang.Class.newInstance(Class.java:1215)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-08 11:48:16.710: E/AndroidRuntime(26754):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
07-08 11:48:16.710

1 个答案:

答案 0 :(得分:0)

您无法在findViewById之外onCreate()进行。

这样做:

public class Messages extends ListActivity{

ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;

ImageButton send;
EditText message;

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

    //put the code here

    send=(ImageButton)findViewById(R.id.messages_send);
    message=(EditText)findViewById(R.id.messages_message);

    adapter=new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            listItems);
    setListAdapter(adapter);

}
public void Itemclick(View v)
{
    listItems.add(message.getText().toString());
    adapter.notifyDataSetChanged();
}

}