Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ListActivity:使用AsyncTask对空对象引用执行EditText.getText()_Java_Android - Fatal编程技术网

Java ListActivity:使用AsyncTask对空对象引用执行EditText.getText()

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

我最近尝试了一点编码来制作一个试用搜索功能,然后读取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;
        }
    }
}
我这辈子都搞不懂。 如果有人能帮忙,我将不胜感激

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文件,该文件具有id
SearchTB
的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.ja‌​bexam.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();