2012-02-22 15 views
18

Im utilizzando un frammento che dovrebbe visualizzare una visualizzazione Web. Quando provo a istanziarlo dalla classe che lo usa ricevo il seguente avviso nel mio logcat.android - non è mai necessario aggiungere frammenti al manifest

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 solo imparare a usare i frammenti e Ive mai provato dichiarandoli nel mio manifesto e io havent visto da nessuna parte che ti dice di farlo.

Ecco la classe WebFrag.

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: aggiunta di WebFrag come attività per il manifesto provoca il seguente errore

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: Ecco il fragmentactivity principale dove Im cercando di usare la mia classe

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(); 
    } 


    } 
    }); 
} 
} 

risposta

29

No don aggiungilo al tuo manifest. Non hai mai bisogno di aggiungere frammenti al tuo manifest.

Si crea un Intento da qualche parte per avviare WebActivity? Com'è portato sullo schermo, probabilmente è il tuo problema.

EDIT

Questo è il problema:

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

Non puoi iniziare una Frammento come attività, si dovrà avvolgere il frammento in un'attività che si estende FragmentActivity

+0

Sì, ero abbastanza sicuro che non lo fai. Ill postare la mia altra classe in cui pensavo di usarla. –

+0

Con il wrapping del frammento intendi la creazione di una frammentattività che estende la mia classe WebFrag? –

+1

No dovresti creare una "WebActivity" che estenda FragmentActivity. Quindi nel layout xml per WebActivity aggiungerei il tuo frammento, ad esempio Blundell

Problemi correlati