インテントを使用して別のアクティビティからこのアクティビティを開こうとすると、アプリが閉じられます。実際に私は質問を表示するクイズのようなアプリを作ろうとしています。XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextSwitcher
android:id="@+id/textSwitcher1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.0">
</TextSwitcher>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:padding="4dp"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="24dp"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="42dp"
android:text="Button" />
</RelativeLayout>
アクティビティ :
public class CQAct extends Activity implements ViewSwitcher.ViewFactory {
TextSwitcher tsw;
Button next,prev;
int m_iCurrIndex=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cq);
tsw=(TextSwitcher) findViewById(R.id.textSwitcher1);
tsw.setFactory(this);
next=(Button) findViewById(R.id.button2);
prev=(Button) findViewById(R.id.button1);
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
tsw.setInAnimation(in);
tsw.setOutAnimation(out);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
m_iCurrIndex++;
updateCounter();
}
});
prev.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
m_iCurrIndex--;
updateCounter();
}
});
updateCounter();
}
void updateCounter() {
String str= getData();
tsw.setText(str);
}
String getData()
{
return new StringBuilder(String.valueOf(new StringBuilder(String.valueOf(new StringBuilder(String.valueOf(new StringBuilder(String.valueOf(new StringBuilder(String.valueOf("\n")).append("Question ").append(1 + this.m_iCurrIndex).append(":\n").toString())).append(QAs.strQs[this.m_iCurrIndex]).toString())).append("\n\n").toString())).append("Answer:\n").toString())).append(QAs.strAs[this.m_iCurrIndex]).toString() + "\n\n";
}
public View makeView() {
TextView myText = new TextView(this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextSize(15);
myText.setTextColor(Color.BLUE);
return myText;
}
}
私の静的クラス:
public class QAs {
public static final int MAX_QAS = 3;
public static String[] strAs;
public static String[] strQs;
QAs() {
strQs = new String[3];
strAs = new String[3];
strQs[0] = "What is a your name?";
strAs[0] = "xyz";
strQs[1] = "abcd?";
strAs[1] = "keyword";
strQs[2] = "abcd?";
strAs[2] = "aabbccdd";
}
}
エラーは次のとおりです。
Error :
09-07 17:52:29.226: W/dalvikvm(23855): threadid=1: thread exiting with uncaught exception (group=0x40ce0450)
09-07 17:52:29.226: E/AndroidRuntime(23855): FATAL EXCEPTION: main
09-07 17:52:29.226: E/AndroidRuntime(23855): java.lang.NullPointerException
09-07 17:52:29.226: E/AndroidRuntime(23855): at com.ankur.interviewquestions.CQAct.getData(CQAct.java:50)
09-07 17:52:29.226: E/AndroidRuntime(23855): at com.ankur.interviewquestions.CQAct.updateCounter(CQAct.java:44)
09-07 17:52:29.226: E/AndroidRuntime(23855): at com.ankur.interviewquestions.CQAct.onClick(CQAct.java:65)
09-07 17:52:29.226: E/AndroidRuntime(23855): at android.view.View.performClick(View.java:4129)
09-07 17:52:29.226: E/AndroidRuntime(23855): at android.view.View$PerformClick.run(View.java:17143)
09-07 17:52:29.226: E/AndroidRuntime(23855): at android.os.Handler.handleCallback(Handler.java:615)
09-07 17:52:29.226: E/AndroidRuntime(23855): at android.os.Handler.dispatchMessage(Handler.java:92)
09-07 17:52:29.226: E/AndroidRuntime(23855): at android.os.Looper.loop(Looper.java:137)
09-07 17:52:29.226: E/AndroidRuntime(23855): at android.app.ActivityThread.main(ActivityThread.java:4802)