0
我,而在片段通信面臨的一個問題, 下面是我的代碼:爲什麼我在將一個片段與另一個片段進行通信時將輸出變爲null?
ListPolicies片段
public class ListPolicies extends ListFragment implements AdapterView.OnItemClickListener{
String polnames;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.list_policies, null, false);
}
public String polName(String name){
if(name!=null) {
polnames = name;
}
Log.d("Luffy", "Pos" +polnames);
return polnames;
}
String polname = polName(polnames);
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (polname != null) {
Log.d("Luffy", "Position 00");
if (polname.equalsIgnoreCase("0")) {
Log.d("Luffy", "Position 0");
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Engineering_Insurance, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Uploaded :" + position, Toast.LENGTH_LONG).show();
}
}
ViewPolicy片段
public class ViewPolicyFragment extends ListFragment implements AdapterView.OnItemClickListener{
String[] menutitles;
TypedArray menuIcons;
PolicyCommunicator comm;
CustomAdapter adapter;
private List<rowItem> rowItems;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.view_policy_fragment, null, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
menutitles = getResources().getStringArray(R.array.Insurance);
menuIcons = getResources().obtainTypedArray(R.array.icons);
rowItems = new ArrayList<rowItem>();
for (int i = 0; i < menutitles.length; i++) {
rowItem items = new rowItem(menutitles[i], menuIcons.getResourceId(
i, -1));
rowItems.add(items);
}
adapter = new CustomAdapter(getActivity(), rowItems);
setListAdapter(adapter);
comm = (PolicyCommunicator) getActivity();
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
comm.policyName("" +position);
if(menutitles[position].equalsIgnoreCase("Engineering Insurance")){
Toast.makeText(getActivity(), "You have Clicked on Engineering Insurance", Toast.LENGTH_SHORT)
.show();
}
if(menutitles[position].equalsIgnoreCase("Fire Insurance")){
Toast.makeText(getActivity(), "You have Clicked on Fire Insurance", Toast.LENGTH_SHORT)
.show();
}
if(menutitles[position].equalsIgnoreCase("Liability Insurance")){
Toast.makeText(getActivity(), "You have Clicked on Liability Insurance", Toast.LENGTH_SHORT)
.show();
}
}
}
和我的活動實現使方法
public class AfterLoginActivity extends AppCompatActivity implements PolicyCommunicator {
Toolbar toolbar;
FragmentManager manager;
Intent intent= null, chooser= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.after_login_activity);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
manager = getFragmentManager();
AfterLoginAboutFragment fragViewPolicy = new AfterLoginAboutFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.linearLayoutLoginFragment, fragViewPolicy, "View Policy");
transaction.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@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) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void addLoginAboutFragment (View v){
AfterLoginAboutFragment fragViewPolicy = new AfterLoginAboutFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.linearLayoutLoginFragment, fragViewPolicy, "View Policy");
transaction.commit();
}
public void addViewPolicyFragment (View v){
ViewPolicyFragment fragViewPolicy = new ViewPolicyFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.linearLayoutLoginFragment, fragViewPolicy, "View Policy");
transaction.commit();
}
public void addMyProfile (View v){
MyProfileFragment fragProfile = new MyProfileFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.linearLayoutLoginFragment, fragProfile, "My profile");
transaction.commit();
}
public void addMyPolicy (View v){
MyPolicyFragment fragMyPolicy = new MyPolicyFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.linearLayoutLoginFragment, fragMyPolicy, "My Policy");
transaction.commit();
}
public void addUploadFragment (View v){
UploadPhotoFragment fragUpload = new UploadPhotoFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.linearLayoutLoginFragment, fragUpload, "Uploaded Policy");
transaction.commit();
}
public void addUploadedFragment (View v){
UploadedPolicyFragment fragUploaded = new UploadedPolicyFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.linearLayoutLoginFragment, fragUploaded, "Uploaded Policy");
transaction.commit();
}
@Override
public void policyName(String name) {
Log.d("Luffy","Inside PolicyName AfterLogin");
ListPolicies fragList = new ListPolicies();
FragmentTransaction transaction = manager.beginTransaction();
fragList.polName(name);
transaction.replace(R.id.linearLayoutLoginFragment, fragList, "Uploaded Policy");
transaction.commit();
}
}
輸出:
10-28 14:00:18.400 2174-2174/onepiece.luffy.investaccui D/Luffy: Inside PolicyName AfterLogin
10-28 14:00:18.400 2174-2174/onepiece.luffy.investaccui D/Luffy: Posnull
10-28 14:00:18.400 2174-2174/onepiece.luffy.investaccui D/Luffy: Pos0
我想輸出:
10-28 14:00:18.400 2174-2174/onepiece.luffy.investaccui D/Luffy: Inside PolicyName AfterLogin
10-28 14:00:18.400 2174-2174/onepiece.luffy.investaccui D/Luffy: Pos0
,這樣我可以打電話給我Engineering_insurance的ListView
需要您的幫助傢伙... 在此先感謝
那麼會是怎樣的解決方案? –
我刪除了這一行'字符串polname = polName(polnames);'它的工作 謝謝... –
要麼處理空的情況下,要麼初始化polnames字符串。如果答案令人滿意,請考慮將其標記爲已接受,謝謝 – FrancescoC