我想出了将设置标题集成到我的自定义微调器的问题。点击任何项目后发生此错误。
08-04 02:48:39.490 16956-16956 / com.example.ilija.festivalapp_makedox E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.example.ilija.festivalapp_makedox,PID:16956 显示java.lang.NullPointerException 在com.example.ilija.festivalapp_makedox.CustomSpinner $ 1.onItemSelected(CustomSpinner.java:98) 在com.example.ilija.festivalapp_makedox.SpinnerPlus.setSelection(SpinnerPlus.java:21) 在android.widget.Spinner $ DropdownPopup $ 1.onItemClick(Spinner.java:1042) 在android.widget.AdapterView.performItemClick(AdapterView.java:299) 在android.widget.AbsListView.performItemClick(AbsListView.java:1282) 在android.widget.ListView.performItemClick(ListView.java:4450) 在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3174) 在android.widget.AbsListView $ 3.run(AbsListView.java:3925) 在android.os.Handler.handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:157) 在android.app.ActivityThread.main(ActivityThread.java:5872) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674) 在dalvik.system.NativeStart.main(本地方法)
CustomSpinner.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_spinner);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Movies");
setSupportActionBar(toolbar);
activity = this;
SpinnerPlus SpinnerExample = (SpinnerPlus) findViewById(R.id.spinner);
SpinnerPlus SpinnerExample1 = (SpinnerPlus) findViewById(R.id.spinner1);
SpinnerPlus SpinnerExample2 = (SpinnerPlus) findViewById(R.id.spinner2);
SpinnerPlus SpinnerExample3 = (SpinnerPlus) findViewById(R.id.spinner3);
SpinnerPlus SpinnerExampleTest = (SpinnerPlus) findViewById(R.id.spinnerTest);
/* Spinner SpinnerExampleTest = (Spinner) findViewById(R.id.spinnerTest);
Spinner SpinnerExampleTest1 = (Spinner) findViewById(R.id.spinner1Test);
Spinner SpinnerExampleTest2 = (Spinner) findViewById(R.id.spinner2Test);
Spinner SpinnerExampleTest3 = (Spinner) findViewById(R.id.spinner3Test);*/
output = (TextView)findViewById(R.id.output);
setListData();
setListData1();
setListData2();
setListData3();
setListDataTest();
/* setListData1Test();
setListData2Test();
setListData3Test();*/
Resources res = getResources();
adapter = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr,res);
adapter1 = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr1,res);
adapter2 = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr2,res);
adapter3 = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr3,res);
adapterTest = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArrTest,res);
/*adapter1Test = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr1Test,res);
adapter2Test = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr2Test,res);
adapter3Test = new CustomAdapter(activity,R.layout.spinner_rows,CustomListViewValuesArr3Test,res);*/
adapter.setDropDownViewResource(R.layout.spinner_rows);
SpinnerExample.setPrompt("Select your favorite Planet!");
SpinnerExample.setAdapter(
new NothingSelectedSpinnerAdapter(
adapter,
R.layout.contact_spinner_row_nothing_selected,
// R.layout.contact_spinner_nothing_selected_dropdown, // Optional
this));
SpinnerExample.setOnItemSelectedEvenIfUnchangedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// i++; //Avoid fist time oppening the OnClick start new intent. First time is for set pic of the first element in the list.
String Movie = ((TextView) view.findViewById(R.id.movieName)).getText().toString();
// if(i>=1){
Intent intent = new Intent(CustomSpinner.this, MovieTrailer.class);
if(position==0) {
intent.putExtra("Cartoons","10");
} else if(position==1) {
intent.putExtra("Cartoons","11");
} else if(position==2) {
intent.putExtra("Cartoons","12");
} else if(position==3) {
intent.putExtra("Cartoons","13");
} else if(position==4) {
intent.putExtra("Cartoons","14");
} else if(position==5) {
intent.putExtra("Cartoons","15");
} else if(position==6) {
intent.putExtra("Cartoons","16");
} else if(position==7) {
intent.putExtra("Cartoons","17");
} else if(position==8) {
intent.putExtra("Cartoons","18");
}
startActivity(intent);
//}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
SpinnerExample1.setAdapter(adapter1);
SpinnerExample1.setOnItemSelectedEvenIfUnchangedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// i++; //Avoid fist time oppening the OnClick start new intent. First time is for set pic of the first element in the list.
String Movie = ((TextView) view.findViewById(R.id.movieName)).getText().toString();
// if(i>2){
Intent intent = new Intent(CustomSpinner.this, MovieTrailer.class);
if(position==0) {
intent.putExtra("Cartoons","20");
} else if(position==1) {
intent.putExtra("Cartoons","21");
} else if(position==2) {
intent.putExtra("Cartoons","22");
} else if(position==3) {
intent.putExtra("Cartoons","23");
} else if(position==4) {
intent.putExtra("Cartoons","24");
} else if(position==5) {
intent.putExtra("Cartoons","25");
} else if(position==6) {
intent.putExtra("Cartoons","26");
} else if(position==7) {
intent.putExtra("Cartoons","27");
} else if(position==8) {
intent.putExtra("Cartoons","28");
}
startActivity(intent);
// }
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
NothingSelectedSpinnerAdapter.java
package com.example.ilija.festivalapp_makedox;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.SpinnerAdapter;
/**
* Decorator Adapter to allow a Spinner to show a 'Nothing Selected...' initially
* displayed instead of the first choice in the Adapter.
*/
public class NothingSelectedSpinnerAdapter implements SpinnerAdapter, ListAdapter {
protected static final int EXTRA = 1;
protected SpinnerAdapter adapter;
protected Context context;
protected int nothingSelectedLayout;
protected int nothingSelectedDropdownLayout;
protected LayoutInflater layoutInflater;
/**
* Use this constructor to have NO 'Select One...' item, instead use
* the standard prompt or nothing at all.
* @param spinnerAdapter wrapped Adapter.
* @param nothingSelectedLayout layout for nothing selected, perhaps
* you want text grayed out like a prompt...
* @param context
*/
public NothingSelectedSpinnerAdapter(
SpinnerAdapter spinnerAdapter,
int nothingSelectedLayout, Context context) {
this(spinnerAdapter, nothingSelectedLayout, -1, context);
}
/**
* Use this constructor to Define your 'Select One...' layout as the first
* row in the returned choices.
* If you do this, you probably don't want a prompt on your spinner or it'll
* have two 'Select' rows.
* @param spinnerAdapter wrapped Adapter. Should probably return false for isEnabled(0)
* @param nothingSelectedLayout layout for nothing selected, perhaps you want
* text grayed out like a prompt...
* @param nothingSelectedDropdownLayout layout for your 'Select an Item...' in
* the dropdown.
* @param context
*/
public NothingSelectedSpinnerAdapter(SpinnerAdapter spinnerAdapter,
int nothingSelectedLayout, int nothingSelectedDropdownLayout, Context context) {
this.adapter = spinnerAdapter;
this.context = context;
this.nothingSelectedLayout = nothingSelectedLayout;
this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout;
layoutInflater = LayoutInflater.from(context);
}
@Override
public final View getView(int position, View convertView, ViewGroup parent) {
// This provides the View for the Selected Item in the Spinner, not
// the dropdown (unless dropdownView is not set).
if (position == 0) {
return getNothingSelectedView(parent);
}
return adapter.getView(position - EXTRA, null, parent); // Could re-use
// the convertView if possible.
}
/**
* View to show in Spinner with Nothing Selected
* Override this to do something dynamic... e.g. "37 Options Found"
* @param parent
* @return
*/
protected View getNothingSelectedView(ViewGroup parent) {
return layoutInflater.inflate(nothingSelectedLayout, parent, false);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// Android BUG! http://code.google.com/p/android/issues/detail?id=17128 -
// Spinner does not support multiple view types
if (position == 0) {
return nothingSelectedDropdownLayout == -1 ?
new View(context) :
getNothingSelectedDropdownView(parent);
}
// Could re-use the convertView if possible, use setTag...
return adapter.getDropDownView(position - EXTRA, null, parent);
}
/**
* Override this to do something dynamic... For example, "Pick your favorite
* of these 37".
* @param parent
* @return
*/
protected View getNothingSelectedDropdownView(ViewGroup parent) {
return layoutInflater.inflate(nothingSelectedDropdownLayout, parent, false);
}
@Override
public int getCount() {
int count = adapter.getCount();
return count == 0 ? 0 : count + EXTRA;
}
@Override
public Object getItem(int position) {
return position == 0 ? null : adapter.getItem(position - EXTRA);
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public long getItemId(int position) {
return position >= EXTRA ? adapter.getItemId(position - EXTRA) : position - EXTRA;
}
@Override
public boolean hasStableIds() {
return adapter.hasStableIds();
}
@Override
public boolean isEmpty() {
return adapter.isEmpty();
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
adapter.registerDataSetObserver(observer);
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
adapter.unregisterDataSetObserver(observer);
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return position != 0; // Don't allow the 'nothing selected'
// item to be picked.
}
}
SpinnerPlus.java:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.AdapterView;
import android.widget.Spinner;
import java.lang.reflect.Field;
public class SpinnerPlus extends Spinner {
AdapterView.OnItemSelectedListener listener;
public SpinnerPlus(Context context, AttributeSet attrs) {
super(context,attrs);
}
@Override
public void setSelection(int position) {
super.setSelection(position);
if (listener != null)
listener.onItemSelected(this, getSelectedView(), position, 0);
}
public void setOnItemSelectedEvenIfUnchangedListener(
AdapterView.OnItemSelectedListener listener) {
this.listener = listener;
}
}
CustomAdapter.java
package com.example.ilija.festivalapp_makedox;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by ilija on 15-Jul-16.
*/
public class CustomAdapter extends ArrayAdapter<String>{
private Activity activity;
private ArrayList data;
public Resources res;
SpinnerModel tempValues=null;
LayoutInflater inflater;
public CustomAdapter(
CustomSpinner activitySpinner,
int textViewResourceId,
ArrayList objects,
Resources resLocal
) {
super(activitySpinner, textViewResourceId, objects);
activity = activitySpinner;
data = objects;
res=resLocal;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getDropDownView(int position, View convertView, ViewGroup parent){
return getCustomView(position,convertView,parent);
}
public View getView(int position, View convertView, ViewGroup parent){
return getCustomView(position,convertView,parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent){
View row = inflater.inflate(R.layout.spinner_rows, parent, false);
tempValues = null;
tempValues = (SpinnerModel) data.get(position);
TextView label = (TextView)row.findViewById(R.id.movieName);
ImageView image = (ImageView)row.findViewById(R.id.imageSpinner);
RelativeLayout backgorund = (RelativeLayout)row.findViewById(R.id.spinnerBackground);
label.setText(tempValues.getMovieName());
image.setImageResource(res.getIdentifier("com.example.ilija.festivalapp_makedox:drawable/"+tempValues.getImage(),null,null));
//backgorund.setBackgroundColor(tempValues.getBackground());
return row;
}
}
activity_custom_spinner.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:agendaCalendar="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainScreen">
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!--tools:context=".MainScreen"-->
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainScreen">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="55dp"
android:id="@+id/imageView2"
android:layout_alignParentTop="true"
android:background="@drawable/makecover"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<ScrollView
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="155dp"
android:layout_marginBottom="10dp"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="11dp"
android:layout_marginRight="11dp"
android:orientation="vertical"
>
<TextView
android:id="@+id/ico"
android:text="SELECTIONS:"
android:paddingTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="1dp" />
<TextView
android:id="@+id/ico1"
android:text="INTERNATIONAL COMPETITION"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="1dp"
android:paddingTop="5dp"/>
<com.example.ilija.festivalapp_makedox.SpinnerPlus
android:id="@+id/spinner"
android:background="@color/main_competition"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/ico2"
android:text="NEWCOMERS COMPETITION"
android:paddingLeft="1dp"
android:paddingTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- <com.example.ilija.festivalapp_makedox.SpinnerPlus
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>-->
<com.example.ilija.festivalapp_makedox.SpinnerPlus
android:id="@+id/spinner1"
android:background="@color/newcomers"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/ico3"
android:text="FOCUS RUSSIA"
android:paddingLeft="1dp"
android:paddingTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<com.example.ilija.festivalapp_makedox.SpinnerPlus
android:id="@+id/spinner2"
android:background="@color/russian"
android:spinnerMode="dropdown"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/ico4"
android:text="SHORT DOX"
android:paddingLeft="1dp"
android:paddingTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<com.example.ilija.festivalapp_makedox.SpinnerPlus
android:id="@+id/spinner3"
android:background="@color/short1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/spinnerTesttxt"
android:text="STUDENT DOX"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="1dp"
android:paddingTop="5dp"/>
<com.example.ilija.festivalapp_makedox.SpinnerPlus
android:id="@+id/spinnerTest"
android:background="@color/student"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<!--<TextView
android:id="@+id/ico2spinnerTest"
android:text="ActionSpinnerTest:"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner1Test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/ico3spinnerTest"
android:text="HorrorspinnerTest:"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner2Test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/ico4spinnerTest"
android:text="CartoonspinnerTest:"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner3Test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>-->
</LinearLayout>
</ScrollView>
<include layout="@layout/content_custom_spinner" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
答案 0 :(得分:1)
可能((TextView) view.findViewById(R.id.movieName)).getText().toString();
为空。从ListViewValues中获取String
SpinnerExample.setOnItemSelectedEvenIfUnchangedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// i++; //Avoid fist time oppening the OnClick start new intent. First time is for set pic of the first element in the list.
String Movie = (String) CustomListViewValuesArr1.get(postion)
// if(i>=1){
Intent intent = new Intent(CustomSpinner.this, MovieTrailer.class);
if(position==0) {
intent.putExtra("Cartoons","10");
} else if(position==1) {
intent.putExtra("Cartoons","11");
} else if(position==2) {
intent.putExtra("Cartoons","12");
} else if(position==3) {
intent.putExtra("Cartoons","13");
} else if(position==4) {
intent.putExtra("Cartoons","14");
} else if(position==5) {
intent.putExtra("Cartoons","15");
} else if(position==6) {
intent.putExtra("Cartoons","16");
} else if(position==7) {
intent.putExtra("Cartoons","17");
} else if(position==8) {
intent.putExtra("Cartoons","18");
}
startActivity(intent);
//}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});