-1

When i try to open this activity through another activity using an intent, app gets closed. Actually I'm trying to make n quiz like app, which show questions. 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>

Activity :

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;
 }
}

My static class:

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"; 
    } 
} 

Here is the error:

  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)
4

1 回答 1

0

根据您的 logcat 输出,您在这行(非常复杂的)代码中得到了NullpointerException :

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";
}

这就是您的应用在启动此 Activity 时崩溃的原因。崩溃与 TextSwitcher 无关。

因此,请检查您的任何变量是否为空。例如:

在哪里做

QAs.strQs[this.m_iCurrIndex]

来自?

更新:

您的问题是您的静态类的构造函数从未被调用,因此您的数组为空。 我建议您使用初始化方法来初始化数组:

public static void initialize() {
    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"; 
}

然后,在 Activity 中调用 getData() 之前(例如在 onCreate() 中),调用:

QAs.initialize();
于 2013-09-07T12:35:14.013 回答