Java 片段参数为空
我试图将数据从一个片段发送到另一个片段,为此我使用了bundle。但是,无论何时,当我试图从第二个片段中的包中获取任何信息时,我都会收到一条错误消息,说我试图获取一个空对象。在创建第二个片段之前,我已经设置了它的参数,在发送它之前,我还向包中添加了信息。我找不出是什么问题。这是主片段中的接口代码,应该打开详细信息片段Java 片段参数为空,java,android,android-fragments,bundle,Java,Android,Android Fragments,Bundle,我试图将数据从一个片段发送到另一个片段,为此我使用了bundle。但是,无论何时,当我试图从第二个片段中的包中获取任何信息时,我都会收到一条错误消息,说我试图获取一个空对象。在创建第二个片段之前,我已经设置了它的参数,在发送它之前,我还向包中添加了信息。我找不出是什么问题。这是主片段中的接口代码,应该打开详细信息片段 public interface ListClickHandler { public void onlistElementClicked ( Bundle ar
public interface ListClickHandler {
public void onlistElementClicked ( Bundle args); //we'll have to override it in the parent activity.
}//end interface.
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (ListClickHandler) activity;
}//end try
catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ListClickHandler interface");
}//end catch
}
另外,我在两个地方创建了bundle,一次是在主片段中,它包含一个列表,如果单击了任何项目,则创建bundle,将信息添加到bundle,并将该bundle传递给接口内的方法,如下所示
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_todo_list, container, false);
mSimpleCursorAdapter=new SimpleCursorAdapter(getActivity(),R.layout.notes_row,null, from, to,0);
getLoaderManager().initLoader(LOADER_ID, null, this); //once this is done onCreateLoader will be called.
ListView listView = (ListView) rootView.findViewById(R.id.notes_list); //findViewById must be called using the rootView because we are inside a fragment.
listView.setAdapter(mSimpleCursorAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Cursor cursor = mSimpleCursorAdapter.getCursor();
if (cursor != null && cursor.moveToPosition(position)) {
String category= cursor.getString(1);
String summary= cursor.getString(2);
String description=cursor.getString(3);
long id= cursor.getLong(cursor.getColumnIndex(NotesContract.NotesTable._ID));
int locationId= cursor.getInt(cursor.getColumnIndex(NotesContract.NotesTable.COLUMN_LOCATION));
String [] retrievedData= {category, summary, description};
if (getActivity().findViewById (R.id.fragment_container)!=null){
//two pane layout:
Bundle args = new Bundle();
args.putStringArray("data",retrievedData);
/*args.putInt("update", 1);*/
args.putLong("id", id);
args.putInt("locationId", locationId);
mCallback.onlistElementClicked(args );/*this is available in the parent activity*/
}
else {
// one pane layout:
Intent intent = new Intent(getActivity(), NotesDetails.class);
intent.putExtra(Intent.EXTRA_TEXT, retrievedData);
/*intent.putExtra("update", 1); */ //to indicate that the query should be update not insert.
intent.putExtra("id", id);
intent.putExtra("locationId", locationId); //whether it is 0 or 1
startActivity(intent);
}
}//end outer cursor if.
}
});
return rootView;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {//open the settings activity to enable the user to change the settings.
//open settings activity via intent here.
startActivity(new Intent (this, Settings.class));
return true;
}
if (id==R.id.text_note){ //open the details activity where the user can enter their notes and save it.
if (twoPane) {
args.putBoolean("location", false);
mCallBack.onlistElementClicked(args);
}
else {
Intent intent = new Intent(this, NotesDetails.class);
startActivity(intent);
return true; //this line is necessary
}
}//end if
if (id==R.id.location_note)
{
if (twoPane) {
args.putBoolean("location", true);
mCallBack.onlistElementClicked(args);
}
else {
//prepare intent here:
Intent intent = new Intent(this, NotesDetails.class);
intent.putExtra("location", true);
startActivity(intent);
}
}
return super.onOptionsItemSelected(item);
}
这就是我在主活动中重写onlistElementClicked的方式
@Override
public void onlistElementClicked(Bundle args) {
DetailsFragment detailsFragment = new DetailsFragment();
detailsFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, detailsFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}//end interface method.
这就是我如何在细节片段(应该从主片段打开的片段)的参数中获取信息的方法
在此之后,我使用args获取捆绑包中的任何信息,但是我得到了前面提到的错误
谁能帮帮我吗?我在网上查过几个解决方案,但都不管用
多谢各位 您应该这样分配Bundle的值:
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
youtBandGlobalMember = getArguments();
}
实际上,我没有在任何方法中调用它,它是在fragment类的主体中调用的,它的值被分配给一个全局变量。这不对吗
现在太早了。成员变量在构造对象时初始化,也就是在对片段对象调用setArguments()
之前
将
getArguments()
调用推迟到片段中的on…()
生命周期回调之一。在哪里调用getArguments?在哪里调用此绑定args=this.getArguments();方法?如何获得这些参数值,可以发布代码吗?您是否调试并查看了它的值?此外,您在哪里创建了包?@yshahaak和@Preethi Rao,我在fragment类中调用它,并将该值分配给全局变量args。因此,我可以从片段中的任何位置访问参数谢谢@yshahahak的回答,我这样做了,但仍然得到相同的错误,java.lang.RuntimeException:无法启动activity ComponentInfo{com.project.android.notes/com.project.android.notes.NotesList}:java.lang.NullPointerException:尝试在空对象引用03-12上调用虚拟方法“boolean android.os.Bundle.containsKey(java.lang.String)”。我也试着在ONATACH中这样做,但是我得到了同样的错误。你能告诉我解决办法吗?谢谢。你的代码不是很清楚。在OnOptions ItemSelected(MenuItem项)中,使用绑定参数。捆绑包的分配在哪里?@yshahahak表示歉意,但我没有回答您的问题,我已经在if语句中创建了捆绑包(Bundle args=new Bundle();),然后我使用args.putLong、args.putString等方法设置了捆绑包中的数据。只有当应用程序在平板电脑上运行时,才会创建捆绑包,在我希望屏幕在同一活动中打开列表和详细信息片段的地方,当单击列表片段中的任何项目时,详细信息片段应根据捆绑包中发送给它的数据设置其内容。我可以把我的代码发给你,你可以查一下吗?请让我知道,非常感谢您编写的if(twoPane){args.putBoolean(“location”,false);mCallBack.onlistElementClicked(args);}。该args的创建在哪里?我在活动中将args定义为一个全局变量,在类的主体中(不在任何方法中)这样做,公共类NotesList扩展ActionBarActivity实现NotesFragment.ListClickHandler{boolean twoPane;Bundle args=new Bundle();NotesFragment.ListClickHandler mCallBack;谢谢@laalto的回答我在onCreate和onAttach回调中都尝试过这样做,但仍然出现相同的错误,您能告诉我如何解决这个问题吗?谢谢。
Bundle args=this.getArguments();
Bundle args=this.getArguments();