2015-06-13 14 views
-2

我是新的Android編程。我處於瓶頸。我正在嘗試chatapp。當我點擊該項目發生這些錯誤:Android上的Null Object Preferences Edittext

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) 

,這裏是線55

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

,我想我應該加入這一行txt=(EditText)findViewById(R.id.text);

感謝所有支持,

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> 
+0

您是否定義/分配了'txt'? –

+0

是的,在這裏是私人的EditText txt; –

+0

你可以發佈你遇到這個問題的代碼部分嗎? –

回答

3

感謝您發佈您的佈局xml。該錯誤的事實,你應該調用引起:

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

,而不是

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

,因爲你沒有在你的佈局,ID爲EditText做。

+0

對!非常感謝你Marco :) –