2011-01-14 24 views
0

我在Android模拟器上执行时强制关闭。我无法弄清楚错误。以下是主要活动SimpleJokeList.java在这个程序中出现什么错误,我得到Force关闭此

package edu.calpoly.android.lab2; 

import java.util.ArrayList; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.ScrollView; 
import android.widget.TextView; 

public class SimpleJokeList extends Activity { 

    /** Contains the list Jokes the Activity will present to the user **/ 
    protected ArrayList<Joke> m_arrJokeList; 

    /** 
    * LinearLayout used for maintaining a list of Views that each display Jokes 
    **/ 
    protected LinearLayout m_vwJokeLayout; 

    /** 
    * EditText used for entering text for a new Joke to be added to 
    * m_arrJokeList. 
    **/ 
    protected EditText m_vwJokeEditText; 

    /** 
    * Button used for creating and adding a new Joke to m_arrJokeList using the 
    * text entered in m_vwJokeEditText. 
    **/ 
    protected Button m_vwJokeButton; 

    /** 
    * Background Color values used for alternating between light and dark rows 
    * of Jokes. 
    */ 
    protected int m_nDarkColor; 
    protected int m_nLightColor; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     initLayout(); 
     // TODO 
     String[] jokestring=getResources().getStringArray(R.array.jokeList);  
     for(String str : jokestring) { 
       addJoke(str); 
      } 
     } 

    /** 
    * Method used to encapsulate the code that initializes and sets the Layout 
    * for this Activity. 
    */ 
    protected void initLayout() { 
     // TODO 
     m_vwJokeLayout=new LinearLayout(this); 
     m_vwJokeLayout.setOrientation(LinearLayout.VERTICAL); 
     ScrollView Sv=new ScrollView(this); 
     Sv.addView(m_vwJokeLayout); 
     setContentView(Sv); 


    } 

    /** 
    * Method used to encapsulate the code that initializes and sets the Event 
    * Listeners which will respond to requests to "Add" a new Joke to the 
    * list. 
    */ 
    protected void initAddJokeListeners() { 
     // TODO 
    } 

    /** 
    * Method used for encapsulating the logic necessary to properly initialize 
    * a new joke, add it to m_arrJokeList, and display it on screen. 
    * 
    * @param strJoke 
    *   A string containing the text of the Joke to add. 
    */ 
    protected void addJoke(String strJoke) { 
     // TODO 
     Joke j=new Joke(strJoke); 
     m_arrJokeList.add(j); 
     TextView TV=new TextView(this); 
     m_vwJokeLayout.addView(TV); 
     TV.setText(strJoke); 
    } 
} 

下面的代码Joke.java

的代码
package edu.calpoly.android.lab2; 
public class Joke { 
    public static final int UNRATED = 0; 
    public static final int LIKE = 1; 
    public static final int DISLIKE = 2; 
    private String m_strJoke; 
    private int m_nRating; 
    public Joke() { 
     //TODO 
     m_strJoke=""; 
     m_nRating=UNRATED; 
    } 
    public Joke(String strJoke) { 
     //TODO 
     m_strJoke=strJoke; 
     m_nRating=UNRATED; 
    } 
    public Joke(String strJoke, int nRating) { 
     //TODO 
     m_strJoke=strJoke; 
     m_nRating=nRating; 
    } 
    public String getJoke() { 
     //TODO 
     return m_strJoke; 
    } 
    public void setJoke(String mstrJoke) { 
     //TODO 
     m_strJoke=mstrJoke; 
    } 
    public int getRating() { 
     //TODO 
     return m_nRating; 
    } 
    public void setRating(int rating) { 
     //TODO 
     m_nRating=rating; 
    } 
    @Override 
    public String toString() { 
     //TODO 
     return m_strJoke; 
    } 
    @Override 
    public boolean equals(Object obj) { 
     //TODO 
     boolean ret=false; 
     if(obj!=null) 
     { 
      if(obj.getClass()==Joke.class) 
       if(obj.toString().equals(this.m_strJoke)) 
        ret=true; 
     } 
     else 
      ret=false; 

     return ret; 
    } 
} 

下面是的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="edu.calpoly.android.lab2" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> 
     <uses-library android:name="android.test.runner" /> 
     <activity android:name=".SimpleJokeList" 
        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> 
    <uses-sdk android:minSdkVersion="4" /> 
    <instrumentation android:name="android.test.InstrumentationTestRunner" 
        android:targetPackage="edu.calpoly.android.lab2" 
        android:label="Tests for Api Demos."/> 
</manifest> 
+0

请正确格式化您的问题,以便它的可读性。确保代码块以四个空格缩进。您可以选择一段文字并点击“{}”按钮来完成此操作。 – 2011-01-14 05:02:37

+0

感谢您的回复...格式正确..请检查 – naquiuddin 2011-01-14 05:07:24

回答

0

您还没有初始化m_arrJokeList。

Android App课程。实验2 ...

在公共无效的onCreate(捆绑savedInstance)方法:

通知的super.onCreate(savedInstance)调用。这是至关重要的,永远不要忘记打这个电话。如果你不这样做,你的活动将无法工作。

调用this.getResources(),它将返回一个Resources对象。 Resources类提供了一个通过其resourceID检索资源的接口。

RESOURCEID的可以在静态R类别可以发现,根据各自的资源子类(这是他们的资源类型命名),在资源文件给他们名下:R.array.jokeList

创建一个Joke ArrayList的实例。

m_arrJokeList = new ArrayList<Joke>(); 
相关问题