当前位置: 代码迷 >> Android >> 从一个Activity跳转到另一个Activity,发生FATAL EXCEPTION:main异常求解决
  详细解决方案

从一个Activity跳转到另一个Activity,发生FATAL EXCEPTION:main异常求解决

热度:59   发布时间:2016-05-01 21:43:29.0
从一个Activity跳转到另一个Activity,发生FATAL EXCEPTION:main错误求解决!
原Activity代码有点长……我只把监听器复过来:
 @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Intent intent = new Intent();
intent.putExtra("choose", position);
intent.setClass(IntroductionActivity.this, detailActivity.class);
IntroductionActivity.this.startActivity(intent);
System.out.println("positon---->"+position);
}

运行程序的时候发现logcat中有输出position,所以应该不是这个activity的问题了吧?

另一个activity源代码如下:

package mars.product;

import java.io.*;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;


public class detailActivity extends ListActivity {
   
private TextView detailview = null;
private Button backbutton = null;
private Button linkbutton = null;
private int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState){
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);

Intent intent = getIntent();
//注意getIntExtra的返回值是int,后面参数int defaultvalue 干嘛的不知道……
i = intent.getIntExtra("choose", 0);  
System.out.println("i------>"+i);

detailview = (TextView)findViewById(R.id.detailview);

//读入txt资源,创建输入流
InputStream is = getResources().openRawResource(R.raw.introduction);
//创建字符创存放缓存
byte[] buffer = new byte[1024];
int rd = 0;
//新建输出流
ByteArrayOutputStream output = new ByteArrayOutputStream();
//读取文本文件到缓存中
try{
while((rd = is.read(buffer)) != -1){
output.write(buffer, 0, rd);
}
//简体中文编码GBK,繁体big5?
String temp = new String(output.toByteArray(),"GBK");

//将获得字符串拆开,截取各段内容赋给string[]数组---trim去掉两边空白字符后的字符串---split分割函数
String[] detail = temp.trim().split("#");

detailview.setText(detail[i]);

//关闭输出流
output.close();
} catch (IOException e){
e.printStackTrace();
}



backbutton = (Button)findViewById(R.id.backbutton);
backbutton.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}

});

linkbutton = (Button)findViewById(R.id.linkbutton);
linkbutton.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(detailActivity.this, linkActivity.class);
intent.putExtra("i", i);//直接传输网址的数组位置,不用+1
detailActivity.this.startActivity(intent);

}

});

}
   
}

这个activity的布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="10dp"
  android:orientation="vertical" >
   
  <TextView
  android:id="@+id/detailview"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:padding="5dp"
  />
   
  相关解决方案