我正在尝试在多个页面上显示查询结果。当我尝试在每个页面上将查询结果显示为字符串时出现错误。
package com.example.cbtn;
public class StepsFragmentActivity extends FragmentActivity {
StepsPagerAdapter mStepsPagerAdapter;
DataBaseHelper myDataBase = new DataBaseHelper(this);
public static int pageCount;
/**
* The {@link android.support.v4.view.ViewPager} that will display the
* object collection.
*/
ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collection_demo);
// Create an adapter that when requested, will return a fragment
// representing an object in
// the collection.
//
// ViewPager and its adapters use support library fragments, so we must
// use
// getSupportFragmentManager.
mStepsPagerAdapter = new StepsPagerAdapter(getSupportFragmentManager());
// Set up action bar.
final ActionBar actionBar = getActionBar();
// Specify that the Home button should show an "Up" caret, indicating
// that touching the
// button will take the user one step up in the application's hierarchy.
actionBar.setDisplayHomeAsUpEnabled(true);
// Set up the ViewPager, attaching the adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mStepsPagerAdapter);
// Query on create to get the number of pages needed for a recipe
String[] testarg = { "5" };
Cursor results = myDataBase.queryDB(
"SELECT s_step FROM Steps WHERE r_id = ?", testarg);
pageCount = results.getCount();
mStepsPagerAdapter.notifyDataSetChanged();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This is called when the Home (Up) button is pressed in the action
// bar.
// Create a simple intent that starts the hierarchical parent
// activity and
// use NavUtils in the Support Package to ensure proper handling of
// Up.
Intent upIntent = new Intent(this, MainActivity.class);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is not part of the application's task, so
// create a new task
// with a synthesized back stack.
TaskStackBuilder.from(this)
// If there are ancestor activities, they should be added here.
.addNextIntent(upIntent).startActivities();
finish();
} else {
// This activity is part of the application's task, so simply
// navigate up to the hierarchical parent activity.
NavUtils.navigateUpTo(this, upIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A {@link android.support.v4.app.FragmentStatePagerAdapter} that returns a
* fragment representing an object in the collection.
*/
public static class StepsPagerAdapter extends FragmentStatePagerAdapter {
public StepsPagerAdapter(
android.support.v4.app.FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int i) {
// increment page object
Fragment fragment = new StepsObjectFragment();
Bundle args = new Bundle();
args.putInt(StepsObjectFragment.ARG_OBJECT, i + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// Sets count to the number of query results
// in the cursor
return StepsFragmentActivity.pageCount;
}
@Override
public CharSequence getPageTitle(int position) {
return "Step " + (position + 1);
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class StepsObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
DataBaseHelper myDataBase = new DataBaseHelper(getActivity());
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Query database
String[] testarg = { "5" };
Cursor results = myDataBase.queryDB(
"SELECT s_step FROM Steps WHERE r_id = ?", testarg);
View rootView = inflater.inflate(
R.layout.fragment_collection_object, container, false);
//Get position from pager adapter
Bundle args = getArguments();
int position = args.getInt(ARG_OBJECT);
//Get the string from the cursor and set it to the page
//This is where I crash
String step = results.getString(position);
((TextView) rootView.findViewById(android.R.id.text1))
.setText(step);
return rootView;
}
}
}
我得到的错误告诉我,我的应用程序无法读取第0行第1列。当我将步骤设置为restults.getString(0)时奇怪!应用程序没有崩溃,但任何其他数字或我的位置整数都会给我错误。
这是我的完整日志猫
05-30 18:36:01.872: E/CursorWindow(7771): Failed to read row 0, column 1 from a CursorWindow which has 5 rows, 1 columns.
05-30 18:36:01.872: W/dalvikvm(7771): threadid=1: thread exiting with uncaught exception (group=0x40bdd1f8)
05-30 18:36:01.892: E/AndroidRuntime(7771): FATAL EXCEPTION: main
05-30 18:36:01.892: E/AndroidRuntime(7771): java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.database.CursorWindow.nativeGetString(Native Method)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.database.CursorWindow.getString(CursorWindow.java:450)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
05-30 18:36:01.892: E/AndroidRuntime(7771): at com.example.cbtn.StepsFragmentActivity$StepsObjectFragment.onCreateView(StepsFragmentActivity.java:178)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.view.View.measure(View.java:12929)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4703)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.view.View.measure(View.java:12929)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.widget.LinearLayout.measureVertical(LinearLayout.java:822)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.view.View.measure(View.java:12929)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4703)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-30 18:36:01.892: E/AndroidRuntime(7771): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2257)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.view.View.measure(View.java:12929)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.os.Looper.loop(Looper.java:137)
05-30 18:36:01.892: E/AndroidRuntime(7771): at android.app.ActivityThread.main(ActivityThread.java:4511)
05-30 18:36:01.892: E/AndroidRuntime(7771): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 18:36:01.892: E/AndroidRuntime(7771): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 18:36:01.892: E/AndroidRuntime(7771): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
05-30 18:36:01.892: E/AndroidRuntime(7771): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
05-30 18:36:01.892: E/AndroidRuntime(7771): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
Cursor.getString(n)
为您提供第n个列的值,而不是第n行(参数是列索引)。
您应该使用move()
来跳转到结果集中所需的行。类似的东西:
result.move(position);
String step result.getString(0);