我不使用任何与线程相关的东西,但模拟器一直在说:
12-15 14:19:22.807 20983-21204/edu.fontys.stayconnected E/AbstractTracker: Can't create handler inside thread that has not called Looper.prepare()
它有办法调试更多,所以看看为什么会发生这个错误?
主要活动:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
final OptionFragmentAdapter adapter = new OptionFragmentAdapter(getSupportFragmentManager());
if (viewPager != null) {
SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tab_layout);
slidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.colorAccent);
}
});
slidingTabLayout.setDistributeEvenly(true);
viewPager.setAdapter(adapter);
slidingTabLayout.setViewPager(viewPager);
}
if (!NotificationManagerCompat.getEnabledListenerPackages(this).contains(getPackageName())) {
new AlertDialog.Builder(this)
.setTitle(getString(R.string.dialog_notification_title))
.setMessage(R.string.dialog_notification_message)
.setPositiveButton(R.string.dialog_notification_settings, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS));
}
})
.setNegativeButton(R.string.dialog_notification_close_app, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.show();
}
}
}
选项片段适配器:
public class OptionFragmentAdapter extends FragmentStatePagerAdapter {
private static final String TAG = "OptionFragmentAdapter";
private String[] titles = new String[] { "Lamps", "Apps", "Profile" };
public OptionFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Log.d(TAG, String.format("Current position: %d", position));
Fragment fragment = null;
if (position == 0) {
fragment = new LampsFragment();
} else if (position == 1) {
fragment = new AppsFragment();
} else if (position == 2) {
fragment = new ProfileFragment();
} else {
Log.w(TAG, "There is no fragment found for this position");
}
return fragment;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
@Override
public int getCount() {
return titles.length;
}
}
SlidingTabLayout 来自谷歌:https ://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabLayout.java