마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

이미지 랜덤 띄우는 방법 ㅠㅠ

0 추천
package com.example.five;
 
import java.util.Random;
 
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
 
import com.example.five.R;
 
 
 
public class Tab01 extends Fragment {
 
 
 
Context mContext;
 
public Tab01(Context context) {
mContext = context;
}
public Tab01() {
 
 
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_tab01, null);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState){
 super.onCreate(savedInstanceState);
int[] images ={R.drawable.a1, R.drawable.a2, R.drawable.a3};
 
// Get the ImageView
 
setContentView(R.layout.activity_tab01);
ImageView mImageView = (ImageView)getActivity().findViewById(R.id.myImageView);
 
// Get a random between 0 and images.length-1
int imageId = (int)(Math.random() * images.length);
 
// Set the image
mImageView.setBackgroundResource(images[imageId]);
 
 
}
 
 
 
}
 
 
굵게 칠해놓은 이 쪽이 자꾸 불이 들어오는데요 
 
뭐가 문제인건가요 ?ㅠㅠ 예전에 레이팅바 넣을때는 저렇게 해서 됏는데 지금은 onCreatView 안에 넣어서 view.findViewById 해도 안되네여 ㅠㅠ 도와주소서 ㅠㅠ
도미노르 (1,390 포인트) 님이 2014년 10월 22일 질문

2개의 답변

0 추천
setContentView(R.layout.activity_tab01);

 

대신에

 

View view = inflater.inflate(R.layout.dialer, null);

 

를 해보시겠어요?
안드로이드로우 (15,740 포인트) 님이 2014년 10월 22일 답변
그래도 불 들어오네요  inflater,dialer 부분요

inflater 임포트 하고 dialer activity_tab01 로 바꾸면 inflate 가 불들어오네요 ㅠ.ㅠ
0 추천
 
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_tab01, null);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState){
 super.onCreate(savedInstanceState);
int[] images ={R.drawable.a1, R.drawable.a2, R.drawable.a3};
 
// Get the ImageView
 
setContentView(R.layout.activity_tab01);
ImageView mImageView = (ImageView)getActivity().findViewById(R.id.myImageView);
 
// Get a random between 0 and images.length-1
int imageId = (int)(Math.random() * images.length);
 
// Set the image
mImageView.setBackgroundResource(images[imageId]);
 
 
}
 
 
 
 
이부분을 아래와같이 바꾸세요
oncreate 함수는 다 지우시고
oncreateview 함수만 남겨두세요
 
 
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 
 
View view = inflater.inflate(R.layout.activity_tab01, null);
 
ImageView mImageView = (ImageView)view .findViewById(R.id.myImageView);
 
int[] images ={R.drawable.a1, R.drawable.a2, R.drawable.a3};
// Get a random between 0 and images.length-1
int imageId = (int)(Math.random() * images.length);
// Set the image
mImageView.setBackgroundResource(images[imageId]);
 
 
return view;
}
 

 

gun0912 (140 포인트) 님이 2014년 10월 22일 답변
...