While several people have asked questions about casting fragments to activities (and how that doesn't work), I couldn't find a fix that worked for me. I'm going through this tutorial, and have a main activity that contains two fragments. I'm probably missing something fairly obvious:
There error I get: Cannot cast from Fragment to FragmentListView (line18)
MainActivity:
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends Activity implements FragmentListView.OnSiteSelectedListener{
Fragmentwebview web;
FragmentListView list;
FragmentManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
list = (FragmentListView) manager.findFragmentById(R.id.fragment1);
list.setRefrence(this);
}
#Override
public void onSiteSelected(int i) {
// TODO Auto-generated method stub
web = (Fragmentwebview) manager.findFragmentById(R.id.fragment2);
// Check for landscape mode
if (web!= null && web.isVisible())
{
web.setNewPage(i);
}
else
{
Intent intent = new Intent(this , FragmentSupport.class);
intent.putExtra("index", i);
startActivity(intent);
}
}
}
Manifest:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.numeraid.MainActivity"
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="com.example.numeraid.FragmentSupport"
android:label="#string/title_activity_fragment_support" >
</activity>
<activity
android:name="com.example.numeraid.FragmentListView"
android:label="#string/title_activity_fragment_list_view" >
</activity>
<activity
android:name="com.example.numeraid.Fragmentwebview"
android:label="#string/title_activity_fragmentwebview" >
</activity>
</application>
</manifest>
The first fragment that's causing the error
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:name="com.example.NumerAid.list"
android:id="#+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
EDIT: Adding FragmentListView class
package com.example.numeraid;
import android.R.string;
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.ArrayAdapter;
import android.widget.ListView;
public class FragmentListView extends Fragment implements AdapterView.OnItemClickListener
{
ListView list;
String [] websites = {"Google","Facebook","Twitter","Xda-developer"};
OnSiteSelectedListener SiteListener;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragmentlistview, container, false);
list = (ListView) v.findViewById(R.id.list);
list.setAdapter(new ArrayAdapter<string>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1));
list.setOnItemClickListener(this);
return v;
}
#Override
public void onItemClick(AdapterView adapterView, View view, int position, long l)
{
SiteListener.onSiteSelected(position);
}
// Container Activity must implement this interface
public interface OnSiteSelectedListener {
public void onSiteSelected(int i);
}
public void setRefrence(OnSiteSelectedListener siteListener)
{
this.SiteListener = siteListener;
}
}
Thanks to #corsair for this answer, originally posted as a comment:
My guess is that your FragmentListView class extends android.support.v4.app.Fragment
instead of android.app.Fragment. If you want to use the support version, then you should
use getSupportFragmentManager() instead of getFragmentManager(). Otherwise, you need to
extend android.app.Fragment everywhere as done in the tutorial you are following.
Related
Here's my code for clicking the button from my class showInforActivity:
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(showInfoActivity.this, InformationFragment.class);
startActivity(intent);
}
});
Whenever I click the button I got error Unable to find explicit activity class {com.example.drawerapplication/com.example.drawerapplication.ui.information.InformationFragment}; have you declared this activity in your AndroidManifest.xml? and it loads the previous class and layout instead of loading this
InformationFragment class:
package com.example.drawerapplication.ui.information;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.example.drawerapplication.databinding.FragmentInformationBinding;
public class InformationFragment extends Fragment {
private FragmentInformationBinding binding;
private EditText name, address, age, contact;
private Button btnAdd, btnViewData;
DatabaseHelper mDatabaseHelper;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentInformationBinding.inflate(inflater, container, false);
View root = binding.getRoot();
name = binding.name;
address = binding.address;
age = binding.age;
contact = binding.contact;
btnAdd = binding.add;
btnViewData = binding.viewdata;
mDatabaseHelper = new DatabaseHelper(getActivity());
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (name.length() != 0 && address.length() != 0
&& age.length() != 0 && contact.length() != 0){
mDatabaseHelper.addData(name.getText().toString(), address.getText().toString(),
age.getText().toString().trim(), Long.valueOf(contact.getText().toString().trim()));
toastMessages("Data Successfully Inserted!");
}else {
toastMessages("Please complete all the requirements needed");
}
}
});
btnViewData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), ListDataActivity.class);
startActivity(intent);
}
});
return root;
}
#Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
private void toastMessages(String message){
Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
}
}
The layout of this InformationFragment is fragment_information:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.information.InformationFragment"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp">
...
Heres my **AndroidManifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.drawerapplication">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.DrawerApplication">
<activity android:name=".ui.information.ListDataActivity"/>
<activity android:name=".ui.information.showInfoActivity"/>
<activity
android:name=".MainActivity"
android:exported="true"
android:label="#string/app_name"
android:theme="#style/Theme.DrawerApplication.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I tried adding InformationFragment there however, it says Class referenced in the manifest, com.example.drawerapplication.InformationFragment, was not found in the project or the libraries
Please let me know what I'm missing or if you need more information. Thanks!
You cannot use an Intent to start/create a Fragment much like you would for an Activity. A Fragment actually lives inside an Activity i.e. its lifecycle is bound to an Activity. To start a Fragment you have to use the FragmentManager that is available with the Activity instance
This error is coming up because Android thinks that your InformationFragment is an Activity rather than a Fragment and hence it is asking you if you have declared it in your AndroidManifest.xml file because as the rule says, you need to have each Activity in your app declared inside the AndroidManifest.xml file
So, now you can use something like this, inside your showInfoActivity. Technically there are many solutions available as to what you could essentially do and that's why I linked you to all possible solutions rather than just writing them here
You can not use Intent for navigate to Fragment from a Activity.
For this reason you are getting this error. As, Intent receive 2 parameter Intent(FromActivity, ToActivity.class) . you are using fragment in 2nd parameter. We know that, when we create a activity then it it declared with AndroidManifest.xml file. Here, in your manifest file there are not any activity called InformationFragment.
You can learn Android Navigation Component to easily communicate with Activity and fragments.
So my goal is to put a barcode scanner onto my app to decode barcodes. I found a few answers using ZXing and Zbar but I either couldnt do it myself or the answer was a little out dated and half the code wouldnt work. I want to put the scanner onto a fragment and then have that 1st fragment send the decoded barcode to the 3rd fragment or results fragment. I added ZXing with intent then it prompted me to download the app and i dont want that.
Heres my MainActivity.java
package com.example.macdaddydarian.pricematch;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return FirstFragment.newInstance("Scanner");
case 1:
return SecondFragment.newInstance("Location");
case 2:
return ThirdFragment.newInstance("Results");
case 3:
return FourthFragment.newInstance("User");
default:
return ThirdFragment.newInstance("");
}
}
#Override
public int getCount() {
return 4;
}
}
}
And the FirstFragment.java that I want the scanner to go on
package com.example.macdaddydarian.pricematch;
import android.graphics.Typeface;
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.TextView;
public class FirstFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_barcode2, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
tv.setText(getArguments().getString("msg"));
//FONT!!!!!!!!!!!
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "Blenda Script.ttf");
tv.setTypeface(font);
return v;
}
public static FirstFragment newInstance(String text) {
FirstFragment f = new FirstFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
And the fragment_barcode2.xml that goes with the first fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1ab5ff" >
<TextView
android:id="#+id/tvFragFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="50dp"
android:text="TextView"
android:textColor="#FFFFFF"
android:layout_alignParentTop="true"
android:layout_marginTop="1dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/Scanbtn"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Okay and here is my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.macdaddydarian.pricematch" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
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>
And last the ThirdFragment.java i want it be sent to
package com.example.macdaddydarian.pricematch;
import android.graphics.Typeface;
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.TextView;
public class ThirdFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle
savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_results, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragThird);
tv.setText(getArguments().getString("msg"));
//FONT!!!!!!!!!!!
Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
"Blenda Script.ttf");
tv.setTypeface(font);
return v;
}
public static ThirdFragment newInstance(String text) {
ThirdFragment f = new ThirdFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
Google recently released a new Android Vision Api that provides barcode functionality.
You can view sample code of the API here: https://github.com/googlesamples/android-vision
Shifting between activities isn't working.
What the app does is it opens the android.xml file then after 5 secs open the main file
Here's the code:
android manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.android.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.android.android"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.ANDROID" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
android.java file:
package com.example.android;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class android extends Activity{
MediaPlayer sound1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.android);
sound1 = MediaPlayer.create(android.this , R.raw.android);
sound1.start();
Thread timer = new Thread()
{public void run()
{
try{
sleep(5000);
}
catch(InterruptedException ac){
ac.printStackTrace();
}
finally{
Intent openactivity = new Intent ("android.intent.action.MAIN");
startActivity(openactivity);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
sound1.release();
finish();
}
}
Mainactivity.java:
package com.example.android;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).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.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
android.xml file:(the first file that launches up)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background = "#drawable/android"
>
</LinearLayout>
Try this way
Intent openactivity = new Intent(android.this,MainActivity.class);
startActivity(openactivity);
And for more information go to starting-new-activity
First, always Capitalise 1st letter of class names (code convention),
and then write your intents like below:
Intent intent = new Intent(android.this, MainActivity.class);
startActivity(intent );
You can check Starting Another Activity in Android Developers website.
I can't for my life figure out why it keeps saying "unable to find Explicit Activity Class com.example.timer.beep. Have you declared the Activity in your Android Manifest?". As far as I can tell it has been declared and i have followed the convention of the last small app I built. My xml files are lowercase, my .java files are CamelCase. Does anyone have any other ideas as to why it would give me this error? The error is in the readyToBeep function in MainActivity.java.
Main Activity.java
package com.example.timer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void readyToBeep(View v)
{
Intent intent = new Intent(this, Beep.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Beep.java
package com.example.timer;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Beep extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buzz);
}
private Context Context;
Timer timer;
public Beep(){};
public Beep(Context Context) {
this.Context = Context;
timer = new Timer();
timer.schedule(new RemindTask(),
0, //initial delay
1*1500); //subsequent rate
}
class RemindTask extends TimerTask {
public void run() {
MediaPlayer mPlayer = MediaPlayer.create(Context, R.raw.beep);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.start();
}
}
public void main(String args[]) {
new Beep();
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.timer.MainActivity"
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="com.example.timer.buzz" >
</activity>
</application>
</manifest>
TIA
unable to find Explicit Activity Class com.example.timer.beep. Have you declared the Activity in your Android Manifest
There is no declaration of your Beep activity in your manifest. Add it :
<activity android:name="com.example.timer.Beep" >
</activity>
I am new to android ,i am trying to create a view pager from a grid view but i have a problem while clicking on the item in the grid view.
here is my code
ImageAdapter.java
import com.netvariant.qareeboon.android.R;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return mThumbIds[position];
}
public long getItemId(int position) {
return 0;
}
// Create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // If it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
ImagePagerAdapter .java
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ImagePagerAdapter extends PagerAdapter {
private List<ImageView> images;
public ImagePagerAdapter(List<ImageView> images) {
this.images = images;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = images.get(position);
container.addView(imageView);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(images.get(position));
}
#Override
public int getCount() {
return images.size();
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
}
// References to our images in res > drawable
public Integer[] mThumbIds = {
R.drawable.bg_logo, R.drawable.three,
R.drawable.four, R.drawable.two,
R.drawable.one, R.drawable.btn_about,
};
}
ImageViewPager .java
import java.util.ArrayList;
import java.util.List;
import com.netvariant.qareeboon.android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
public class ImageViewPager extends Activity {
// Declare Variable
int position;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the ViewPager
setTitle("ViewPager");
// Get the view from view_pager.xml
setContentView(R.layout.view_pager);
// Retrieve data from MainActivity on item click event
Intent p = getIntent();
position = p.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
List<ImageView> images = new ArrayList<ImageView>();
// Retrieve all the images
for (int i = 0; i < imageAdapter.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageAdapter.mThumbIds[i]);
imageView.setScaleType(ImageView.ScaleType.CENTER);
images.add(imageView);
}
// Set the images into ViewPager
ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
ViewPager viewpager = (ViewPager) findViewById(R.id.pager);
viewpager.setAdapter(pageradapter);
// Show images following the position
viewpager.setCurrentItem(position);
}
}
the activity
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.view.View;
public class InfoGraphicActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the GridView
setTitle("GridView");
// Get the view from grid_view.xml
setContentView(R.layout.grid_view);
// Set the images from ImageAdapter.java to GridView
GridView gridview = (GridView) findViewById(R.id.gridview);
// ViewPager viewPager =(ViewPager)findViewById(R.id.pager);
gridview.setAdapter(new ImageAdapter(this));
// Listening to GridView item click
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Launch ImageViewPager.java on selecting GridView Item
Intent i = new Intent(getApplicationContext(), ImageViewPager.class);
// Send the click position to ImageViewPager.java using intent
i.putExtra("id", position);
// Start ImageViewPager
startActivity(i);
}
});
}
// Not using options menu for this tutorial
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.info_graphic, menu);
return true;
}
}
you can see error log below:
08-21 11:03:38.289: E/AndroidRuntime(5196): FATAL EXCEPTION: main
08-21 11:03:38.289: E/AndroidRuntime(5196): android.content.ActivityNotFoundException: Unable to find explicit activity class
{com.netvariant.qareeboon.android/com.netvariant.android.qareeboon.ImageViewPager}; have you declared this activity in your AndroidManifest.xml?
08-21 11:03:38.289: E/AndroidRuntime(5196): at com.netvariant.android.qareeboon.InfoGraphicActivity$1.onItemClick(InfoGraphicActivity.java:40)
08-21 11:03:38.289: E/AndroidRuntime(5196): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
08-21 11:03:38.289: E/AndroidRuntime(5196): at android.widget.AbsListView.performItemClick(AbsListView.java:1065)
android manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.netvariant.qareeboon.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.netvariant.android.qareeboon.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.netvariant.android.qareeboon.MainMenu"
android:label="#string/title_activity_main_menu"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.EducateActivity"
android:label="#string/title_activity_consult"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.ConsultActivity"
android:label="#string/title_activity_consult"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.ContactUsActivity"
android:label="#string/title_activity_contact_us"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.InfoGraphicActivity"
android:label="#string/title_activity_info_graphic"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.qareeboon.android.SettingsActivity"
android:label="#string/title_activity_settings"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.SettingActivity"
android:label="#string/title_activity_setting"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.MyConsultation"
android:label="#string/title_activity_my_consultation"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.AboutUs"
android:label="#string/title_activity_about_us"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.ArticleActivity"
android:label="#string/title_activity_article"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.Comments"
android:label="#string/title_activity_add_comment"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.AddComment"
android:label="#string/title_activity_add_comment"
android:screenOrientation="portrait" >
</activity>
<activity android:name=".ImageViewPager" >
</activity>
</application>
</manifest>
Have a look at this in logcat:
08-21 11:03:38.289: E/AndroidRuntime(5196): android.content.ActivityNotFoundException: Unable to find explicit activity class
{com.netvariant.qareeboon.android/com.netvariant.android.qareeboon.ImageViewPager}; have you declared this activity in your AndroidManifest.xml?
You have not mentioned that activity in the AndroidManifest file of your application. Please do so.
<activity
android:name="com.netvariant.android.qareeboon.ImageViewPager">
</activity>
Declare your Activity in Manifest file please
You haven't defined the new Activity in your manifest. Which ever Activity you are launching, its name needs to be added between your Application tags like this:
<activity
android:name=".Activity_Class_Name" />