getArguments()抛出空指针异常

时间:2015-12-20 11:14:33

标签: java android sqlite bundle

这是我想要实现的目标:

有一个页面(片段是一个选项卡),它为单个事务从数据库加载数据并将其显示在recyclerView中。 RecyclerView工作得很好,因为我能够在TransactionTab中手动填充这些字符串数组String[] Name = new String[] {"Lunch", "Dinner", "Breakfast"}

我试图从我在MainAcitivity中打开的Cursor对象加载所有这些数据,然后通过bundle将所有数据传递给片段。

Cursor对象返回所有值,并且捆绑包正确(我认为)已填充。但是,当涉及到片段中的包,我得到NullPointerException

我在NullPointerException

中的以下一行获得了TransactionsTab.java

String[] Name = args.getStringArray(names);

我不确定为什么它为空。我知道recyclerView很好,并且通过向String Arrays添加虚拟数据来测试它。

我知道Bundle正在MainActivity中正确保存(见下图),但它仍在抛出NullPointerException

enter image description here

MainActivity.java:

public class MainActivity extends AppCompatActivity
    implements CategoryLoad.categoryLoadComplete,
    TransactionLoad.LoadComplete {

private android.support.v7.widget.Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
ViewPagerAdapter mAdapter;
CharSequence Titles[]={"Budgets", "Transactions"};
int Numboftabs = 2;
private Cursor tData = null;
private Cursor cData = null;
String names, categories, currencies, prices, dates;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    mAdapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
    mTabs.setDistributeEvenly(true);
    mTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.accent);
        }
    });
    mTabs.setViewPager(mPager);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this, AddTransaction.class));
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main_activity, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        startActivity(new Intent(this, Settings.class));
        return true;
    }

    if (id == R.id.category) {
        startActivity(new Intent(this, AddCategory.class));
        return true;
    }
    if (id == R.id.search) {
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void categoryLoadComplete(Cursor cursor) {
    cData = cursor;
}

@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);
    }
}
}

AdapterTransactions.class:

public class AdapterTransactions extends RecyclerView.Adapter<AdapterTransactions.MyViewHolder> {

private LayoutInflater inflater;
List<TransactionInfo> transactionData = Collections.emptyList();

public void setData(List<TransactionInfo> newData) {
    transactionData = newData;
}

public AdapterTransactions(Context context, List<TransactionInfo> transactionData) {
    inflater = LayoutInflater.from(context);
    this.transactionData = transactionData;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.transaction_row, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    TransactionInfo current = transactionData.get(position);
    holder.itemName.setText(current.itemName);
    holder.itemCat.setText(current.itemCat);
    holder.itemPrice.setText(current.itemPrice);
    holder.itemDate.setText(current.date);
    holder.itemCurrency.setText(current.itemCurrency);
}

@Override
public int getItemCount() {
    return transactionData.size();
}

class MyViewHolder extends RecyclerView.ViewHolder {
    TextView itemName, itemCat, itemPrice, itemDate, itemCurrency;

    public MyViewHolder(View itemView) {
        super(itemView);
        itemName = (TextView) itemView.findViewById(R.id.itemName);
        itemCat = (TextView) itemView.findViewById(R.id.itemCat);
        itemPrice = (TextView) itemView.findViewById(R.id.itemPrice);
        itemDate = (TextView) itemView.findViewById(R.id.itemDate);
        itemCurrency = (TextView) itemView.findViewById(R.id.itemCurrency);
    }
}
}

TransactionInfo.class:

public class TransactionInfo {
public String itemName, itemPrice, itemCurrency, itemCat, date;
}

TransactionsTab.class:

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;
}
}

0 个答案:

没有答案