Null Object Preferences Edittext on Android

888 Views Asked by At

I am new Android programming. I am at bottleneck. I am trying to chatapp. when I click the item these errors occur :

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setInputType(int)' on a null object reference 
at com.senturk.fatih.chat03.Chat.onCreate(Chat.java:55) 

and here is line 55

txt.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);

and I guess I should add this line txt=(EditText)findViewById(R.id.text);

Thanks for all favor ,

private ArrayList<Conversation> convList;
private ChatAdapter adp;
private EditText txt;
private String buddy;
private Date lstMsgDate;
private boolean isRunning;
private static Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chat);
    convList=new ArrayList<Conversation>();

    ListView list=(ListView)findViewById(R.id.list);
    adp=new ChatAdapter();

    list.setAdapter(adp);
    list.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    list.setStackFromBottom(true);

    txt=(EditText)findViewById(R.id.text);
    txt.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);

    setTouchNClick(R.id.btnSend);
    buddy=getIntent().getStringExtra(Const.EXTRA_DATA);
    getActionBar().setTitle(buddy);

    handler=new Handler();
}

The xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:background="@color/white">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:divider="@color/white"
        android:dividerHeight="@dimen/pad_5dp"
        android:fastScrollEnabled="true"
        android:paddingBottom="@dimen/pad_10dp"
        android:paddingTop="@dimen/pad_10dp"
        tools:listitem="@layout/chat_item_rcv" >
    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/gray_light"
        android:gravity="center_vertical"
        android:padding="@dimen/pad_5dp"
        tools:context=".MainActivity" >


        <EditText
            android:id="@+id/txt"
            style="@style/edittext_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/type_msg" >


        </EditText>

        <Button
            android:id="@+id/btnSend"
            style="@style/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/send_telegram" />

    </LinearLayout>

</LinearLayout>
1

There are 1 best solutions below

1
On BEST ANSWER

Thanks for posting your layout xml. The error is caused by the fact that you should be calling:

txt=(EditText)findViewById(R.id.txt);

and not

txt=(EditText)findViewById(R.id.text);

since you don't have an EditText with that id in your layout.