我有两个活动。第一个是 ActOne,第二个是 ActTwo。在此处查看这两个活动:
在 ActOne 中,当我输入 no 并点击生成时,它会计算该 no 的阶数并将其显示在生成按钮下方的 TextView 中。所以这工作正常。现在当我点击 Generate fibo.. button 时,它将斐波那契数列存储在数组。现在我必须在下一个活动中显示这个系列,但是一旦我点击生成按钮,它就不会切换到另一个活动并停止说“不幸的是 FirstProject 已经停止”。那么我在这里做错了什么。下面我粘贴代码以及logcat:
public class ActOne extends Activity
{
Button generate,genfact;
EditText input;
TextView factans;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
generate=(Button)findViewById(R.id.generate);
genfact=(Button)findViewById(R.id.genfact);
input=(EditText)findViewById(R.id.input);
factans=(TextView)findViewById(R.id.factans);
generate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int no=new Integer(input.getText().toString()).intValue();
int fact=1;
while(no>0)
{
fact=fact*no;
no--;
}
factans.setText("The factorial is"+fact);
}
});
genfact.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int no=new Integer(input.getText().toString()).intValue();
int[] series = new int[no];
for(int i=2; i < no; i++){
series[i] = series[i-1] + series[i-2];
}
Intent intent = new Intent(ActOne.this, ActTwo.class);
Bundle b = new Bundle();
b.putIntArray("array",series);
intent.putExtras(b);
startActivity(intent);
}
});
}
}
这是 ActTwo 活动
public class ActTwo extends Activity {
TextView fiboans;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fiboans=(TextView)findViewById(R.id.fiboans);
Intent intent = getIntent();
Bundle b = intent.getExtras();
String[] arrRecd = b.getStringArray("array");
fiboans.setText(String.valueOf(arrRecd[2]));
}
}
我也想知道如何将整个数组打印到 textView 中?
这是我的错误日志:
12-19 11:28:38.912: ERROR/Trace(1133): error opening trace file: No such file or directory (2)
12-19 11:29:20.722: ERROR/AndroidRuntime(1133): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.FIrstProject/com.example.FIrstProject.ActTwo}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.FIrstProject.ActTwo.onCreate(ActTwo.java:17)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
... 11 more