我在同一个活动中将一个数字从一个片段传递到另一个片段时出现问题,我将该数字传递给Activity,但是当我尝试将它传递给第二个片段时,它会给我null。 任何帮助表示赞赏
以下是Passing的代码以及更多内容,可能存在错别字,因为我想尽可能少地编写代码
MainActivity
package com.example.design.ex;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import example.test.R;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.example.design.ex.fragments.ReceiptItemsFragment;
public class NewReceiptActivity extends SherlockFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int num = 0;
Intent intent = getIntent();
int Broj = intent.getIntExtra("Num", num);
Bundle bundle=new Bundle();
bundle.putInt("Broj", Broj);
setContentView(R.layout.activity_test);
ReceiptItemsFragment frag = (ReceiptItemsFragment)
getSupportFragmentManager().findFragmentById(R.id.receipt);
frag.setArguments(bundle);
}
}
片段1
package com.example.design.ex.fragments;
import example.test.R;
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import com.actionbarsherlock.app.SherlockFragment;
public class CategoriesItemsFragment extends SherlockFragment {
int num = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_categories_items,
container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init();
}
private void init() {
itemsGrid.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, final View itemView, int
arg2, long arg3)
{
num = arg2 + 1;
Intent intent = new Intent(getActivity().getBaseContext(),NewReceiptActivity.class);
intent.putExtra("Num", num);
startActivity(new Intent(getActivity(), NewReceiptActivity.class));
getActivity().startActivity(intent);
}
});
}
}
片段2
package example.design.ex.fragments;
import example.test.R;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;
public class ReceiptItemsFragment extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
Bundle arguments = getArguments();
if (arguments != null)
{
Log.d("ISnull","no!");
} else {
Log.d("ISnull","yes!");
}
int num=getArguments().getInt("Num");
return inflater.inflate(R.layout.fragment_receipt_items, container,
false);
}
主要活动XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="@+id/titles"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1.5"
class="example.ex.fragments.CategoriesItemsFragment" />
<LinearLayout
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/sun_flower" >
</LinearLayout>
<fragment
android:id="@+id/receipt"
android:name="example.ex.fragments.ReceiptItemsFragment"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
class="example.ex.fragments.ReceiptItemsFragment" />
</LinearLayout>
这是错误
06-11 07:41:00.419: E/AndroidRuntime(2801): at
com.example.NewReceiptActivity.onCreate(NewReceiptActivity.java:60)
这是这行或下一个如果我把日志(试图让它给我看id)
ReceiptItemsFragment frag = (ReceiptItemsFragment)
getSupportFragmentManager().findFragmentById(R.id.receipt);
答案 0 :(得分:1)
我认为问题在于:
ReceiptItemsFragment frag=new ReceiptItemsFragment(); //2nd fragment
frag.setArguments(bundle);
您正在创建一个新片段,但它不是您在布局中获得的片段。
试试这个:
setContentView(R.layout.activity_test); // Here is where that 2 fragments are Located
ReceiptItemsFragment frag = getFragmentManager().findFragmentById(...) //your fragment id
frag.setArguments(bundle);
所以你不要创建一个新的片段,但要引用你在布局中得到的片段。
希望有所帮助。
答案 1 :(得分:1)
验证frag
中的NewReceiptActivity
是否为空。验证Numt
是否为2
。
在ReceiptItemsFragment中移动该行:
int num=getArguments().getInt("Num"); //this should get number 2 from Frist Fragment but it gives null
从方法onCreateView到onActivityCreated:
public void onActivityCreated (Bundle savedInstanceState) {
int num=getArguments().getInt("Num"); //this should get number 2 from Frist Fragment but it gives null
答案 2 :(得分:0)
您可以创建Application的子类并将对象存储在那里。检查this。 或者您可以在活动类中使用静态变量。
答案 3 :(得分:0)
我使用Interface传递数据的方法不同,如此处所述
https://stackoverflow.com/a/12311939/1393695
让它运转起来,我要感谢大家的帮助。
问题在于我正在看片段,因为它们是活动的所在。