Android 调用片段方法时出现空指针异常
我已经在这上面停留了一段时间,我正在从一个活动中加载一个片段(替换另一个)。它加载得很好,我可以看到它并返回到前一个片段,但是当我调用一个方法将数据加载到其中时,我得到一个空指针异常。我的第一个密码是Android 调用片段方法时出现空指针异常,android,nullpointerexception,fragment,Android,Nullpointerexception,Fragment,我已经在这上面停留了一段时间,我正在从一个活动中加载一个片段(替换另一个)。它加载得很好,我可以看到它并返回到前一个片段,但是当我调用一个方法将数据加载到其中时,我得到一个空指针异常。我的第一个密码是 public class MainActivity extends Activity implements MainListFragment.MainListListener, StoryFragment.StoryListener { MainListFragment mainListFrag
public class MainActivity extends Activity implements MainListFragment.MainListListener, StoryFragment.StoryListener {
MainListFragment mainListFragment;
StoryFragment storyFragment;
。
.
我得到这个错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at com.maniblett.listtest.StoryFragment.loadStory(StoryFragment.java:60)
at com.maniblett.listtest.MainActivity.onMainListClick(MainActivity.java:104)
片段中的方法是在edittext中设置文本,下面是我调用的片段方法mName和MSSummary是编辑文本小部件的引用:
public void loadStory(Story story){
mName.setText(story.get_name());
mSummary.setText(story.get_summary());
}
引用这样的片段合法吗(通过片段管理器在我用来添加片段的同一引用上调用方法)?似乎我已经有了对片段的引用,所以使用find fragment by ID是多余的,但是当我仔细检查我读到的所有内容时,似乎表明我需要首先使用findframentbyid或Tag来查找片段,所以将我的代码更改为
storyFragment = new StoryFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, storyFragment, "loadedFragment");
transaction.addToBackStack(null);
transaction.commit();
StoryFragment loadedFragment = (StoryFragment)getFragmentManager().findFragmentByTag("loadedFragment");
loadedFragment.loadStory(story);
break;
但我得到了一个类似的错误,那就是:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.maniblett.listtest.StoryFragment.loadStory(com.maniblett.listtest.datamodel.Story)' on a null object reference
at com.maniblett.listtest.MainActivity.onMainListClick(MainActivity.java:107)
at com.maniblett.listtest.MainListFragment.onListItemClick(MainListFragment.java:156)
at android.app.ListFragment$2.onItemClick(ListFragment.java:160)
at android.widget.AdapterView.performItemClick(AdapterView.java:300)
我已经验证了我正在发送的对象在创建后不为null(case语句中的“story”是一个枚举)。同样,如果我注释掉方法调用,frgament会加载并运行良好,只是在我调用emthod时失败了。那么,我想我没有实际加载的片段?有人能告诉我我做错了什么吗?(我确实搜索了许多其他类似的主题,但没有找到任何有用的内容,我对安卓相当陌生)。感谢所有花时间帮忙的人
故事片段类:
public class StoryFragment extends Fragment
{
StoryListener activityCallback;
private EditText mName;
private EditText mSummary;
public interface StoryListener {
public void onAddButtonClick(String text);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_story, container, false);
Button button = (Button)rootView.findViewById(R.id.button);
mName = (EditText)rootView.findViewById(R.id.name);
mSummary = (EditText)rootView.findViewById(R.id.summary);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
DBHandler dbHandler = new DBHandler(getActivity().getBaseContext(), null, null, 1);
Story story = new Story(mName.getText().toString(),mSummary.getText().toString());
dbHandler.addStory(story);
activityCallback.onAddButtonClick("Story"); //use same callback for all record types passing back record type created?
}
} );
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try { activityCallback = (StoryListener) activity;
}
catch (ClassCastException e) {
throw new ClassCastException(activity.toString()+ " must implement StoryListener");
}
}
public void loadStory(Story story){
mName.setText(story.get_name());
mSummary.setText(story.get_summary());
}
}
首先,你应该了解android是如何工作的。
onCreateView
方法仅在片段可见时调用。因此,当您在FragmentTransaction
方法之后立即调用loadstory
时,很明显您将得到一个NullPointer异常
我的解决方案:
在StoryFragment
中将两个变量声明为name
和summary
更改loadStory
方法如下
public void loadStory(Story story){
this.name = story.get_name();
this.summary = story.get_summary();
}
最后在StoryFragment
的OnCreateView
方法中进行适当更改
mName = (EditText)rootView.findViewById(R.id.name);
mSummary = (EditText)rootView.findViewById(R.id.summary);
//if your fragment is also gonna be called by some other manner you should check for null in this.name and this.summary before setting it to the `TextView`
mName.setText(this.name);
mSummary.setText(this.summary);
首先,你应该了解android是如何工作的。
onCreateView
方法仅在片段可见时调用。因此,当您在FragmentTransaction
方法之后立即调用loadstory
时,很明显您将得到一个NullPointer异常
我的解决方案:
在StoryFragment
中将两个变量声明为name
和summary
更改loadStory
方法如下
public void loadStory(Story story){
this.name = story.get_name();
this.summary = story.get_summary();
}
最后在StoryFragment
的OnCreateView
方法中进行适当更改
mName = (EditText)rootView.findViewById(R.id.name);
mSummary = (EditText)rootView.findViewById(R.id.summary);
//if your fragment is also gonna be called by some other manner you should check for null in this.name and this.summary before setting it to the `TextView`
mName.setText(this.name);
mSummary.setText(this.summary);
首先,你应该了解android是如何工作的。
onCreateView
方法仅在片段可见时调用。因此,当您在FragmentTransaction
方法之后立即调用loadstory
时,很明显您将得到一个NullPointer异常
我的解决方案:
在StoryFragment
中将两个变量声明为name
和summary
更改loadStory
方法如下
public void loadStory(Story story){
this.name = story.get_name();
this.summary = story.get_summary();
}
最后在StoryFragment
的OnCreateView
方法中进行适当更改
mName = (EditText)rootView.findViewById(R.id.name);
mSummary = (EditText)rootView.findViewById(R.id.summary);
//if your fragment is also gonna be called by some other manner you should check for null in this.name and this.summary before setting it to the `TextView`
mName.setText(this.name);
mSummary.setText(this.summary);
首先,你应该了解android是如何工作的。
onCreateView
方法仅在片段可见时调用。因此,当您在FragmentTransaction
方法之后立即调用loadstory
时,很明显您将得到一个NullPointer异常
我的解决方案:
在StoryFragment
中将两个变量声明为name
和summary
更改loadStory
方法如下
public void loadStory(Story story){
this.name = story.get_name();
this.summary = story.get_summary();
}
最后在StoryFragment
的OnCreateView
方法中进行适当更改
mName = (EditText)rootView.findViewById(R.id.name);
mSummary = (EditText)rootView.findViewById(R.id.summary);
//if your fragment is also gonna be called by some other manner you should check for null in this.name and this.summary before setting it to the `TextView`
mName.setText(this.name);
mSummary.setText(this.summary);
多谢各位。这非常有效。实际上,我所做的是创建一个私有的故事类型,然后在StoryFragment.loadStory中分配它
public void loadStory(Story story){
mStory = story;
}
我还能够看到,不必使用findFragmentByID搜索片段也可以很好地工作,这部分工作:
storyFragment = new StoryFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, storyFragment);
transaction.addToBackStack(null);
transaction.commit();
storyFragment.loadStory(story);
非常感谢您在这里为我指明了正确的方向谢谢。这非常有效。实际上,我所做的是创建一个私有的故事类型,然后在StoryFragment.loadStory中分配它
public void loadStory(Story story){
mStory = story;
}
我还能够看到,不必使用findFragmentByID搜索片段也可以很好地工作,这部分工作:
storyFragment = new StoryFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, storyFragment);
transaction.addToBackStack(null);
transaction.commit();
storyFragment.loadStory(story);
非常感谢您在这里为我指明了正确的方向谢谢。这非常有效。实际上,我所做的是创建一个私有的故事类型,然后在StoryFragment.loadStory中分配它
public void loadStory(Story story){
mStory = story;
}
我还能够看到,不必使用findFragmentByID搜索片段也可以很好地工作,这部分工作:
storyFragment = new StoryFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, storyFragment);
transaction.addToBackStack(null);
transaction.commit();
storyFragment.loadStory(story);
非常感谢您在这里为我指明了正确的方向谢谢。这非常有效。实际上,我所做的是创建一个私有的故事类型,然后在StoryFragment.loadStory中分配它
public void loadStory(Story story){
mStory = story;
}
我还能够看到,不必使用findFragmentByID搜索片段也可以很好地工作,这部分工作:
storyFragment = new StoryFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, storyFragment);
transaction.addToBackStack(null);
transaction.commit();
storyFragment.loadStory(story);
非常感谢你给我指明了正确的方向