Android上的空对象首选项Edittext
我是新的安卓编程。我正处于瓶颈。我正在尝试聊天应用程序。单击项目时,会出现以下错误:Android上的空对象首选项Edittext,android,textview,Android,Textview,我是新的安卓编程。我正处于瓶颈。我正在尝试聊天应用程序。单击项目时,会出现以下错误: 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行
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)findviewbyd(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();
}
私有数组列表;
私人聊天室适配器adp;
私有编辑文本文本;
私人串伙伴;
私人约会;
私有布尔运算;
私有静态处理程序;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.chat);
convList=newarraylist();
ListView列表=(ListView)findViewById(R.id.list);
adp=新的聊天适配器();
列表.设置适配器(adp);
list.setTranscriptMode(AbsListView.TRANSCRIPT\u MODE\u ALWAYS\u 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(常量额外数据);
getActionBar().setTitle(好友);
handler=新的handler();
}
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>
感谢您发布布局xml。该错误是由您应该调用的事实引起的:
txt=(EditText)findViewById(R.id.txt);
而不是
txt=(EditText)findViewById(R.id.text);
因为您的版面中没有带有该id的EditText
。您是否定义/分配了txt
?是的,在这里是私有的EditText txt;你能把你遇到这个问题的那部分代码贴出来吗?当然。我编辑了我的帖子你的chat.xml
版面中有R.id.text
吗?对!非常感谢你,马可:)