开发者

What's error in this program, I get Force close for this

I Get force close when executed on Android emulator. I can't figure out the error. Here is code of Main Activity 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);
    }
}

Here is code of 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
        开发者_Python百科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;
    }
}

Here is Code of 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> 


You are not initializing m_arrJokeList

try

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

Note: Since you are overriding equals method in the Joke class, you should override the hashCode method aswell

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


You are not initializing m_arrJokeList.

Android App Course. Lab 2 ...

In the public void onCreate(Bundle savedInstance) method:

Notice the super.onCreate(savedInstance) call. This is crucial, never forget to make this call. If you don't your Activity won't work.

Make a call to this.getResources() , which will return a Resources object. The Resources class provides an interface for retrieving resources by their resourceID.

resourceID's can be found in the static R class, under their respective resource subclasses (which are named after their resource type), under the name given to them in the resource file: R.array.jokeList

Create an instance of the Joke ArrayList.

m_arrJokeList = new ArrayList<Joke>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜