typedefcoder2
typedefcoder2

Reputation: 300

Multiple inheritance vs usage of Activity & ListActivity Class in Java

I have just started working with Android ListView. However, setAdapter function in my program works only when ListActivity is extended by class DisplayMovie. While I use ListActivity and remove Activity, the application crashes. Since multiple Inheritance cannot be supported, I wish to know if there is a way by which I can use both ListViewActivity as well as Activity classes in my program.

//public class DisplayMovies extends ListActivity {
public class DisplayMovies extends Activity {
    /** Called when the activity is first created. */
    public String flixURL="";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main3);

        Bundle extras = getIntent().getExtras();        
        int myzip = extras.getInt("globalZip");        
        flixURL=("http://servermin.com:19818/cgi-bin/movielog.pl?zip=" + myzip);        
        displayAllMovieList();                
    }

    public void displayAllMovieList()
    {
        SAXBuilder builder = new SAXBuilder();
        URL url;

        try 
        {
            url = new URL(flixURL);
            Document doc = null;
            doc=builder.build(url);             
            Element root = doc.getRootElement();                                
            List children = root.getChild("movies").getChildren("movie");

            System.out.println("Tracker3");
                    int MovieNum=children.size();  //Count the number of Movies                 
                    String[] MovieName=new String[MovieNum]; //Initialize a String 
                    String[] MovieCover=new String[MovieNum];                                       
                    String[] ShowTime=new String[MovieNum];                 
                    String TheatereName = ((Element) doc.getRootElement().getChild("movies").getChildren("movie").get(0)).getAttributeValue( "theatre" );                                       
                    String TweetText="";                                          
                    for (int i = 0; i < children.size(); i++) 
                    {   
                        Element movieAtt = (Element)doc.getRootElement().getChild("movies").getChildren("movie").get(i);                                            
                        MovieName[i]=movieAtt.getAttributeValue( "Title" );
                        MovieCover[i]=movieAtt.getAttributeValue( "cover" );
                        ShowTime[i]=movieAtt.getAttributeValue( "showtime" );
                            finalTime=movieAtt.getAttributeValue( "showtime" ).substring(0,8);
                        TweetText+=" I will see "+movieAtt.getAttributeValue("Title")+" at "+TheatereName+" at "+finalTime+"\n";


                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, MovieName);
                        setListAdapter(adapter);

                        //TextView tv = new TextView(this);
                        //tv.setText(TweetText);
                        //setContentView(tv);                                               
                    }                     

        } 
        catch (MalformedURLException e1) 
        {
            e1.printStackTrace();
            System.out.println("ErrorThrowedIs: "+e1);
            Toast.makeText(getBaseContext(), e1.toString(),5);
        } 
        catch (JDOMException e2) 
        {
            e2.printStackTrace();
            System.out.println("ErrorThrowedIs: "+e2);
            Toast.makeText(getBaseContext(), e2.toString(),5);
        }
        catch (IOException e3) 
        {
            e3.printStackTrace();
            System.out.println("ErrorThrowedIs: "+e3);
            Toast.makeText(getBaseContext(), e3.toString(),5);
        }
    }
}

Error Log Says :

11-21 02:18:19.609: W/dalvikvm(21193): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
11-21 02:18:19.629: E/AndroidRuntime(21193): FATAL EXCEPTION: main
11-21 02:18:19.629: E/AndroidRuntime(21193): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vj/com.vj.DisplayMovies}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
11-21 02:18:19.629: E/AndroidRuntime(21193):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at android.os.Looper.loop(Looper.java:123)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at android.app.ActivityThread.main(ActivityThread.java:4627)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at java.lang.reflect.Method.invokeNative(Native Method)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at java.lang.reflect.Method.invoke(Method.java:521)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at dalvik.system.NativeStart.main(Native Method)
11-21 02:18:19.629: E/AndroidRuntime(21193): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
11-21 02:18:19.629: E/AndroidRuntime(21193):    at android.app.ListActivity.onContentChanged(ListActivity.java:245)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:203)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at android.app.Activity.setContentView(Activity.java:1647)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at com.vj.DisplayMovies.onCreate(DisplayMovies.java:30)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-21 02:18:19.629: E/AndroidRuntime(21193):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-21 02:18:19.629: E/AndroidRuntime(21193):    ... 11 more

Updated XML:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">

<ListView android:id="@android:id/list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#00FF00"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

     <TextView android:id="@android:id/empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#FF0000"
               android:text="No data"/>

 </LinearLayout>

Updated Code for the class:

public class DisplayMovies extends ListActivity {
//public class DisplayMovies extends Activity {
    /** Called when the activity is first created. */
    public String flixURL="";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main3);

        Bundle extras = getIntent().getExtras();        
        int myzip = extras.getInt("globalZip");        
        flixURL=("http://myweb.com/cgi-bin/movielog.pl?zip=" + myzip);      
        displayAllMovieList();                
    }

    public void displayAllMovieList()
    {
        SAXBuilder builder = new SAXBuilder();
        URL url;

        try 
        {
            url = new URL(flixURL);
            Document doc = null;
            doc=builder.build(url);             
            Element root = doc.getRootElement();                                
            List children = root.getChild("movies").getChildren("movie");

            System.out.println("Tracker2");
                    System.out.println("Tracker3");
                    int MovieNum=children.size();  //Count the number of Movies                 
                    String[] MovieName=new String[MovieNum]; //Initialize a String 
                    String[] MovieCover=new String[MovieNum];                                       
                    String[] ShowTime=new String[MovieNum];                 
                    String TheatereName = ((Element) doc.getRootElement().getChild("movies").getChildren("movie").get(0)).getAttributeValue( "theatre" );                                       
                    String TweetText="";                                          
                    for (int i = 0; i < children.size(); i++) 
                    {   
                        Element movieAtt = (Element)doc.getRootElement().getChild("movies").getChildren("movie").get(i);                                            
                        MovieName[i]=movieAtt.getAttributeValue( "Title" );
                        MovieCover[i]=movieAtt.getAttributeValue( "cover" );
                        ShowTime[i]=movieAtt.getAttributeValue( "showtime" );
                            finalTime=movieAtt.getAttributeValue( "showtime" ).substring(0,8);  TweetText+=" I will see "+movieAtt.getAttributeValue("Title")+" at "+TheatereName+" at "+finalTime+"\n";


                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, MovieName);
                        setListAdapter(adapter);


                }               
        } 
        catch (MalformedURLException e1) 
        {
            e1.printStackTrace();
            System.out.println("ErrorThrowedIs: "+e1);
            Toast.makeText(getBaseContext(), e1.toString(),5);
        } 
        catch (JDOMException e2) 
        {
            e2.printStackTrace();
            System.out.println("ErrorThrowedIs: "+e2);
            Toast.makeText(getBaseContext(), e2.toString(),5);
        }
        catch (IOException e3) 
        {
            e3.printStackTrace();
            System.out.println("ErrorThrowedIs: "+e3);
            Toast.makeText(getBaseContext(), e3.toString(),5);
        }
    }
}

Upvotes: 0

Views: 675

Answers (1)

alex.zherdev
alex.zherdev

Reputation: 24174

First of all, if your app crashes, and you're asking a question, make sure you also include the stacktrace from the log.

Regarding your question, make sure you understand the preconditions to using ListActivity: you MUST have a listview in your layout, with an android:id="@android:id/list" (as stated in the docs). I'm quite positive this is the reason your app crashes.

You don't need multiple inheritance here anyway: ListActivity extends Activity, it would make no sense to extend them both, even if Java had multiple inheritance.

Upvotes: 3

Related Questions