Reputation:
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;
}
}
Why when I want use setText, Activity crushes? When I put it into comment it works fine. Xml file looks like that:
<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" />
Tell me if logcat is needed. How change text in this textView? Where is error? Please help
Upvotes: 1
Views: 4751
Reputation: 39
Could you please verify whether your attribute id provided in setText() is correct?. You might have probably encountered a NullPointerException while setText() is being called since the id of the attribute provided in activity_main.xml may not be matching with the attribute_id provided in MainActivity.java
Upvotes: 0
Reputation: 1
You forgot to inflate your layout. Insert setContentView(R.layout.mylayoutfile)
after the super.onCreate()
call.
Upvotes: 0
Reputation: 12919
You have to call setContentView()
before using findViewById()
.
findViewById
iterates over the content view to find the sub view specified by the id you pass as the first argument. But if the content view has not been set yet, how should it find anything?
This leads to a NullPointerException
when you access a function or variable of one of the Views that weren't found.
Upvotes: 0
Reputation: 12042
you are declaring and setting the text in textview
before the setContentView
move the setContentView(R.layout.activity_add_net);
below the onCreate();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
here > setContentView(R.layout.activity_add_net);
Upvotes: 1