我最近尝试了一些编码来制作试用搜索功能,然后读取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 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;
}
}
}
这就是我的代码和我所知道的所有错误
Unable to start activity ComponentInfo{com.example.owner.jabexam/com.example.owner.jabexam.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference" "java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference"
我为我的生活无法弄明白。 如果有人能帮助我,我会非常感激。
答案 0 :(得分:1)
1。)在执行mEditText
MyAsyncTask
2。)创建一个XML
文件,其中包含edittext
和ListView
,其中ListView
必须id
为@android:id/list
,如下所示。
3.。在setContentView(R.layout.your_layout)
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>
答案 1 :(得分:0)
您需要在setContentView(R.layout.your_layout)
之前致电findViewById()
,之前请调用此方法并传递您的布局的editText
标识为SearchTB
且列表视图的xml文件标识为@android:id/list
。
其他观察: 切换这两条线的定位 来自
new MyAsyncTask().execute();
mEditText = (EditText) findViewById(R.id.SearchTB);
到
mEditText = (EditText) findViewById(R.id.SearchTB);
new MyAsyncTask().execute();
解释您需要在访问对象之前初始化对象,而在myAsyncTask中,您需要访问exittext,因此需要先对其进行初始化。