안드로이드 개발 질문/답변
(글 수 45,052)
로그켓 이미지사진 첨부했습니다.
package com.android.Test5;
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView;
public class Test5 extends Activity { public static final int ran[]= { R.drawable.image01, R.drawable.image02, R.drawable.image03, R.drawable.image04, R.drawable.image05 }; public static final int ran2[]= { R.drawable.image06, R.drawable.image07, R.drawable.image08, R.drawable.image09, R.drawable.image10 }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button BtnLeft = (Button)findViewById(R.id.btn_left); Button BtnRight = (Button)findViewById(R.id.btn_right); BtnLeft.setOnClickListener(ButtonOnClick); BtnRight.setOnClickListener(ButtonOnClick); } private View.OnClickListener ButtonOnClick = new View.OnClickListener(){ public void onClick(View v){ switch(v.getId()){ case R.id.btn_left: ImageView test = (ImageView)findViewById(R.id.im_left); int index = (int) (Math.random() * 5); int res = ran[index]; test.setBackgroundResource(res); break; case R.id.btn_right: ImageView test2 = (ImageView)findViewById(R.id.im_right); int index2 = (int) (Math.random() * 5); int res2 = ran2[index2]; test2.setBackgroundResource(res2); break; } } }; }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical"> <TableLayout android:id="@+id/tableLayout1" style="*" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="8.88"> <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <ImageView android:layout_height="fill_parent" android:layout_width="wrap_content" android:layout_weight="1" android:id="@+id/im_left" android:background="@drawable/image01"></ImageView> <ImageView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:id="@+id/im_right" android:background="@drawable/image02"></ImageView> </TableRow> </TableLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/relativeLayout1" android:layout_weight="8.88"> <Button android:text="Button" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="52dp" android:layout_width="wrap_content" android:id="@+id/btn_left"></Button> <TextView android:text="TextView" android:layout_height="wrap_content" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true"></TextView> <Button android:text="Button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="72dp" android:id="@+id/btn_right"></Button> </RelativeLayout> </LinearLayout>
이미지뷰 2개에다가 각자 버튼2개씩.
버튼누를때마다 이미지랜덤하게 변경.
그런데. SDK실행시키자마자 에러뜨거군요;;;ㅠㅠ
test5.java 파일 30번째 라인에서 nullpointerException이라고 친절히 로그캣에 써잇네요