我想展示一些片段。我有一些学生的sqlite数据库,(字段是:_id,Name,Lastname,sex,photoSize,field1,field2,field3,field4)。
如果,例如field1 == 1我必须显示片段1,如果field2 == 2我必须显示片段2,依此类推。
我想选择我要展示的片段。我不知道我做错了什么。这是代码。
清单
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<activity
android:name=".ComunicadorGrillaActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AlumnoActivity" />
<activity
android:name=".ImagenesAlumnoActivity"
android:label="@string/title_activity_imagenes_alumno"
android:theme="@style/AppTheme.NoActionBar"></activity>
<activity android:name=".AltaAlumnoActivity" />
<activity
android:name=".ModoAlumnoActivity"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity android:name=".AjusteActivity" />
</application>
public class SectionsPagerAdapter extends FragmentPagerAdapter {
Bundle b = getIntent().getExtras();
int i = 5;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
String id = b.getString("id");
c1 = dbm.buscarAlumno(id);
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
try {
if (c1.moveToFirst()){
switch (position){
case 0:
if (c1.getString(5).equals("1")){
return PlaceholderFragment.newInstance(position);
}
case 1:
if (c1.getString(6).equals("1")){
return PlaceholderFragment.newInstance(position);
}
case 2:
if (c1.getString(7).equals("1")){
return PlaceholderFragment.newInstance(position);
}
case 3:
if (c1.getString(8).equals("1")){
return PlaceholderFragment.newInstance(position);
}
case 4:
return PlaceholderFragment.newInstance(position);
}
}
}
finally {
c1.close();
}
return null;
}
@Override
public int getCount() {
// Show 1 total pages.
return i;
}
@Override
public CharSequence getPageTitle(int position) {
String id = b.getString("id");
c = dbm.buscarAlumno(id);
try {
if (c.moveToFirst()) {
switch (position) {
case 0:
if (c.getString(5).equals("1")){
return "PISTA";
}
case 1:
if (c.getString(6).equals("1")){
return "ESTABLO";
}else{break;}
case 2:
if (c.getString(7).equals("1")){
return "EMOCIONES";
}else{break;}
case 3:
if (c.getString(8).equals("1")){
return "NECESIDADES";
}else{break;}
case 4:
return b.getString("alumno");
}
}
}
finally {
c.close();
}
return null;
}
}
getItem()不起作用,但getPageTitle工作正常。 dbm变量在容器类中声明,表示DataBaseManager,而buscarAlumno(id)是返回学生的方法,并且发誓该方法工作正常。这是logcat错误:
12-30 11:48:00.168 1847-1847/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: laboratorio.hermes2, PID: 1847
java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.IllegalStateException: Unable to get package info for laboratorio.hermes2; is package not installed?
at android.app.LoadedApk.makeApplication(LoadedApk.java:507)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4301)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Unable to get package info for laboratorio.hermes2; is package not installed?
at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:368)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:321)
at android.app.LoadedApk.makeApplication(LoadedApk.java:500)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4301)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
12-30 11:48:00.178 1389-1389/? W/EGL_emulation: eglSurfaceAttrib not implemented
12-30 11:48:00.178 1253-1264/? W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 1783 uid 10057
12-30 11:48:00.198 1356-1367/? W/Binder: Caught a RuntimeException from the binder stub implementation.
java.lang.NullPointerException
at android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280)
at com.android.internal.view.IInputMethod$Stub.onTransact(IInputMethod.java:129)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(Native Method)
12-30 11:48:06.238 1389-1389/? E/ActivityThread: Performing stop of activity that is not resumed: {com.android.launcher/com.android.launcher2.Launcher}
java.lang.RuntimeException: Performing stop of activity that is not resumed: {com.android.launcher/com.android.launcher2.Launcher}
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3133)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3220)
at android.app.ActivityThread.access$1100(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
12-30 11:55:16.425 1356-1368/? W/Binder: Caught a RuntimeException from the binder stub implementation.
java.lang.NullPointerException
at android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280)
at com.android.internal.view.IInputMethod$Stub.onTransact(IInputMethod.java:129)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(Native Method)
请帮忙!感谢。