自定义操作栏上的微调器出现问题... 应用程序在启动时崩溃了“ spnJobList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()... ”,我不明白错误......
这是Logcat:
03-28 21:07:06.594 17934-17934/com.robertot.timereport E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.robertot.timereport, PID: 17934
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.robertot.timereport/com.robertot.timereport.com.robertot.timereport.pages.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
at android.app.ActivityThread.access$800(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5081)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.robertot.timereport.com.robertot.timereport.pages.SummaryFragment.onActivityCreated(SummaryFragment.java:53)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5241)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
at android.app.ActivityThread.access$800(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5081)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
这是MainActivity:
public class MainActivity extends FragmentActivity
{
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private String[] mFragmentTitles;
private CharSequence mTitle;
private ActionBar actionBar;
private Spinner spnJobList;
private int count_job = 0, FragmentTag;
private ArrayList<String> stringListName;
private ArrayList<Integer> stringListValue;
private TextView titleFragment;
private Fragment frgm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_item);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
mTitle = getTitle();
mFragmentTitles = getResources().getStringArray(R.array.fragments);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer);
mDrawerList = (ListView)findViewById(R.id.drawer_list);
spnJobList = (Spinner)findViewById(R.id.spnJobList);
titleFragment = (TextView)findViewById(R.id.titleFragment);
stringListName = new ArrayList<String>();
stringListValue = new ArrayList<Integer>();
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mFragmentTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close){
public void onDrawerClosed(View v)
{
titleFragment.setText(getTitle());
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View v)
{
titleFragment.setText("");
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null){
selectItem(0);
}
PopulateSpinner();
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case android.R.id.home:
if (mDrawerLayout.isDrawerOpen(mDrawerList)){
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
return true;
case R.id.action_settings:
startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
return true;
case R.id.AddWorkingDay:
startActivity(new Intent(getApplicationContext(), AddWorkingDayActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private class DrawerItemClickListener implements ListView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
selectItem(position);
}
}
private void selectItem(int position){
frgm = new SummaryFragment();
FragmentTag = 0;
FragmentTransaction fm = getSupportFragmentManager().beginTransaction();
switch(position)
{
case 0:
FragmentTag = 0;
frgm = new SummaryFragment();
break;
case 1:
FragmentTag = 1;
frgm = new NewJobFragment();
break;
case 2:
FragmentTag = 2;
frgm = new StatisticsFragment();
break;
}
fm.replace(R.id.content_frame, frgm, String.valueOf(FragmentTag))
.commit();
mDrawerList.setItemChecked(position, true);
setTitle(mFragmentTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title)
{
mTitle = title;
actionBar.setTitle(title);
}
@Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
private void PopulateSpinner()
{
DbHelperJob findjob = new DbHelperJob(this);
try
{
QueryBuilder<Job, String> queryBuilder = findjob.getJobDao().queryBuilder();
List<Job> results = queryBuilder.selectColumns("ID_JOB", "NAME_JOB").orderBy("DEFAULT_JOB", false).query();
for (Job a : results)
{
stringListName.add(a.getName_job());
stringListValue.add(a.getId());
count_job ++;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
if (count_job > 0)
{
ArrayAdapter<String> spnAdapt = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, stringListName);
spnAdapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnJobList.setAdapter(spnAdapt);
spnJobList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id)
{
switch (FragmentTag)
{
//Summary - Refresh ListView
case 0:
SummaryFragment sf = (SummaryFragment)getSupportFragmentManager().findFragmentByTag(String.valueOf(FragmentTag));
sf.init(String.valueOf(stringListValue.get(position)));
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0){}
});
findjob.close();
}
}
}
这是自定义操作栏布局(actionbar_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/appName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<TextView
android:id="@+id/titleFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below="@+id/appName"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Spinner
android:id="@+id/spnJobList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center"/>
</RelativeLayout>
</LinearLayout>
这是SummaryFragment()中的onActivityCreated:
private ArrayList<String> stringListName;
private ArrayList<Integer> stringListValue;
private ActionBar actionBar;
private TextView titleFragment;
// onCreate
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
// onActivityCreated
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
actionBar = getActivity().getActionBar();
stringListName = new ArrayList<String>();
stringListValue = new ArrayList<Integer>();
titleFragment.setText(actionBar.getTitle());
//PopulateSpinner();
//init();
}
谢谢!!!
答案 0 :(得分:0)
SummaryFragment代码的第53行是什么?该错误是由NullPointerException引起的。当然,你在这一行上调用的东西是空的。我敢打赌,ActionBar实例正在返回Null。在这一行上尝试这样的事情:
titleFragment.setText((actionBar != null) ? actionBar.getTitle() : "ActionBar is Null");
希望有所帮助:D