简体   繁体   中英

Intent not working.Second activity launching error

I am building an app in android studio.Upon launching the main activity when I click to see the details of the list item the second activity do not launch.I have written the intent in popuplistfragment.java in method called Onlistitemclick and specified the bundle in oncreate method in swipe views.Can you please help?

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

}
public class PopupListFragment extends ListFragment implements View.OnClickListener {

//String pageData[];
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    ArrayList<String> items = new ArrayList<String>();
    for (int i = 0, z = Cheeses.QUOTES.length ; i < z ; i++) {
        items.add(Cheeses.QUOTES[i]);
    }


    setListAdapter(new PopupAdapter(items));
}


@Override
public void onListItemClick(ListView listView, View v, int position, long   id) {
    String item = (String) listView.getItemAtPosition(position);

    Intent myIntent = new Intent(PopupListFragment.this.getActivity(), SwipeViews.class);
    myIntent.putExtra("key", item);
    startActivity(myIntent);

    // Show a toast if the user clicks on an item
   // Toast.makeText(getActivity(), "Item Clicked: " + item, Toast.LENGTH_SHORT).show();

}

@Override
public void onClick(View v) {

}





/**
 * A simple array adapter that creates a list of cheeses.
 */
class PopupAdapter extends ArrayAdapter<String> {

    PopupAdapter(ArrayList<String> items) {
        super(getActivity(), R.layout.list_item, android.R.id.text1, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup     container)     {
        // Let ArrayAdapter inflate the layout and set the text
        View view = super.getView(position, convertView, container);

        return view;
    }
}
}
public class SwipeViews extends Activity {
String pageData[];  //Stores the text to swipe.
LayoutInflater inflater;    //Used to create individual pages
ViewPager vp;   //Reference to class to swipe views

TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.swipe_view);
    //Get the data to be swiped through
    Bundle extras = getIntent().getExtras();
    if(extras!=null)
    {
        String item = extras.getString("key");
        textview.setText(item);
    }
    pageData=getResources().getStringArray(R.array.quotes);
    //get an inflater to be used to create single pages
    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //Reference ViewPager defined in activity
    vp=(ViewPager)findViewById(R.id.viewPager);
    //set the adapter that will create the individual pages
    vp.setAdapter(new MyPagesAdapter());
}
//Implement PagerAdapter Class to handle individual page creation
class MyPagesAdapter extends PagerAdapter {
    @Override
    public int getCount() {
        //Return total pages, here one for each data item
        return pageData.length;
    }
    //Create the given page (indicated by position)
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View page = inflater.inflate(R.layout.page, null);
        ((TextView)page.findViewById(R.id.textMessage)).setText(pageData[position]);
        //Add the page to the front of the queue
        ((ViewPager) container).addView(page, 0);
        return page;
    }
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        //See if object from instantiateItem is related to the given view
        //required by API
        return arg0==(View)arg1;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
        object=null;
    }
}

}

And here is the manifest file.

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".SplashScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <activity
        android:name=".MainActivity"
        android:label="SuccessQuotes" >
        <intent-filter>
            <action android:name="com.hacktechbd.successquotes.MAINACTIVITY" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SwipeViews"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.launcher" />

        </intent-filter>
    </activity>
</application>

</manifest>

Make sure that you are able to detect the click event. Use something like

 Log.v("check","item is clicked");

in ur code.

and Try to use Intent myIntent = new Intent(getActivity(), SwipeViews.class);

instead of Intent myIntent = new Intent(PopupListFragment.this.getActivity(), SwipeViews.class);

It may work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM