2014-11-11 5 views
-2

내 응용 프로그램에서 재생할 노래의 장르에 따라 레이아웃 배경을 프로그래밍 방식으로 설정하려고합니다. 나는이 시도 :레이아웃을 프로그래밍 방식으로 설정하는 방법

public class AnswerActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     Log.e("TRANSITION", "TRANSITIONED TO ANSWER ACTIVITY"); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.play); 
     android.support.v7.app.ActionBar actionBar = getSupportActionBar(); 
     actionBar.hide(); 
     LinearLayout root = (LinearLayout) findViewById(R.id.ctr1); 
     Bundle data = getIntent().getExtras(); 
     if(data.getString("genre").equals("rock")){ 
      root.setBackgroundResource(R.drawable.wprock); 
     } 
     else if(data.getString("genre").equals("pop")){ 
      root.setBackgroundResource(R.drawable.wppop); 
     } 
     else if(data.getString("genre").equals("hiphop")){ 
      root.setBackgroundResource(R.drawable.wphiphop); 
     } 

    } 

을하지만 이들 중 누군가가 일어날 때마다 자신의 root.setBackgroundResource 라인에 널 포인터 예외를 던지고, 작동하지 않습니다. 아이디어가 있으십니까? 고맙습니다 미리 편집 : 나는 R.drawable.wprock/pop/hiphop을 가지고 있으며, 그 대신에 setBackgroundColor 메도우와 함께 색상을 사용하려고 시도하고 그와 동일한 가능성을 배제했다. XML

: 당신이 루트에되지 않은 새로운 layout을하기 때문에

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#1d1d1d" > 

    <ImageView 
     android:id="@+id/res" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center" 

     android:layout_marginTop="130dp" 
     android:onClick="go" 
     android:background="@drawable/playbtn" /> 

</LinearLayout> 
+0

xml 코드도 –

+0

[Android set background drawable 프로그래밍 방식으로 가능] http://stackoverflow.com/questions/12523005/android-set-background-drawable-programmatically) –

+0

게시물 xml ------------------ –

답변

0

당신은 오류가 발생했습니다 당신의 Activity :

LinearLayout root = new LinearLayout(this); 

위의 코드를 제거하고 외부에 android:id을 제공 레이아웃을 R.layout.play에 입력하고 대신 findViewbyId을 사용하십시오. 그 다음 코드를

protected void onCreate(Bundle savedInstanceState) { 
     Log.e("TRANSITION", "TRANSITIONED TO ANSWER ACTIVITY"); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.play); 
     android.support.v7.app.ActionBar actionBar = getSupportActionBar(); 
     actionBar.hide(); 
     LinearLayout root = (LinearLayout) findViewById(R.id.ctr1); 
     Bundle data = getIntent().getExtras(); 
     if(data.getString("genre").equals("rock")){ 
      root.setBackgroundResource(R.drawable.wprock); 
     } 
     else if(data.getString("genre").equals("pop")){ 
      root.setBackgroundResource(R.drawable.wppop); 
     } 
     else if(data.getString("genre").equals("hiphop")){ 
      root.setBackgroundResource(R.drawable.wphiphop); 
     } 

를 따라 올 경우

+0

어쨌든 시도해 보니 작동하지 않았다. 최종 코드 – OrangeWall

+0

@user1832697로 보내 주시면 XML을 게시 해주세요. :) –

+0

@BlazeTama 정답을 게시하기 전에 신중하게 질문을 읽어보십시오. ..... –

0

먼저 당신은 번들이 null의 경우, 또는 다른 어떤 것을

귀하의 XML

에 다음 Bundle data = getIntent().getExtras(); 데이터를 확인하여 XML의 루트 레이아웃 ID를 확인
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ctr1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:background="#1d1d1d" > 

<ImageView 
    android:id="@+id/res" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:gravity="center" 

    android:layout_marginTop="130dp" 
    android:onClick="go" 
    android:background="@drawable/playbtn" /></LinearLayout> 
관련 문제