在下面的代码中我尝试从和活动
调用片段public class WalletActivity extends AppCompatActivity
{
private Button mBankAccount;
private Button mCreditCard;
private Button mPassport;
private Button mLicence;
private Button mIdentity;
private Button mWifiRouter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wallet);
mBankAccount = (Button) findViewById(R.id.bank_account);
mBankAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = BankAccountListActivity.newIntent(WalletActivity.this);
startActivity(intent);
}
});
mIdentity = (Button) findViewById(R.id.identity);
mIdentity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = IndentityListActivity.newIntent(WalletActivity.this);
startActivity(intent);
}
});
}
}
注意: - 查看代码的身份部分
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/identity_recycler_view"
android:layout_height="match_parent">
public class IndentityListActivity extends SingleFragmentActivity
{
public static Intent newIntent(Context packageContext) {
Intent i = new Intent(packageContext, IndentityListActivity.class);
return i;
}
@Override
protected Fragment createFragment() {
return new IdentityListFragment();
}}
public class IdentityListFragment extends Fragment
{
private RecyclerView mRecyclerView;
private IdentityAdapter mIdentityAdapter;
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//Wiring recycler view
View view = inflater.inflate(R.layout.identity_list_fragment, container, false);
mRecyclerView = (RecyclerView) view
.findViewById(R.id.identity_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
@Override
public void onResume() {
super.onResume();
updateUI();
}
private class IdentityHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private Identity mIdentity;
private TextView mTitleTextView;
public IdentityHolder(View itemView)
{
super(itemView);
itemView.setOnClickListener(this);
mTitleTextView = (TextView) itemView.findViewById(R.id.list_item_identity_title_text_view);
}
//binds the title,date and check box
public void bindIdentity(Identity identity)
{
mIdentity = identity;
mTitleTextView.setText(mIdentity.getIdentity());
}
@Override
public void onClick(View v)
{
Intent intent = IdentityPagerActivity.newIntent(getActivity(), mIdentity.getUUID());
startActivity(intent);
}
}
private class IdentityAdapter extends RecyclerView.Adapter<IdentityHolder>
{
private List<Identity> mIdentities;
public IdentityAdapter (List<Identity> identities )
{
mIdentities = identities ;
}
//This method is called to create a view
@Override
public IdentityHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View view = layoutInflater
.inflate(R.layout.identity_list_fragment, parent, false);
return new IdentityHolder(view);
}
//binds them together
@Override
public void onBindViewHolder(IdentityHolder holder, int position)
{
Identity identity = mIdentities.get(position);
holder.bindIdentity(identity);
}
private void setIdentities (List<Identity> identities)
{
mIdentities = identities;
}
//RecycleView Calls this method to get the size of mCrimes
@Override
public int getItemCount() {
return mIdentities.size();
}
}
public void updateUI()
{
WalletIdentity walletIdentity = WalletIdentity.get(getActivity());
List<Identity> identities = walletIdentity.getIdentity();
if (mIdentityAdapter == null)
{
mIdentityAdapter = new IdentityAdapter(identities);
mRecyclerView.setAdapter(mIdentityAdapter);
}
else
{
mIdentityAdapter.setIdentities(identities);
mIdentityAdapter.notifyDataSetChanged();
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_crime_list, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_item_new_crime:
Identity identity = new Identity();
WalletIdentity.get(getActivity()).addIdentity(identity);
Intent intent = IdentityPagerActivity
.newIntent(getActivity(), identity.getUUID());
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}}
注意: - 我在BankAccount中实现了几乎相同的代码,它工作正常
FATAL EXCEPTION: main
Process: com.example.kamalnrf.wallet, PID: 2456
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kamalnrf.wallet/com.example.kamalnrf.wallet.Controllers.IdentityControllers.IndentityListActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2362)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:139)
at android.app.ActivityThread.main(ActivityThread.java:5298)
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:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at com.example.kamalnrf.wallet.Controllers.IdentityControllers.IdentityListFragment.onCreateView(IdentityListFragment.java:45)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:602)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:139)
at android.app.ActivityThread.main(ActivityThread.java:5298)
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:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
我点击身份按钮时收到上述消息......
如果您需要任何其他数据,请告诉我
答案 0 :(得分:1)
检查identity_recycler_view是否在identity_list_fragment布局文件中
答案 1 :(得分:1)
<强>问题强>
View view = inflater.inflate(R.layout.identity_list_fragment, container, false);
mRecyclerView = (RecyclerView) view
.findViewById(R.id.identity_recycler_view);
此处发生异常
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
<强>原因强>
引起:java.lang.NullPointerException:尝试调用 虚方法&#39;无效 android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $布局管理)&#39; 在null对象引用上 在 com.example.kamalnrf.wallet.Controllers.IdentityControllers.IdentityListFragment.onCreateView(IdentityListFragment.java:45)
Logcat说你正试图在null
RecyclerView对象
可能的解决方案
查看identity_list_fragment.xml
内部必须包含ID为identity_recycler_view