Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - do you ever have to add fragments to the manifest

Tags:

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();     }       }     });  } } 
like image 414
James andresakis Avatar asked Feb 22 '12 07:02

James andresakis


People also ask

Are fragments still used in Android?

Using the support library, fragments are supported back to all relevant Android versions. Fragments encapsulate views and logic so that it is easier to reuse within activities.


1 Answers

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

like image 102
Blundell Avatar answered Oct 07 '22 12:10

Blundell