xml parser and thread in Android
I have two class : MainActivity and Quake.
package com.example.ex24;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.w3c.dom.Element;
import android.app.ListFragment;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
public class MainActivity extends ListFragment {
ArrayAdapter<Quake> aa ;
ArrayList<Quake> earthquake = new ArrayList<Quake>();
private Handler handler = new Handler();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
int layoutid = android.R.layout.simple_expandable_list_item_1 ;
aa = new ArrayAdapter<Quake>(getActivity(), layoutid, earthquake);
setListAdapter(aa);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
refreshQ();
}
});t.start();
}
public void refreshQ(){
URL url ;
try{
String quakefeed = getString(R.string.quake_feed);
url = new URL(quakefeed);
URLConnection connection ;
connection = url.openConnection();
HttpURLConnection httpconnection = (HttpURLConnection)connection;
int responsecode = httpconnection.getResponseCode();
if(responsecode == HttpURLConnection.HTTP_OK){
InputStream in = httpconnection.getInputStream();
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder() ;
Document dom = db.parse(in);
Element docelm = dom.getDocumentElement();
earthquake.clear();
NodeList nl = docelm.getElementsByTagName("entry");
if(nl != null && nl.getLength() > 0 ){
for(int i = 0 ; i < nl.getLength() ; ++i ){
Element entry = (Element)nl.item(i);
Element title = (Element)entry.getElementsByTagName("title").item(0);
String title_str = title.getFirstChild().getNodeValue();
final Quake quake = new Quake(title_str);
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
addNewQ(quake);
}
});
}
}
}
} catch (SAXException e) {
// TODO: handle exception
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void addNewQ(Quake _quake){
earthquake.add(_quake);
aa.notifyDataSetChanged();
}
}
package com.example.ex24;
public class Quake {
String detalis ;
public String getDetalis(){return detalis ;}
public Quake( String _str ) {
// TODO Auto-generated constructor stub
detalis = _str ;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Detalis :" + detalis ;
}
}
XML
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<fragment
android:name="com.example.ex24.MainActivity"
android:id="@+id/EarthquakeListFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ex24</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="quake_feed">http://earthquake.usgs.gov/eqcenter/catalogs/1day-M2.5.xml</string>
now, Why do I get this error?
Unfortunately, ex24 has stopped.
12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.951: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.951: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:03.011: D/AndroidRuntime(1187): Shutting down VM
12-24 16:49:03.011: W/dalvikvm(1187): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-24 16:49:03.091: E/AndroidRuntime(1187): FATAL EXCEPTION: main
12-24 16:49:03.091: E/AndroidRuntime(1187): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ex24/com.example.ex24.MainActivity}: java.lang.ClassCastException: com.example.ex24.MainActivity cannot be cast to android.app.Activity
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.os.Looper.loop(Looper.java:137)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-24 16:49:03.091: E/AndroidRuntime(1187): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 16:49:03.091: E/AndroidRuntime(1187): at java.lang.reflect.Method.invoke(Method.java:511)
12-24 16:49:03.091: E/AndroidRuntime(1187): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-24 16:49:03.091: E/AndroidRuntime(1187): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-24 16:49:03.091: E/AndroidRuntime(1187): at dalvik.system.NativeStart.main(Native Method)
12-24 16:49:03.091: E/AndroidRuntime(1187): Caused by: java.lang.ClassCastException: com.example.ex24.MainActivity cannot be cast to android.app.Activity
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
12-24 16:49:03.091: E/AndroidRuntime(1187): ... 11 more
Is this true?
This is My Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ex24"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.ex24.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
java android multithreading
add a comment |
I have two class : MainActivity and Quake.
package com.example.ex24;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.w3c.dom.Element;
import android.app.ListFragment;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
public class MainActivity extends ListFragment {
ArrayAdapter<Quake> aa ;
ArrayList<Quake> earthquake = new ArrayList<Quake>();
private Handler handler = new Handler();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
int layoutid = android.R.layout.simple_expandable_list_item_1 ;
aa = new ArrayAdapter<Quake>(getActivity(), layoutid, earthquake);
setListAdapter(aa);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
refreshQ();
}
});t.start();
}
public void refreshQ(){
URL url ;
try{
String quakefeed = getString(R.string.quake_feed);
url = new URL(quakefeed);
URLConnection connection ;
connection = url.openConnection();
HttpURLConnection httpconnection = (HttpURLConnection)connection;
int responsecode = httpconnection.getResponseCode();
if(responsecode == HttpURLConnection.HTTP_OK){
InputStream in = httpconnection.getInputStream();
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder() ;
Document dom = db.parse(in);
Element docelm = dom.getDocumentElement();
earthquake.clear();
NodeList nl = docelm.getElementsByTagName("entry");
if(nl != null && nl.getLength() > 0 ){
for(int i = 0 ; i < nl.getLength() ; ++i ){
Element entry = (Element)nl.item(i);
Element title = (Element)entry.getElementsByTagName("title").item(0);
String title_str = title.getFirstChild().getNodeValue();
final Quake quake = new Quake(title_str);
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
addNewQ(quake);
}
});
}
}
}
} catch (SAXException e) {
// TODO: handle exception
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void addNewQ(Quake _quake){
earthquake.add(_quake);
aa.notifyDataSetChanged();
}
}
package com.example.ex24;
public class Quake {
String detalis ;
public String getDetalis(){return detalis ;}
public Quake( String _str ) {
// TODO Auto-generated constructor stub
detalis = _str ;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Detalis :" + detalis ;
}
}
XML
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<fragment
android:name="com.example.ex24.MainActivity"
android:id="@+id/EarthquakeListFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ex24</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="quake_feed">http://earthquake.usgs.gov/eqcenter/catalogs/1day-M2.5.xml</string>
now, Why do I get this error?
Unfortunately, ex24 has stopped.
12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.951: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.951: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:03.011: D/AndroidRuntime(1187): Shutting down VM
12-24 16:49:03.011: W/dalvikvm(1187): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-24 16:49:03.091: E/AndroidRuntime(1187): FATAL EXCEPTION: main
12-24 16:49:03.091: E/AndroidRuntime(1187): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ex24/com.example.ex24.MainActivity}: java.lang.ClassCastException: com.example.ex24.MainActivity cannot be cast to android.app.Activity
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.os.Looper.loop(Looper.java:137)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-24 16:49:03.091: E/AndroidRuntime(1187): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 16:49:03.091: E/AndroidRuntime(1187): at java.lang.reflect.Method.invoke(Method.java:511)
12-24 16:49:03.091: E/AndroidRuntime(1187): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-24 16:49:03.091: E/AndroidRuntime(1187): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-24 16:49:03.091: E/AndroidRuntime(1187): at dalvik.system.NativeStart.main(Native Method)
12-24 16:49:03.091: E/AndroidRuntime(1187): Caused by: java.lang.ClassCastException: com.example.ex24.MainActivity cannot be cast to android.app.Activity
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
12-24 16:49:03.091: E/AndroidRuntime(1187): ... 11 more
Is this true?
This is My Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ex24"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.ex24.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
java android multithreading
can you post manifest file and how you invoke the fragment
– Raghunandan
Dec 24 '13 at 16:57
You are trying to cast ListFragment as Activity, try to "getActivity()" instead
– yahya
Dec 24 '13 at 16:59
Manifest added.
– user3103823
Dec 24 '13 at 19:56
add a comment |
I have two class : MainActivity and Quake.
package com.example.ex24;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.w3c.dom.Element;
import android.app.ListFragment;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
public class MainActivity extends ListFragment {
ArrayAdapter<Quake> aa ;
ArrayList<Quake> earthquake = new ArrayList<Quake>();
private Handler handler = new Handler();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
int layoutid = android.R.layout.simple_expandable_list_item_1 ;
aa = new ArrayAdapter<Quake>(getActivity(), layoutid, earthquake);
setListAdapter(aa);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
refreshQ();
}
});t.start();
}
public void refreshQ(){
URL url ;
try{
String quakefeed = getString(R.string.quake_feed);
url = new URL(quakefeed);
URLConnection connection ;
connection = url.openConnection();
HttpURLConnection httpconnection = (HttpURLConnection)connection;
int responsecode = httpconnection.getResponseCode();
if(responsecode == HttpURLConnection.HTTP_OK){
InputStream in = httpconnection.getInputStream();
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder() ;
Document dom = db.parse(in);
Element docelm = dom.getDocumentElement();
earthquake.clear();
NodeList nl = docelm.getElementsByTagName("entry");
if(nl != null && nl.getLength() > 0 ){
for(int i = 0 ; i < nl.getLength() ; ++i ){
Element entry = (Element)nl.item(i);
Element title = (Element)entry.getElementsByTagName("title").item(0);
String title_str = title.getFirstChild().getNodeValue();
final Quake quake = new Quake(title_str);
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
addNewQ(quake);
}
});
}
}
}
} catch (SAXException e) {
// TODO: handle exception
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void addNewQ(Quake _quake){
earthquake.add(_quake);
aa.notifyDataSetChanged();
}
}
package com.example.ex24;
public class Quake {
String detalis ;
public String getDetalis(){return detalis ;}
public Quake( String _str ) {
// TODO Auto-generated constructor stub
detalis = _str ;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Detalis :" + detalis ;
}
}
XML
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<fragment
android:name="com.example.ex24.MainActivity"
android:id="@+id/EarthquakeListFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ex24</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="quake_feed">http://earthquake.usgs.gov/eqcenter/catalogs/1day-M2.5.xml</string>
now, Why do I get this error?
Unfortunately, ex24 has stopped.
12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.951: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.951: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:03.011: D/AndroidRuntime(1187): Shutting down VM
12-24 16:49:03.011: W/dalvikvm(1187): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-24 16:49:03.091: E/AndroidRuntime(1187): FATAL EXCEPTION: main
12-24 16:49:03.091: E/AndroidRuntime(1187): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ex24/com.example.ex24.MainActivity}: java.lang.ClassCastException: com.example.ex24.MainActivity cannot be cast to android.app.Activity
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.os.Looper.loop(Looper.java:137)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-24 16:49:03.091: E/AndroidRuntime(1187): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 16:49:03.091: E/AndroidRuntime(1187): at java.lang.reflect.Method.invoke(Method.java:511)
12-24 16:49:03.091: E/AndroidRuntime(1187): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-24 16:49:03.091: E/AndroidRuntime(1187): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-24 16:49:03.091: E/AndroidRuntime(1187): at dalvik.system.NativeStart.main(Native Method)
12-24 16:49:03.091: E/AndroidRuntime(1187): Caused by: java.lang.ClassCastException: com.example.ex24.MainActivity cannot be cast to android.app.Activity
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
12-24 16:49:03.091: E/AndroidRuntime(1187): ... 11 more
Is this true?
This is My Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ex24"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.ex24.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
java android multithreading
I have two class : MainActivity and Quake.
package com.example.ex24;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.w3c.dom.Element;
import android.app.ListFragment;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
public class MainActivity extends ListFragment {
ArrayAdapter<Quake> aa ;
ArrayList<Quake> earthquake = new ArrayList<Quake>();
private Handler handler = new Handler();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
int layoutid = android.R.layout.simple_expandable_list_item_1 ;
aa = new ArrayAdapter<Quake>(getActivity(), layoutid, earthquake);
setListAdapter(aa);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
refreshQ();
}
});t.start();
}
public void refreshQ(){
URL url ;
try{
String quakefeed = getString(R.string.quake_feed);
url = new URL(quakefeed);
URLConnection connection ;
connection = url.openConnection();
HttpURLConnection httpconnection = (HttpURLConnection)connection;
int responsecode = httpconnection.getResponseCode();
if(responsecode == HttpURLConnection.HTTP_OK){
InputStream in = httpconnection.getInputStream();
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder() ;
Document dom = db.parse(in);
Element docelm = dom.getDocumentElement();
earthquake.clear();
NodeList nl = docelm.getElementsByTagName("entry");
if(nl != null && nl.getLength() > 0 ){
for(int i = 0 ; i < nl.getLength() ; ++i ){
Element entry = (Element)nl.item(i);
Element title = (Element)entry.getElementsByTagName("title").item(0);
String title_str = title.getFirstChild().getNodeValue();
final Quake quake = new Quake(title_str);
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
addNewQ(quake);
}
});
}
}
}
} catch (SAXException e) {
// TODO: handle exception
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void addNewQ(Quake _quake){
earthquake.add(_quake);
aa.notifyDataSetChanged();
}
}
package com.example.ex24;
public class Quake {
String detalis ;
public String getDetalis(){return detalis ;}
public Quake( String _str ) {
// TODO Auto-generated constructor stub
detalis = _str ;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Detalis :" + detalis ;
}
}
XML
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<fragment
android:name="com.example.ex24.MainActivity"
android:id="@+id/EarthquakeListFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ex24</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="quake_feed">http://earthquake.usgs.gov/eqcenter/catalogs/1day-M2.5.xml</string>
now, Why do I get this error?
Unfortunately, ex24 has stopped.
12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.751: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.951: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:02.951: W/Trace(1187): Unexpected value from nativeGetEnabledTags: 0
12-24 16:49:03.011: D/AndroidRuntime(1187): Shutting down VM
12-24 16:49:03.011: W/dalvikvm(1187): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-24 16:49:03.091: E/AndroidRuntime(1187): FATAL EXCEPTION: main
12-24 16:49:03.091: E/AndroidRuntime(1187): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ex24/com.example.ex24.MainActivity}: java.lang.ClassCastException: com.example.ex24.MainActivity cannot be cast to android.app.Activity
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.os.Looper.loop(Looper.java:137)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-24 16:49:03.091: E/AndroidRuntime(1187): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 16:49:03.091: E/AndroidRuntime(1187): at java.lang.reflect.Method.invoke(Method.java:511)
12-24 16:49:03.091: E/AndroidRuntime(1187): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-24 16:49:03.091: E/AndroidRuntime(1187): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-24 16:49:03.091: E/AndroidRuntime(1187): at dalvik.system.NativeStart.main(Native Method)
12-24 16:49:03.091: E/AndroidRuntime(1187): Caused by: java.lang.ClassCastException: com.example.ex24.MainActivity cannot be cast to android.app.Activity
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
12-24 16:49:03.091: E/AndroidRuntime(1187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
12-24 16:49:03.091: E/AndroidRuntime(1187): ... 11 more
Is this true?
This is My Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ex24"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.ex24.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
java android multithreading
java android multithreading
edited Nov 20 at 17:17
Cœur
17.4k9102144
17.4k9102144
asked Dec 24 '13 at 16:53
user3103823
75311
75311
can you post manifest file and how you invoke the fragment
– Raghunandan
Dec 24 '13 at 16:57
You are trying to cast ListFragment as Activity, try to "getActivity()" instead
– yahya
Dec 24 '13 at 16:59
Manifest added.
– user3103823
Dec 24 '13 at 19:56
add a comment |
can you post manifest file and how you invoke the fragment
– Raghunandan
Dec 24 '13 at 16:57
You are trying to cast ListFragment as Activity, try to "getActivity()" instead
– yahya
Dec 24 '13 at 16:59
Manifest added.
– user3103823
Dec 24 '13 at 19:56
can you post manifest file and how you invoke the fragment
– Raghunandan
Dec 24 '13 at 16:57
can you post manifest file and how you invoke the fragment
– Raghunandan
Dec 24 '13 at 16:57
You are trying to cast ListFragment as Activity, try to "getActivity()" instead
– yahya
Dec 24 '13 at 16:59
You are trying to cast ListFragment as Activity, try to "getActivity()" instead
– yahya
Dec 24 '13 at 16:59
Manifest added.
– user3103823
Dec 24 '13 at 19:56
Manifest added.
– user3103823
Dec 24 '13 at 19:56
add a comment |
2 Answers
2
active
oldest
votes
I guess you have this 'Activity' declared in the manifest as an activity. However, it's a fragment. The system tries to start it, casting it to an Activity in the process, but that is obviously not possible because it's a fragment.
Manifest added.
– user3103823
Dec 24 '13 at 20:09
My answer is correct, in the manifest you declare 'MainActivity' as an activity, but it's a fragment (it extendsListFragment
). You need to either change this to extend fromListActivity
, or change your manifest and declare your actual activity there (the one that loads theListFragment
which you callMainActivity
).
– Jeffrey Klardie
Dec 24 '13 at 23:45
add a comment |
You should change your "class MainActivity extends ListFragment"
into
class MainActivity extends ListActivity
as currently your MainActivity isn't an Activity but a Fragment.
But in your case what you really want is an Activity
You will also get some other errors as Activity has different methods than Fragment
Notes:
Fragment do not need to be in the manifest.
I wrote this example from :[Reto_Meier]_Professional_Android_4_Application_De - Page 206 !
– user3103823
Dec 24 '13 at 20:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20764256%2fxml-parser-and-thread-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I guess you have this 'Activity' declared in the manifest as an activity. However, it's a fragment. The system tries to start it, casting it to an Activity in the process, but that is obviously not possible because it's a fragment.
Manifest added.
– user3103823
Dec 24 '13 at 20:09
My answer is correct, in the manifest you declare 'MainActivity' as an activity, but it's a fragment (it extendsListFragment
). You need to either change this to extend fromListActivity
, or change your manifest and declare your actual activity there (the one that loads theListFragment
which you callMainActivity
).
– Jeffrey Klardie
Dec 24 '13 at 23:45
add a comment |
I guess you have this 'Activity' declared in the manifest as an activity. However, it's a fragment. The system tries to start it, casting it to an Activity in the process, but that is obviously not possible because it's a fragment.
Manifest added.
– user3103823
Dec 24 '13 at 20:09
My answer is correct, in the manifest you declare 'MainActivity' as an activity, but it's a fragment (it extendsListFragment
). You need to either change this to extend fromListActivity
, or change your manifest and declare your actual activity there (the one that loads theListFragment
which you callMainActivity
).
– Jeffrey Klardie
Dec 24 '13 at 23:45
add a comment |
I guess you have this 'Activity' declared in the manifest as an activity. However, it's a fragment. The system tries to start it, casting it to an Activity in the process, but that is obviously not possible because it's a fragment.
I guess you have this 'Activity' declared in the manifest as an activity. However, it's a fragment. The system tries to start it, casting it to an Activity in the process, but that is obviously not possible because it's a fragment.
answered Dec 24 '13 at 16:56
Jeffrey Klardie
2,65911122
2,65911122
Manifest added.
– user3103823
Dec 24 '13 at 20:09
My answer is correct, in the manifest you declare 'MainActivity' as an activity, but it's a fragment (it extendsListFragment
). You need to either change this to extend fromListActivity
, or change your manifest and declare your actual activity there (the one that loads theListFragment
which you callMainActivity
).
– Jeffrey Klardie
Dec 24 '13 at 23:45
add a comment |
Manifest added.
– user3103823
Dec 24 '13 at 20:09
My answer is correct, in the manifest you declare 'MainActivity' as an activity, but it's a fragment (it extendsListFragment
). You need to either change this to extend fromListActivity
, or change your manifest and declare your actual activity there (the one that loads theListFragment
which you callMainActivity
).
– Jeffrey Klardie
Dec 24 '13 at 23:45
Manifest added.
– user3103823
Dec 24 '13 at 20:09
Manifest added.
– user3103823
Dec 24 '13 at 20:09
My answer is correct, in the manifest you declare 'MainActivity' as an activity, but it's a fragment (it extends
ListFragment
). You need to either change this to extend from ListActivity
, or change your manifest and declare your actual activity there (the one that loads the ListFragment
which you call MainActivity
).– Jeffrey Klardie
Dec 24 '13 at 23:45
My answer is correct, in the manifest you declare 'MainActivity' as an activity, but it's a fragment (it extends
ListFragment
). You need to either change this to extend from ListActivity
, or change your manifest and declare your actual activity there (the one that loads the ListFragment
which you call MainActivity
).– Jeffrey Klardie
Dec 24 '13 at 23:45
add a comment |
You should change your "class MainActivity extends ListFragment"
into
class MainActivity extends ListActivity
as currently your MainActivity isn't an Activity but a Fragment.
But in your case what you really want is an Activity
You will also get some other errors as Activity has different methods than Fragment
Notes:
Fragment do not need to be in the manifest.
I wrote this example from :[Reto_Meier]_Professional_Android_4_Application_De - Page 206 !
– user3103823
Dec 24 '13 at 20:22
add a comment |
You should change your "class MainActivity extends ListFragment"
into
class MainActivity extends ListActivity
as currently your MainActivity isn't an Activity but a Fragment.
But in your case what you really want is an Activity
You will also get some other errors as Activity has different methods than Fragment
Notes:
Fragment do not need to be in the manifest.
I wrote this example from :[Reto_Meier]_Professional_Android_4_Application_De - Page 206 !
– user3103823
Dec 24 '13 at 20:22
add a comment |
You should change your "class MainActivity extends ListFragment"
into
class MainActivity extends ListActivity
as currently your MainActivity isn't an Activity but a Fragment.
But in your case what you really want is an Activity
You will also get some other errors as Activity has different methods than Fragment
Notes:
Fragment do not need to be in the manifest.
You should change your "class MainActivity extends ListFragment"
into
class MainActivity extends ListActivity
as currently your MainActivity isn't an Activity but a Fragment.
But in your case what you really want is an Activity
You will also get some other errors as Activity has different methods than Fragment
Notes:
Fragment do not need to be in the manifest.
answered Dec 24 '13 at 20:17
Benoit
3,80632040
3,80632040
I wrote this example from :[Reto_Meier]_Professional_Android_4_Application_De - Page 206 !
– user3103823
Dec 24 '13 at 20:22
add a comment |
I wrote this example from :[Reto_Meier]_Professional_Android_4_Application_De - Page 206 !
– user3103823
Dec 24 '13 at 20:22
I wrote this example from :[Reto_Meier]_Professional_Android_4_Application_De - Page 206 !
– user3103823
Dec 24 '13 at 20:22
I wrote this example from :[Reto_Meier]_Professional_Android_4_Application_De - Page 206 !
– user3103823
Dec 24 '13 at 20:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20764256%2fxml-parser-and-thread-in-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you post manifest file and how you invoke the fragment
– Raghunandan
Dec 24 '13 at 16:57
You are trying to cast ListFragment as Activity, try to "getActivity()" instead
– yahya
Dec 24 '13 at 16:59
Manifest added.
– user3103823
Dec 24 '13 at 19:56