Java ListActivity:使用AsyncTask对空对象引用执行EditText.getText()
我最近尝试了一点编码来制作一个试用搜索功能,然后读取RSS提要并列出它Java ListActivity:使用AsyncTask对空对象引用执行EditText.getText(),java,android,Java,Android,我最近尝试了一点编码来制作一个试用搜索功能,然后读取RSS提要并列出它 package com.example.owner.jabexam; import android.app.ListActivity; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import an
package com.example.owner.jabexam;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ListActivity
{
private EditText mEditText;
List headlines;
List links;
List discription;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
new MyAsyncTask().execute();
mEditText = (EditText) findViewById(R.id.SearchTB);
}
class MyAsyncTask extends AsyncTask<Object,Void,ArrayAdapter>
{
private String urlLink;
@Override
protected void onPreExecute(){
urlLink = mEditText.getText().toString();
}
@Override
protected ArrayAdapter doInBackground(Object[] params)
{
headlines = new ArrayList();
links = new ArrayList();
discription = new ArrayList();
try
{
URL url = new URL(urlLink);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if (eventType == XmlPullParser.START_TAG)
{
if (xpp.getName().equalsIgnoreCase("item"))
{
insideItem = true;
}
else if (xpp.getName().equalsIgnoreCase("title"))
{
if (insideItem)
headlines.add(xpp.nextText()); //extract the headline
}
else if (xpp.getName().equalsIgnoreCase("link"))
{
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
else if (xpp.getName().equalsIgnoreCase("discription"))
{
if (insideItem)
discription.add(xpp.nextText()); //extract the discription of article
}
}
else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item"))
{
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
protected void onPostExecute(ArrayAdapter adapter)
{
adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
Uri uri = Uri.parse((links.get(position)).toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
public InputStream getInputStream(URL url)
{
try
{
return url.openConnection().getInputStream();
}
catch (IOException e)
{
return null;
}
}
}
我这辈子都搞不懂。
如果有人能帮忙,我将不胜感激 1.)在执行MyAsyncTask
2.)创建一个XML
文件,其中包含edittext
和ListView
,其中ListView
必须具有id
作为@android:id/list
,例如添加到下面
3.)在super.onCreate..
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
// set layout ^^^^
mEditText = (EditText) findViewById(R.id.SearchTB);
new MyAsyncTask().execute();
}
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/SearchTB"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="anything"/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/something"/>
</RelativeLayout>
您需要调用
setContentView(R.layout.your_layout)
在执行findViewById()
之前,调用此方法,并传递布局的xml文件,该文件具有idSearchTB
的editText和id@android:id/list
的listview
其他观察结果:
这两条线路的道岔定位
从
到
解释在访问对象之前,您需要初始化对象,而在myAsyncTask中,您正在访问exittext,因此它需要在访问之前进行初始化。谢谢,这主要帮助了我仍然获得了相同的两个错误。“java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.owner.jabexam/com.example.owner.jabexam.MainActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.text.Editable android.widget.EditText.getText()'”@JBell我已更新我的答案谢谢,这主要帮助我仍然得到同样的两个错误<代码>java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.owner.jabexam/com.example.owner.jabexam.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'android.text.Editable android.widget.EditText.getText()'在空对象引用上@PavneetSingh@JBell您需要使用
setContentView
设置布局,并确保其中包含编辑文本和列表视图,并且列表视图的id
应该是@android:id/list
@JBell在res/layout
下添加上述xml,并使用setContentView
谢谢你。它在启动时不再崩溃。现在只要让我的实际代码正常工作,我就可以放心了。再次感谢。@JBell我很高兴能帮上忙,祝你好运,编码快乐
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/SearchTB"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="anything"/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/something"/>
</RelativeLayout>
new MyAsyncTask().execute();
mEditText = (EditText) findViewById(R.id.SearchTB);
mEditText = (EditText) findViewById(R.id.SearchTB);
new MyAsyncTask().execute();