我試圖加載一個背景到的LinearLayout當的NullPointerException錯誤可繪製背景:機器人:錯誤裝載上一個的LinearLayout
的LinearLayout fondoSpinner =(的LinearLayout)findViewById(R.id.fondoSpinner); fondoSpinner.setBackgroundResource(R.drawable.spinner_morado_gradiente);
LinearLayout屬於自定義微調器。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fondoSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner_naranja_gradiente"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imagenSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textoSpinner"
style="@style/SpinnerText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="left"/>
此代碼還收你的背景和它的作品確定。當我嘗試將它加載到主文件時發生錯誤。我必須做改變顏色動態
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.barra_titulo);
cargarCombo();
LinearLayout fondoSpinner = (LinearLayout)findViewById(R.id.fondoSpinner);
fondoSpinner.setBackgroundResource(R.drawable.spinner_morado_gradiente);
}
private void cargarCombo() {
Spinner spinner = (Spinner) findViewById(R.id.comboTipo_Vistos);
listaSpinner = new ArrayList<CountryInfo>();
listaSpinner = cargarComboVistos();
CountryAdapter myAdapter = new CountryAdapter
(this, android.R.layout.simple_spinner_item, listaSpinner);
spinner.setAdapter(myAdapter);
}
private ArrayList<CountryInfo> cargarComboVistos() {
ArrayList<CountryInfo> lista = new ArrayList<CountryInfo>();
int imagen = R.drawable.mapanaranja;
lista.add(new CountryInfo("Todos", imagen));
lista.add(new CountryInfo("Sí", imagen));
lista.add(new CountryInfo("No", imagen));
return lista;
}
public class CountryAdapter extends ArrayAdapter<CountryInfo>
{
private Activity context;
ArrayList<CountryInfo> data = null;
public CountryAdapter(Activity context, int resource, ArrayList<CountryInfo> data)
{
super(context, resource, data);
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{ // Ordinary view in Spinner, we use android.R.layout.simple_spinner_item
return super.getView(position, convertView, parent);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{ // This view starts when we click the spinner.
View row = convertView;
if(row == null)
{
LayoutInflater inflater = context.getLayoutInflater();
row = inflater.inflate(R.layout.spinner_row, parent, false);
}
CountryInfo item = data.get(position);
if(item != null)
{ // Parse the data from each object and set it.
ImageView imagenSpinner = (ImageView) row.findViewById(R.id.imagenSpinner);
TextView textoSpinner = (TextView) row.findViewById(R.id.textoSpinner);
if(imagenSpinner != null)
{
imagenSpinner.setBackgroundResource(item.getCountryFlag());
}
if(textoSpinner != null)
textoSpinner.setText(item.getCountryName());
}
return row;
}
}
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#9933CC" />
<stroke
android:width="3dp"
android:color="#AA66CC" />
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270"
android:type="linear"
/>
<stroke
android:width="1dp"
android:color="#9933CC" />
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" />
</shape>
</item>
</selector>
什麼是錯誤? – laalto
NullpointerException in line fondoSpinner.setBackgroundResource(R.drawable.spinner_morado_gradiente) – user3092292
因此,您的活動視圖層次結構不包含具有此類標識的視圖。你在哪裏實例化/引用包含fondoSpinner佈局的佈局? – laalto