Android 什么';在这个程序中有一个错误,我得到了这个的强制关闭
在Android emulator上执行时,我会强制关闭。我想不出这个错误。下面是主要活动SimpleJokeList.java的代码Android 什么';在这个程序中有一个错误,我得到了这个的强制关闭,android,Android,在Android emulator上执行时,我会强制关闭。我想不出这个错误。下面是主要活动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
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);
}
}
下面是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>
您没有初始化m_arrJokeList
试一试
protectedarraylist m_arrJokeList=newarraylist();
注意:由于您正在重写Joke
类中的equals
方法,因此也应该重写hashCode
方法
您没有初始化m_arrJokeList Android应用程序课程。实验二 在public void onCreate(Bundle savedInstance)方法中: 注意super.onCreate(savedInstance)调用。这是至关重要的,永远不要忘记打这个电话。如果你不这样做,你的活动就不会起作用 调用此.getResources(),它将返回一个Resources对象。 Resources类提供了一个接口,用于通过资源ID检索资源 resourceID可以在静态R类中找到,在其各自的资源子类(以其资源类型命名)下,在资源文件R.array.jokeList中为其指定的名称下 创建笑话数组列表的实例。
m_arrJokeList = new ArrayList<Joke>();
m_arrJokeList=new ArrayList();
请正确设置问题的格式,使其可读。确保代码块缩进四个空格。您可以选择一块文本并单击“{}”按钮来执行此操作。感谢您的回复…格式正确..请检查谢谢..我的问题已解决..还有一个问题..每个对象声明都必须用Java初始化吗?有任何资源吗?@andyfan正确,但值类型字段除外,它们会自动初始化为默认值。:-)关于在中重写equals&hasCodeJava@andyfan
protected ArrayList<Joke> m_arrJokeList = new ArrayList<Joke> ();
m_arrJokeList = new ArrayList<Joke>();