多重继承与使用Activity& Java中的ListActivity类

时间:2011-11-21 10:22:43

标签: android android-listview

我刚开始使用Android ListView。但是,只有当setAdapter被类ListActivity扩展时,我的程序中的DisplayMovie函数才有效。当我使用ListActivity并删除Activity时,应用程序崩溃了。由于无法支持多个继承,我想知道是否有一种方法可以在我的程序中同时使用ListViewActivityActivity类。

//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);
        }
    }
}

错误日志说:

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

更新了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>

更新了该课程的代码:

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);
        }
    }
}

1 个答案:

答案 0 :(得分:3)

首先,如果您的应用崩溃了,并且您在提问题,请确保您还包含日志中的堆栈跟踪。

关于您的问题,请确保您了解使用ListActivity的前提条件:您的布局中必须有一个列表视图,其中包含android:id="@android:id/list"(如docs中所述)。我非常肯定这是你的应用程序崩溃的原因。

无论如何你不需要多继承:ListActivity扩展Activity,扩展它们都没有意义,即使Java有多重继承。