正在创建一个我正在使用框架布局的应用程序.....我已经使用了三个类 1)活动 2)选择图像 3)在图像上绘图
所以我的活动课如下
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
i= new FirstImage(this);
j=(Draw)findViewById(R.id.info);
t=(TextView)findViewById(R.id.textView);
k=(TextView)findViewById(R.id.textView1);
back=(ImageButton)findViewById(R.drawable.monotone_arrow_next_left);
next=(ImageButton)findViewById(R.drawable.monotone_arrow_next_lrightcopy);
if (count==0){
((FirstImage) i).changeImage(R.drawable.human);
}
addListenerOnButton();
}
private void addListenerOnButton() {
// TODO Auto-generated method stub
next.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
if(count==0){
//i.startAnimation(animationFadeout);
((FirstImage) i).changeImage(R.drawable.hand);
back.setEnabled(true);
count++;
addListenerOnButton();
}
else if (count==1){
//i.startAnimation(animationFadeout);
((FirstImage) i).changeImage(R.drawable.tissue);
count++;
addListenerOnButton();
}
else if (count==2){
//i.startAnimation(animationFadeout);
((FirstImage) i).changeImage(R.drawable.cell);
count++;
addListenerOnButton();
}
else if (count==3){
//nucleus
//i.startAnimation(animationFadeout);
((FirstImage) i).changeImage(R.drawable.nucleus);
count++;
addListenerOnButton();
}
else if (count==4){
//nchromosome
//i.startAnimation(animationFadeout);
((FirstImage) i).changeImage(R.drawable.chromosome);
count++;
addListenerOnButton();
}
else
{ //double helix
//i.startAnimation(animationFadeout);
count++;
((FirstImage) i).changeImage(R.drawable.dnahelix);
next.setEnabled(false);
addListenerOnButton();
}
}
});
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(count==1){
//i.startAnimation(animationFadeout);
((FirstImage) i).changeImage(R.drawable.human);
back.setEnabled(false);
count--;
addListenerOnButton();
}
else if (count==2){
//i.startAnimation(animationFadeout);
((FirstImage) i).changeImage(R.drawable.hand);
count--;
addListenerOnButton();
}
else if (count==3){
//i.startAnimation(animationFadeout);
((FirstImage) i).changeImage(R.drawable.tissue);
count--;
addListenerOnButton();
}
else if (count==4){
//i.startAnimation(animationFadeout);
((FirstImage) i).changeImage(R.drawable.cell);
count--;
addListenerOnButton();
}
else if (count==5){
//nucleus
//i.startAnimation(animationFadeout);
((FirstImage) i).changeImage(R.drawable.nucleus);
count--;
addListenerOnButton();
}
else {
//chromosome
//count==6
//i.startAnimation(animationFadeout);
((FirstImage) i).changeImage(R.drawable.chromosome);
count--;
addListenerOnButton();
next.setEnabled(true);
}
}
});
}
}
我的班级正在选择图像,如下所示..
public class FirstImage extends ImageView implements OnTouchListener {
ImageView i;
MotionEvent event;
int x;
Bitmap image;
String huma ="human";
String info = "";
float y = 0; //init value
float z = 0; //init value
Animation animationFadeIn;
Draw child;
public FirstImage(Context context) {
super(context);
child=new Draw (context);
i= new ImageView (context);
}
public FirstImage(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void changeImage(int id){
final Animation animationFadeout=AnimationUtils.loadAnimation(getContext(), R.anim.zoomout);
i.setImageResource(id);
i.startAnimation(animationFadeout);
i.setOnTouchListener(this);
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
}
我的课程将在图像上绘制如下
public class Draw extends View {
String info = "";
float x = 0;
float y = 0;
int color = Color.GREEN;
public Draw(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public Draw(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setColor(color);
paint.setStrokeWidth(2);
paint.setTextSize(30);
canvas.drawLine(x-10, y, x+10, y, paint);
canvas.drawLine(x, y-10, x, y+10, paint);
canvas.drawText(info, x, y, paint);
}
public void updateInfo(String t_info, float t_x, float t_y){
info = t_info;
x = t_x;
y = t_y;
invalidate();
}
public void clearInfo(){
info = "";
x = 0;
y = 0;
invalidate();
}
}
它显示应用程序意外关闭...但在 logcat 显示
05-21 15:55:18.964: E/AndroidRuntime(850): FATAL EXCEPTION: main
05-21 15:55:18.964: E/AndroidRuntime(850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nam/com.example.nam.AshActivity}: java.lang.NullPointerException
05-21 15:55:18.964: E/AndroidRuntime(850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-21 15:55:18.964: E/AndroidRuntime(850): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-21 15:55:18.964: E/AndroidRuntime(850): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-21 15:55:18.964: E/AndroidRuntime(850): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-21 15:55:18.964: E/AndroidRuntime(850): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 15:55:18.964: E/AndroidRuntime(850): at android.os.Looper.loop(Looper.java:123)
05-21 15:55:18.964: E/AndroidRuntime(850): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-21 15:55:18.964: E/AndroidRuntime(850): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 15:55:18.964: E/AndroidRuntime(850): at java.lang.reflect.Method.invoke(Method.java:507)
05-21 15:55:18.964: E/AndroidRuntime(850): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-21 15:55:18.964: E/AndroidRuntime(850): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-21 15:55:18.964: E/AndroidRuntime(850): at dalvik.system.NativeStart.main(Native Method)
05-21 15:55:18.964: E/AndroidRuntime(850): Caused by: java.lang.NullPointerException
05-21 15:55:18.964: E/AndroidRuntime(850): at com.example.nam.AshActivity.addListenerOnButton(AshActivity.java:48)
05-21 15:55:18.964: E/AndroidRuntime(850): at com.example.nam.AshActivity.onCreate(AshActivity.java:40)
05-21 15:55:18.964: E/AndroidRuntime(850): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-21 15:55:18.964: E/AndroidRuntime(850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-21 15:55:18.964: E/AndroidRuntime(850): ... 11 more
我的xml文件如下....
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- Screen Design for VIDEOS -->
<TableLayout
android:id="@+id/tablelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dip"
android:text="pic on click which will tell where is the dna located in human body or cell "
android:textSize="18dip" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.example.nam.FirstImage
android:id="@+id/image"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<com.example.nam.Draw
android:id="@+id/info"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</FrameLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="@drawable/monotone_arrow_next_left"/>
<ImageView
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="@drawable/monotone_arrow_next_lrightcopy"/>
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dip"
android:text="please don't mind "
android:textSize="18dip" />
</TableRow>
</TableLayout>
</ScrollView>