내 응용 프로그램에서 재생할 노래의 장르에 따라 레이아웃 배경을 프로그래밍 방식으로 설정하려고합니다. 나는이 시도 :레이아웃을 프로그래밍 방식으로 설정하는 방법
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>
xml 코드도 –
[Android set background drawable 프로그래밍 방식으로 가능] http://stackoverflow.com/questions/12523005/android-set-background-drawable-programmatically) –
게시물 xml ------------------ –