Moving from One Fragment to another activity Using

2019-09-08 04:20发布

问题:

I am using navigation drawer which moves from fragment to another when on clicked on the menu_item in it. In one of my fragments i have a listview. I have to move to another ACTIVITY and not FRAGMENT when clicked on one of the list view item here is the code of both the things the Activity as well as fragment. I have used intent but still it gives error on the parameters of intent.

"One" Fragment package com.navigate2;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View; 
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;


 /**
  * A simple {@link Fragment} subclass.
  */
  public class One extends Fragment {


  public One() {
      // Required empty public constructor
  }


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_one, container, false);

    ListView listView = (ListView)listView.findViewById(R.id.mobile_list2);
    listView.setBackgroundColor(Color.WHITE);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            switch (position)
            {
                case 0:
                    Intent intent = new Intent(One.this,AndroidClass.class);
                    startActivity(intent);
            }
        }
    });
}


}

"AndroidClass" Activity

   package com.navigate2;

  import android.os.Bundle;
  import android.os.PersistableBundle;
  import android.support.v7.app.AppCompatActivity;

  /**
  * Created by Nathani Aliakbar on 06-01-2016.
  */
  public class AndroidClass extends AppCompatActivity
  {
   @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.textview);
  }
  }

回答1:

change onCreateView()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview = inflater.inflate(R.layout.fragment_one, container, false);

ListView listView = (ListView)   rootview.findViewById(R.id.mobile_list2);

listView.setBackgroundColor(Color.WHITE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch (position)
        {
            case 0:
                Intent intent = new Intent(One.this,AndroidClass.class);
                startActivity(intent);
        }
    }
});

 return rootview;
}


回答2:

Inside fragment onCreateView you are returning immediately before adding onItemCliclListener:

return inflater.inflate(R.layout.fragment_one, container, false);

Try like this:

View view = inflater.inflate(R.layout.fragment_one, container, false);
ListView listView = (ListView)listView.findViewById(R.id.mobile_list2);
listView.setBackgroundColor(Color.WHITE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch (position)
        {
            case 0:
                Intent intent = new Intent(One.this,AndroidClass.class);
                startActivity(intent);
        }
    }
});
return view;


回答3:

use

  View view = inflater.inflate(R.layout.fragment_one, container, false);
  ListView listView = (ListView)listView.findViewById(R.id.mobile_list2);
 Intent intent = new Intent(getActivity(),AndroidClass.class);
                    startActivity(intent);


return view;


回答4:

View view = inflater.inflate(R.layout.fragment_one, container, false);
ListView listView = (ListView)view.findViewById(R.id.mobile_list2);
listView.setBackgroundColor(Color.WHITE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    switch (position)
    {
        case 0:
            Intent intent = new Intent(One.this,AndroidClass.class);
            startActivity(intent);
    }
}
});
return view;