SetText error Android

问题描述 投票:0回答:4
public class AddNetActivity extends Activity {

String Url = "https://dl.dropboxusercontent.com/u/103170339/WebStock.xml";
TextView textInfo;
ActionBar actionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    actionBar = getActionBar();
    textInfo = (TextView) findViewById(R.id.text_view_new_info);
    // textInfo.setText("adasd"); <<<<<<<<<<<<
    actionBar.setDisplayHomeAsUpEnabled(true);
    NetworkStatusXmlStart();
    setContentView(R.layout.activity_add_net);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.add_net, menu);
    return true;
}

// Respond to Action Buttons
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_accept:
            // openAccept();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

public void NetworkStatusXmlStart(){
    ConnectivityManager connMgr = (ConnectivityManager) 
            getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            new WriteToXml().execute(Url);
        } else {
            // show error
        }
}
private class WriteToXml extends AsyncTask<String, Void, String>{
    @Override
    protected String doInBackground(String... urlS){
        try {
            return downloadUrl(urlS[0]);
        } catch (IOException e) {
            return null;
        } catch (XmlPullParserException e) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(String Result){
        // make resutlts
    }
}

private String downloadUrl(String UrlStr) throws XmlPullParserException, IOException{
    InputStream streamInput = null;
    XmlPullParser parser;
    String writed;
    try{
        streamInput = dowStream(UrlStr);
        parser = downloadParser(streamInput);
        writed = WriteToParserAndShow(parser);
    } finally{
        if (streamInput != null) {
            streamInput.close();
        } 
    }
    return writed;
}

private InputStream dowStream(String Surl) throws IOException{
    URL url = new URL (Surl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000 /* milliseconds */);
    conn.setConnectTimeout(15000 /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query
    conn.connect();
    return conn.getInputStream();
}

private XmlPullParser downloadParser(InputStream impStream) throws XmlPullParserException, IOException{
    XmlPullParser parser = Xml.newPullParser();
    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
    parser.setInput(impStream, null);
    parser.nextTag();
    return parser;
}

private String WriteToParserAndShow(XmlPullParser pars) throws XmlPullParserException, IOException{
    String Info = null, endTagName;


    return Info;
}
}

为什么我要使用setText时Activity会崩溃?当我发表评论时,它工作正常。Xml文件如下所示:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AddNetActivity"
android:background="#ccccf9" >

<TextView
    android:id="@+id/text_view_new_info"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="132dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

告诉我是否需要logcat。如何更改此textView中的文本?错误在哪里?请帮助

android text settext
4个回答
1
投票

您要在textview之前声明并设置setContentView中的文本

setContentView(R.layout.activity_add_net);移到onCreate();下方

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
here  > setContentView(R.layout.activity_add_net);

0
投票

使用setContentView()之前,您必须先致电findViewById()

findViewById遍历内容视图以查找由您作为第一个参数传递的id指定的子视图。但是,如果尚未设置内容视图,应该如何查找?

[当您访问未找到的视图之一的函数或变量时,将导致NullPointerException


0
投票

您忘了增加布局。在setContentView(R.layout.mylayoutfile)调用后插入 super.onCreate()


0
投票

您能否验证setText()中提供的属性ID是否正确?您可能在调用setText()时遇到了NullPointerException,因为activity_main.xml中提供的属性的ID可能与MainActivity.java中提供的attribute_id不匹配。

© www.soinside.com 2019 - 2024. All rights reserved.