下面这个程序是实现一个activity调用另一个activity,程序编译无错,运行时报错:The application ex12(process ex12)
has stop unexpectedly.Please try again
我后来把wipe user data选中,或者关了eclipse重开,依然是报这个错,其它程序是好的,所以也不是配置问题,帮忙找找错误吧,另外我想问下,这个android的错误提示怎么这么模糊,就一个stop unexpectedly,没有具体的错误提示吗?还是我没找到?
程序如下:
- Java code
ex12.javapackage com.misoo.ex12;import android.app.Activity;import android.content.Intent;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.LinearLayout;public class ex12 extends Activity { private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT; private LinearLayout layout; private LinearLayout.LayoutParams para; static final int RG_REQUEST = 0; private int mColor = Color.YELLOW; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); para = new LinearLayout.LayoutParams(230, 140); DrawView dv = new DrawView(this); layout.addView(dv, para); Button btn = new Button(this); btn.setText("Change Color"); btn.setOnClickListener(listener); para = new LinearLayout.LayoutParams(WC, WC); layout.addView(btn, para); setContentView(layout); } public int getColor() { return mColor; } private OnClickListener listener = new OnClickListener() { public void onClick(View v) { Intent in = new Intent(ex12.this, rgActivity.class); startActivityForResult(in,RG_REQUEST); } }; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == RG_REQUEST) { if (resultCode == RESULT_CANCELED) setTitle("Canceled..."); else if(resultCode == RESULT_OK) { String data_str = (String)data.getCharSequenceExtra("DataKey"); setTitle(data_str); if(data_str.contains("Y")) mColor = Color.YELLOW; else mColor = Color.BLUE; } } }}DrawView.javapackage com.misoo.ex12;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.view.View;public class DrawView extends View { private Paint pa; private Context ctx; public DrawView(Context context) { super(context); ctx = context; pa = new Paint(); } @Override protected void onDraw(Canvas canvas) { ex12 obj = (ex12)ctx; pa.setColor(obj.getColor()); canvas.drawRect(10, 10, 100, 100, pa); pa.setColor(Color.GREEN); pa.setStrokeWidth(4); pa.setStrokeCap(Paint.Cap.ROUND); canvas.drawLine(10, 55, 100, 55, pa); canvas.drawLine(55, 10, 55, 100, pa); pa.setColor(Color.RED); canvas.drawRect(50, 50, 60, 60, pa); } }rgActivity.javapackage com.misoo.ex12;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;public class rgActivity extends Activity implements OnCheckedChangeListener{ private final int WC = RadioGroup.LayoutParams.WRAP_CONTENT; private RadioGroup rg_layout; RadioGroup.LayoutParams params; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); rg_layout = new RadioGroup(this); params = new RadioGroup.LayoutParams(WC, WC); rg_layout.setOrientation(RadioGroup.VERTICAL); rg_layout.setLayoutParams(params); rg_layout.setOnCheckedChangeListener(this); RadioButton button1 = new RadioButton(this); button1.setText("Yellow"); button1.setId(1001); params = new RadioGroup.LayoutParams(WC, WC); rg_layout.addView(button1,params); RadioButton button2 = new RadioButton(this); button2.setText("Blue"); button2.setId(1002); params = new RadioGroup.LayoutParams(WC, WC); rg_layout.addView(button2,params); setContentView(rg_layout); } public void onCheckedChanged(RadioGroup arg0, int arg1) { String cc; if(arg0.getCheckedRadioButtonId() == 1001) cc = "Y"; else cc = "B"; Bundle bundle = new Bundle(); bundle.putString("DataKey", cc); Intent mIntent = new Intent(); mIntent.putExtras(bundle); setResult(RESULT_OK, mIntent); finish(); }}