调用片段方法抛出 NullPointerException
Calling fragment method throws NullPointerException
我实际上有两个活动。第一个是调用另一个以获得结果。
startActivityForResult 部分没问题。请求码和结果码都可以。
但是它在 Context.getResources() 上抛出 NullPointerException,我不明白为什么。我知道这个异常是什么意思,但是在这个特定的情况下,我不明白。
这是我的源代码:
在 onCreate 之前(在 class 范围内)
ComponentsFragment componentsFragment;
在onCreate
componentsFragment = new ComponentsFragment();
按钮点击事件
Intent i = new Intent(ReportActivity.this, ManageComponentsActivity.class);
i.putExtra("action", PICK_COMPONENT_REQUEST);
startActivityForResult(i, PICK_COMPONENT_REQUEST);
第二个activity
Toast.makeText(c, "*****activity started for result", Toast.LENGTH_LONG).show();
Component component = (Component) parent.getItemAtPosition(position);
Toast.makeText(c, "****component : " + component.getName(), Toast.LENGTH_LONG).show();
Intent i = new Intent();
i.putExtra("component_name", component.getName());
setResult(RESULT_OK, i);
finish();
第一个activity-onActivityResult
if (requestCode == PICK_COMPONENT_REQUEST && resultCode == RESULT_OK) {
Toast.makeText(this, "*****on activity result", Toast.LENGTH_LONG).show();
Toast.makeText(this, "*****component_name : " + data.getStringExtra("component_name"), Toast.LENGTH_LONG).show();
Toast.makeText(this, "*****report_id : " + report_id, Toast.LENGTH_LONG).show();
Toast.makeText(this, "*****category_id : " + category_id, Toast.LENGTH_LONG).show();
try {
if (componentsFragment == null) {
Toast.makeText(this, "*****componentsFragment IS NULL", Toast.LENGTH_LONG).show();
}
//componentsFragment.AddComponentItem(data.getStringExtra("component_name"), report_id, category_id);
componentsFragment.AddComponentItem("Fenêtre", "1", "1");
componentsFragment.RefreshList("1", "1");
}
catch (Exception e) {
Toast.makeText(this, String.valueOf(e), Toast.LENGTH_LONG).show();
}
}
对于'Kitchen'的示例组件,实际输出是:
*****activity 已开始,结果
*****组件:厨房
*****在 activity 结果上
*****component_name:厨房
*****report_id : 1
*****category_id : 1
抛出异常
所以 componentsFragment 不为空...
抱歉,我的 logcat 有一些问题所以我实际上使用 Toasts :(
这是替换片段事务的问题。
我没有在这个事务中使用上面描述的 componentsFragment,而是一个新创建的。
ComponentsFragment fragment = new CommponentsFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null); // Commit the transaction
transaction.commit();
这一行:
transaction.replace(R.id.fragment_container, fragment);
必须替换为:
transaction.replace(R.id.fragment_container, componentsFragment);
而且应该删除这个:
ComponentsFragment fragment = new CommponentsFragment();
我实际上有两个活动。第一个是调用另一个以获得结果。
startActivityForResult 部分没问题。请求码和结果码都可以。
但是它在 Context.getResources() 上抛出 NullPointerException,我不明白为什么。我知道这个异常是什么意思,但是在这个特定的情况下,我不明白。
这是我的源代码:
在 onCreate 之前(在 class 范围内)
ComponentsFragment componentsFragment;
在onCreate
componentsFragment = new ComponentsFragment();
按钮点击事件
Intent i = new Intent(ReportActivity.this, ManageComponentsActivity.class);
i.putExtra("action", PICK_COMPONENT_REQUEST);
startActivityForResult(i, PICK_COMPONENT_REQUEST);
第二个activity
Toast.makeText(c, "*****activity started for result", Toast.LENGTH_LONG).show();
Component component = (Component) parent.getItemAtPosition(position);
Toast.makeText(c, "****component : " + component.getName(), Toast.LENGTH_LONG).show();
Intent i = new Intent();
i.putExtra("component_name", component.getName());
setResult(RESULT_OK, i);
finish();
第一个activity-onActivityResult
if (requestCode == PICK_COMPONENT_REQUEST && resultCode == RESULT_OK) {
Toast.makeText(this, "*****on activity result", Toast.LENGTH_LONG).show();
Toast.makeText(this, "*****component_name : " + data.getStringExtra("component_name"), Toast.LENGTH_LONG).show();
Toast.makeText(this, "*****report_id : " + report_id, Toast.LENGTH_LONG).show();
Toast.makeText(this, "*****category_id : " + category_id, Toast.LENGTH_LONG).show();
try {
if (componentsFragment == null) {
Toast.makeText(this, "*****componentsFragment IS NULL", Toast.LENGTH_LONG).show();
}
//componentsFragment.AddComponentItem(data.getStringExtra("component_name"), report_id, category_id);
componentsFragment.AddComponentItem("Fenêtre", "1", "1");
componentsFragment.RefreshList("1", "1");
}
catch (Exception e) {
Toast.makeText(this, String.valueOf(e), Toast.LENGTH_LONG).show();
}
}
对于'Kitchen'的示例组件,实际输出是:
*****activity 已开始,结果
*****组件:厨房
*****在 activity 结果上
*****component_name:厨房
*****report_id : 1
*****category_id : 1
抛出异常
所以 componentsFragment 不为空...
抱歉,我的 logcat 有一些问题所以我实际上使用 Toasts :(
这是替换片段事务的问题。
我没有在这个事务中使用上面描述的 componentsFragment,而是一个新创建的。
ComponentsFragment fragment = new CommponentsFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, fragment);
transaction.addToBackStack(null); // Commit the transaction
transaction.commit();
这一行:
transaction.replace(R.id.fragment_container, fragment);
必须替换为:
transaction.replace(R.id.fragment_container, componentsFragment);
而且应该删除这个:
ComponentsFragment fragment = new CommponentsFragment();