23

Im using a fragment that is supposed to display a webview. When I try to instantiate it from the class that uses it I get the following warning in my logcat.

02-21 23:26:46.843: W/System.err(32468): android.content.ActivityNotFoundException: Unable   to find explicit activity class {get.scanner/get.scanner.WebFrag}; have you declared this activity in your AndroidManifest.xml?

Im just learning how to use fragments and Ive never tried declaring them in my manifest and I havent seen anywhere telling you to do so.

Heres the WebFrag class.

public class WebFrag extends Fragment{
private WebView viewer = null;

// if we weren't just using the compat library, we could use WebViewFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    viewer = (WebView) inflater
            .inflate(R.layout.webview, container, false);
    WebSettings settings = viewer.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setDefaultZoom(ZoomDensity.FAR);

    return viewer;
 }

 @Override
 public void onPause() {
   if (viewer != null) {
       viewer.onPause();
   }
   super.onPause();
 }

 @Override
 public void onResume() {
    super.onResume();
    if (viewer != null) {
        viewer.onResume();
    }
 }

 public void updateUrl(String newUrl) {
    if (viewer != null) {
        viewer.loadUrl(newUrl);
    }
}
}

EDIT: adding WebFrag as an activity to the manifest causes the following error

02-22 00:17:55.711: E/AndroidRuntime(2524): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{get.scanner/get.scanner.WebFrag}: java.lang.ClassCastException: get.scanner.WebFrag

EDIT: Heres the main fragmentactivity where Im trying to use my class

public class GetScannerActivity extends FragmentActivity {

private String mUrl = "http://www.yahoo.com/";

Button scanButton;
Button paint;
Button compTrans;
String yurl = "http://www.yahoo.com/";

@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    compTrans = (Button) findViewById(R.id.checkCurrentDeals);
    compTrans.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
    WebFrag viewer = (WebFrag) getSupportFragmentManager()
            .findFragmentById(R.id.web_frag);

    try{
    if (viewer == null || !viewer.isInLayout()) {
        Intent showContent = new Intent(getApplicationContext(),
                WebFrag.class);
        showContent.setData(Uri.parse(yurl));
        try{
        startActivity(showContent);
        }catch(ActivityNotFoundException e){
            e.printStackTrace();
        }
    } else {
        viewer.updateUrl(yurl);
    }   
    }catch(Exception e){
        e.printStackTrace();
    }


    }
    });
 }
}
James andresakis
  • 5,335
  • 9
  • 53
  • 88

2 Answers2

36

No don't add it to your manifest. You never need to add fragments to your manifest.

Do you create an Intent somewhere to start the WebActivity? How is it brought to the screen, that is probably where your problem lies.

EDIT

This is your problem:

 Intent showContent = new Intent(getApplicationContext(),
            WebFrag.class);
 startActivity(showContent);

You can't start a Fragment as an Activity, you'll have to wrap the fragment in an Activity that extends FragmentActivity

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • Yeah i was pretty sure you dont do that. Ill post my other class where I thought I was using it. – James andresakis Feb 22 '12 at 08:31
  • By wrapping the fragment do you mean creating a fragmentactivity that extends my WebFrag class? – James andresakis Feb 22 '12 at 08:41
  • 1
    No you would create a 'WebActivity' that extends FragmentActivity. Then in the xml layout for WebActivity you would add your fragment i.e. – Blundell Feb 22 '12 at 08:43
  • Thats what Im doing now but for somereason it never loads the webfragment. Thats why I tried to implement the button. Should the fragment just load once declared in the layout and then the layout set in setContentView()? – James andresakis Feb 22 '12 at 08:48
  • 1
    Yeah, add some `Log.d("","");` lines to your fragment to see if it is loading but not displaying. – Blundell Feb 22 '12 at 08:50
  • Cool so I finally got it to work but I had to add a innerclass that extended webclient. Without that it would just load the fragment completely like it was a browser. Thanks for helping me with that I spent all day messing with it :p – James andresakis Feb 22 '12 at 09:00
  • How do I add the fragment to the XML file? – praxmon Jan 28 '15 at 13:43
0

if you want to go back from activity to Fragment try this

proToolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

//Note: you must give an id to the Main layout of your activity e.g : searchVehicles

            ((ConstraintLayout) findViewById(R.id.searchVehicles)).removeAllViews();
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragment_nearby_vehicles fnv = new fragment_nearby_vehicles();
            fragmentTransaction.add(R.id.searchVehicles, fnv).commit();
        }
    });
Jatin
  • 17
  • 2