当我尝试使用意图通过其他活动打开此活动时,应用程序将关闭。实际上我正在尝试制作类似应用程序的n测验,它会显示问题。 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)
答案 0 :(得分: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";
}
原因启动此活动时,您的应用崩溃的原因。崩溃与TextSwitcher无关。
因此,请检查您的任何变量是否为空。 例如:
在哪里
QAs.strQs[this.m_iCurrIndex]
来自?
<强>更新强>
您的问题是静态类的构造函数永远不会被调用,因此您的数组为空。 我建议您使用initialize方法初始化数组:
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();