捆绑参数返回null

时间:2015-12-04 19:26:10

标签: android sqlite arguments fragment bundle

我正在尝试通过捆绑包将SQLite数据库中的一些数据发送到片段。

调试器显示cursor正在返回所有列,但StringArrays在将它们添加到包时为空。

为什么会这样?

更新

这似乎没问题,但它正在片段中抛出null pointer exception

第46行是String[] Name = args.getStringArray(names);

参见LOGCAT:

12-04 13:55:41.812 5379-5379/com.example.michael.budgetapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.example.michael.budgetapp, PID: 5379
                                                                         java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference
                                                                             at com.example.michael.budgetapp.Tabs.Transactions.TransactionsTab.onCreateView(TransactionsTab.java:46)
                                                                             at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                                             at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                             at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                                             at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                             at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
                                                                             at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
                                                                             at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
                                                                             at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
                                                                             at android.view.View.measure(View.java:17547)
                                                                             at android.widget.LinearLayout.measureVertical(LinearLayout.java:875)
                                                                             at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                                                                             at android.view.View.measure(View.java:17547)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                             at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                             at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                             at android.view.View.measure(View.java:17547)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                             at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                             at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                                                                             at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                                                                             at android.view.View.measure(View.java:17547)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                             at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                             at android.view.View.measure(View.java:17547)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                             at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                             at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                                                                             at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                                                                             at android.view.View.measure(View.java:17547)
                                                                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                             at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                             at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
                                                                             at android.view.View.measure(View.java:17547)
                                                                             at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
                                                                             at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
                                                                             at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
                                                                             at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
                                                                             at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
                                                                             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                                                                             at android.view.Choreographer.doCallbacks(Choreographer.java:580)
                                                                             at android.view.Choreographer.doFrame(Choreographer.java:550)
                                                                             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
                                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:135)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5257)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:372)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

片段类:

public class TransactionsTab extends Fragment {

RecyclerView transactionsRecycler;
AdapterTransactions adapter;
TransactionLoad transactionLoad = null;
private Cursor data = null;
List<TransactionInfo> adapterData = new ArrayList<>();
String names, categories, currencies, prices, dates;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    transactionLoad = new TransactionLoad(getActivity());
    transactionLoad.execute();
}

@Override
public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab_transactions, container, false);
    transactionsRecycler = (RecyclerView) view.findViewById(R.id.transactionRecycler);
    adapter = new AdapterTransactions(getActivity(), adapterData);
    transactionsRecycler.setAdapter(adapter);
    transactionsRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
    transactionLoad = new TransactionLoad(getActivity());
    transactionLoad.execute();

    Bundle args = getArguments();
    String[] Name = args.getStringArray(names);
    String[] Category = args.getStringArray(categories);
    String[] Currency = args.getStringArray(currencies);
    String[] Price = args.getStringArray(prices);
    String[] Date = args.getStringArray(dates);
    for (int i = 0;
         i < Name.length &&
         i < Category.length &&
         i < Currency.length &&
         i < Price.length &&
         i < Date.length;
         i++) {
        TransactionInfo info = new TransactionInfo();
        info.itemName = Name[i];
        info.itemCat = Category[i];
        info.itemCurrency = Currency[i];
        info.itemPrice = Price[i];
        info.date = Date[i];
        adapterData.add(info);
        }
    adapter.setData(adapterData);
    adapter.notifyDataSetChanged();
    adapter.transactionData.notifyAll();

    return view;
}

enter image description here

String names, categories, currencies, prices, dates;

@Override
public void transactionLoadComplete(Cursor cursor) {
    tData = cursor;
    while (tData.moveToNext()) {
        String[] Name = new String[]{DatabaseHelper.TRANSACTION_NAME};
        String[] Category = new String[]{DatabaseHelper.TRANSACTION_CATEGORY};
        String[] Currency = new String[]{DatabaseHelper.TRANSACTION_CURRENCY};
        String[] Price = new String[]{DatabaseHelper.TRANSACTION_PRICE};
        String[] Date = new String[]{DatabaseHelper.TRANSACTION_DATE};

        String names = tData.getString(tData.getColumnIndex(DatabaseHelper.TRANSACTION_NAME));
        String categories = tData.getString(tData.getColumnIndex(DatabaseHelper.TRANSACTION_CATEGORY));
        String currencies = tData.getString(tData.getColumnIndex(DatabaseHelper.TRANSACTION_CURRENCY));
        String prices = tData.getString(tData.getColumnIndex(DatabaseHelper.TRANSACTION_PRICE));
        String dates = tData.getString(tData.getColumnIndex(DatabaseHelper.TRANSACTION_DATE));

        Bundle args = new Bundle();
        args.putStringArray(names, Name);
        args.putStringArray(categories, Category);
        args.putStringArray(currencies, Currency);
        args.putStringArray(prices, Price);
        args.putStringArray(dates, Date);
        TransactionsTab transactionsTab = new TransactionsTab();
        transactionsTab.setArguments(args);
    }
}

0 个答案:

没有答案