2011-09-13 4 views
1

나는 타이머를 기반으로 각 애플 리케이션 배경을 바꾸기 위해 노력하고있다. 나는 내가 할 수있는 일을 해왔고, 자바와 프로그래밍을 초심자로 생각하고있다.) 만약 누군가가 내 코드를 정정 할 수 있다면 좋아할 것입니다.) (타이머 등을 사용하면 전화를 걸기도하지만 일식이 잘못 표시되지만 앱이 강제 종료됩니다.) 여기에 있습니다 :동적 배경 (LinearLayout에서) - 내 오류 란 무엇입니까?

public class CookBookActivity extends Activity { 
    /** Called when the activity is first created. */ 

    private static final long GET_DATA_INTERVAL = 10000; 
    int images[] = {R.drawable.smothie1,R.drawable.omletherb1}; 
    int index = 0; 
    ImageView img; 
    Handler hand = new Handler(); 
    private LinearLayout layout; 

    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.main); 
     layout = (LinearLayout)findViewById(R.id.linearLayout1); 
     hand.postDelayed(run, GET_DATA_INTERVAL); 
    } 

    Runnable run = new Runnable() { 
     public void run() { 
      layout.setBackgroundDrawable(getDrawable(images[index++])); 
      if (index == images.length) 
       index = 0; 
      hand.postDelayed(run, GET_DATA_INTERVAL); 


     Typeface tf2 = Typeface.createFromAsset(getAssets(), 
       "fonts/BPreplay.otf"); 
     TextView tv2 = (TextView) findViewById(R.id.textView2); 
     tv2.setTypeface(tf2); 


     Typeface tf = Typeface.createFromAsset(getAssets(), 
       "fonts/BPreplay.otf"); 
     TextView tv = (TextView) findViewById(R.id.textView1); 
     tv.setTypeface(tf); 


     Button mainNext = (Button) findViewById(R.id.nextScreen1); 
     mainNext.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       Intent i = new Intent(); 
       i.setClassName("com.unKnown.cookbook", "com.unKnown.cookbook.screen1"); 
       startActivity(i); 

      } 
     }); 
     } 
    }; 

    protected Drawable getDrawable(int i) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 

편집 : 이제

나는 finnaly 지금 내 그것은 단지의 지금, 그것의 거의 미세 (쿠마의 도움과 나를 :) @Yashwanth, 감사) 내 문제와 내 이미지의 배경 세트를 해결하지만, 한 ETS (이 같은 이미지를 설정, 각 십초)을 배경으로 하나 개의 이미지, 나는 그것이 아래로 다음 중 두 가지 중 하나에 대한 생각 :

중 하나

-handler이 (내가 의심 whcich) 중지 - 내가 가지고있는 이 경우 나는 ',

단지 목록 (R.drawable.omletherb1)에서 첫 번째 이미지를 사용하여 지금은 작동하고 모든 그것이 절차를 수행 swecond, 그래서 두 번째 문제

또는 아래의 확인 R.Drawable.zzz가 설정된 다음 이미지를 설정하는 것과 같이 설정해야합니다. R.drawable.ccc

은 어떻게 생각하는지 말해, 여기에 지금과 만난다 tyhe 코드하십시오 :

공용 클래스 CookBookActivity 활동을 {확장 /** 활동이 처음 생성 될 때 호출됩니다. */

private static final long GET_DATA_INTERVAL = 1000; 
int images[] = {R.drawable.omletherb1,R.drawable.smothie1}; 
int index = 0; 
LinearLayout img; 
Handler hand = new Handler(); 
private LinearLayout layout; 

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.main); 
    layout = (LinearLayout)findViewById(R.layout.main); 
    hand.postDelayed(run, GET_DATA_INTERVAL); 

    Typeface tf2 = Typeface.createFromAsset(getAssets(), 
      "fonts/BPreplay.otf"); 
    TextView tv2 = (TextView) findViewById(R.id.textView2); 
    tv2.setTypeface(tf2); 


    Typeface tf = Typeface.createFromAsset(getAssets(), 
      "fonts/BPreplay.otf"); 
    TextView tv = (TextView) findViewById(R.id.textView1); 
    tv.setTypeface(tf); 


    Button mainNext = (Button) findViewById(R.id.nextScreen1); 
    mainNext.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Intent i = new Intent(); 
      i.setClassName("com.unKnown.cookbook", "com.unKnown.cookbook.screen1"); 
      startActivity(i); 

     } 
    }); 
} 

Runnable run = new Runnable() { 
    public void run() { 
     layout.setBackgroundDrawable(getDrawable(images[index++])); 
     if (index == images.length) 
      index = 0; 
     hand.postDelayed(run, GET_DATA_INTERVAL); 

    } 
}; 

protected Drawable getDrawable(int i) { 
    // TODO Auto-generated method stub 
    return getResources().getDrawable(images[i%2]); 
} 

}

답변

2
layout.setBackgroundDrawable(getDrawable(images[index++])); 

protected Drawable getDrawable(int i) { 
    // TODO Auto-generated method stub 
    return null; 
} 

이것은 당신은 background.return 일부 유효한 당김을 널 (null)로 설정하고 그것을 작동 문제입니다.

+0

wow 무엇 빠른 답변 :) 그래서 뭔가 : proctected Drawable getDrawable (이미지, 이미지) 또는 다른 무엇입니까 – Bercik

+0

또는 그 drawable/imgage 또는 뭔가 반환 무엇입니까? – Bercik

+0

return getResources(). getDrawable (images [i % 2]); –

관련 문제