2011-01-14 7 views
0

I Androidエミュレータで強制実行すると強制終了します。私はエラーを理解することはできません。ここでの主な活動SimpleJokeList.javaこここのプログラムのエラーは何ですか?私はこのために強制終了します

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

あなたの質問は正しい形式で入力してください。コードブロックが4つのスペースでインデントされていることを確認してください。テキストのブロックを選択して「{}」ボタンをクリックすると、これを行うことができます。 –

+0

返信ありがとうございます...正しくフォーマットされました。ご確認ください – naquiuddin

答えて

2

あなたはm_arrJokeList

を初期化していないのコードされています

try

protected ArrayList<Joke> m_arrJokeList = new ArrayList<Joke>(); 

注:Jokeクラスでequalsメソッドをオーバーライドしているので、あなたが

What issues should be considered when overriding equals and hashCode in Java?

+0

ありがとうございました....私の問題は解決されました。もう1つの質問..すべてのオブジェクト宣言はJavaで初期化する必要がありますか?どんな資源? – naquiuddin

+0

@andyfan値の型フィールドを除き、デフォルト値に自動的に初期化されます。 http://tinyurl.com/66vt58b :-) –

+0

Javaでequals&hasCodeをオーバーライドすることについて – naquiuddin

0

aswell hashCodeメソッドをオーバーライドする必要がありますあなたはm_arrJokeListを初期化されていません。

Android App Course。演習2 ...公共のボイドのonCreate(バンドルsavedInstance)の方法では

お知らせsuper.onCreate(savedInstance)コール。これは非常に重要なことです。決してこの電話を忘れないでください。あなたがしない場合、あなたの活動は動作しません。

this.getResources()を呼び出すと、Resourcesオブジェクトが返されます。 Resourcesクラスは、resourceIDでリソースを取得するためのインターフェイスを提供します。 R.array.jokeList

:のリソースファイルでそれらに与えられた名前で、(そのリソースタイプにちなんで命名されている)には、それぞれのリソースのサブクラスの下で、静的Rクラスで見つけることができ

RESOURCEID Joke ArrayListのインスタンスを作成します。

m_arrJokeList = new ArrayList<Joke>(); 
関連する問題