Autore Topic: WebView e Navigation Drawer  (Letto 590 volte)

Offline Ceciud

  • Nuovo arrivato
  • *
  • Post: 10
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Optimus G e Samsung Galaxy S3
  • Sistema operativo:
    Mac OS X
WebView e Navigation Drawer
« il: 07 Giugno 2014, 15:39:09 CEST »
0
Ciao ragazzi,
premetto che non sono molto capace e quindi dovrete avere pazienza. Sto cercando di inserire una webview ma mi crasha l'app appena parte: ecco il logcat:

Codice (Java): [Seleziona]
06-07 15:33:24.705: D/dalvikvm(611): Not late-enabling CheckJNI (already on)
06-07 15:33:25.615: E/Trace(611): error opening trace file: No such file or directory (2)
06-07 15:33:26.525: D/AndroidRuntime(611): Shutting down VM
06-07 15:33:26.525: W/dalvikvm(611): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
06-07 15:33:26.565: E/AndroidRuntime(611): FATAL EXCEPTION: main
06-07 15:33:26.565: E/AndroidRuntime(611): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.navigationdrawer/com.example.navigationdrawer.MainActivity}: java.lang.NullPointerException
06-07 15:33:26.565: E/AndroidRuntime(611):      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-07 15:33:26.565: E/AndroidRuntime(611):      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-07 15:33:26.565: E/AndroidRuntime(611):      at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-07 15:33:26.565: E/AndroidRuntime(611):      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-07 15:33:26.565: E/AndroidRuntime(611):      at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 15:33:26.565: E/AndroidRuntime(611):      at android.os.Looper.loop(Looper.java:137)
06-07 15:33:26.565: E/AndroidRuntime(611):      at android.app.ActivityThread.main(ActivityThread.java:4745)
06-07 15:33:26.565: E/AndroidRuntime(611):      at java.lang.reflect.Method.invokeNative(Native Method)
06-07 15:33:26.565: E/AndroidRuntime(611):      at java.lang.reflect.Method.invoke(Method.java:511)
06-07 15:33:26.565: E/AndroidRuntime(611):      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-07 15:33:26.565: E/AndroidRuntime(611):      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-07 15:33:26.565: E/AndroidRuntime(611):      at dalvik.system.NativeStart.main(Native Method)
06-07 15:33:26.565: E/AndroidRuntime(611): Caused by: java.lang.NullPointerException
06-07 15:33:26.565: E/AndroidRuntime(611):      at com.example.navigationdrawer.Page0.onCreateView(Page0.java:18)
06-07 15:33:26.565: E/AndroidRuntime(611):      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
06-07 15:33:26.565: E/AndroidRuntime(611):      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
06-07 15:33:26.565: E/AndroidRuntime(611):      at android.app.BackStackRecord.run(BackStackRecord.java:635)
06-07 15:33:26.565: E/AndroidRuntime(611):      at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
06-07 15:33:26.565: E/AndroidRuntime(611):      at android.app.Activity.performStart(Activity.java:5017)
06-07 15:33:26.565: E/AndroidRuntime(611):      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
06-07 15:33:26.565: E/AndroidRuntime(611):      ... 11 more
06-07 15:33:28.475: I/Process(611): Sending signal. PID: 611 SIG: 9

Di seguito l'activity:

Codice (Java): [Seleziona]
package com.example.navigationdrawer;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

public class Page0 extends Fragment {
       
         @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
         View rootView = inflater.inflate(R.layout.page0, container, false);
       //carichiamo la pagina web
       
                      WebView myWebView = (WebView) getView().findViewById(R.id.webview);
     
                      myWebView.loadUrl("http://www.google.it/");

       

       
       
         return rootView;
     }
 
}

MainActivity:
Codice (Java): [Seleziona]
package com.example.navigationdrawer;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;



public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    @SuppressWarnings("unused")
        private CharSequence mTitle;
    private String[] mGalaxyTitles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mTitle = mDrawerTitle = getTitle();
        mGalaxyTitles = getResources().getStringArray(R.array.items_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

       
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.list_item, mGalaxyTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

       
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

       
       
       
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  
                mDrawerLayout,        
                R.drawable.ic_drawer,  
                R.string.drawer_open,  
                R.string.drawer_close  
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

   
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
             
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
                return false;
       
       
       
    }

       

   

   
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch(position){
        case 0:
                selectItem(position);
        case 1:
                selectItem(position);
        case 2:
                selectItem(position);
    }
    }    
}
       
       
        private void selectItem(int position) {
                switch(position){
       
        case 0:
               
                Fragment fragment = new Page0();
            Bundle args = new Bundle();
            fragment.setArguments(args);

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.main, fragment).commit();

       
        mDrawerList.setItemChecked(position, true);
        setTitle(mGalaxyTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
        return;
       
        case 1:
        Fragment fragment1 = new Page1();
        Bundle args1 = new Bundle();
        fragment1.setArguments(args1);
        FragmentManager fragmentManager1 = getFragmentManager();
        fragmentManager1.beginTransaction().replace(R.id.main, fragment1).commit();

       
        mDrawerList.setItemChecked(position, true);
        setTitle(mGalaxyTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
        return;
       
        case 2:
            Fragment fragment2 = new Page2();
            Bundle args2 = new Bundle();
            fragment2.setArguments(args2);
            FragmentManager fragmentManager2 = getFragmentManager();
            fragmentManager2.beginTransaction().replace(R.id.main, fragment2).commit();

           
            mDrawerList.setItemChecked(position, true);
            setTitle(mGalaxyTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
            return;
    }
         }
    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mDrawerTitle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
       
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
       
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

   
}

file page0.xml:
Codice (XML): [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:id="@+id/page0"
   android:layout_height="match_parent" >

 <WebView
   android:id="@+id/webview"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
        />
   

</FrameLayout>

file main.xml:
Codice (XML): [Seleziona]
<android.support.v4.widget.DrawerLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/drawer_layout"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   
   
    <FrameLayout
       android:id="@+id/main"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />


     <ListView
       android:id="@+id/left_drawer"
       android:layout_width="240dp"
       android:layout_height="match_parent"
       android:layout_gravity="start"
       android:background="#F3F3F4"
       android:choiceMode="singleChoice"
       android:divider="@android:color/transparent"
       android:dividerHeight="0.5dp" ></ListView>
</android.support.v4.widget.DrawerLayout>

ed infine il Manifest:
Codice (XML): [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.navigationdrawer"
   android:versionCode="1"
   android:versionName="1.0" >

    <uses-sdk
       android:minSdkVersion="15"
       android:targetSdkVersion="18" />
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <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.navigationdrawer.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="Page0"></activity>
        <activity android:name="Page1"></activity>
    </application>

</manifest>

Grazie a chiunque mi aiuti..

Offline Ohmnibus

  • Utente senior
  • ****
  • Post: 716
  • Respect: +151
    • Github
    • Google+
    • @ohmnibus
    • Mostra profilo
    • Lords of Knowledge GdR
  • Dispositivo Android:
    Huawei P9 Lite
  • Play Store ID:
    Ohmnibus
  • Sistema operativo:
    Windows 7 x64
Re:WebView e Navigation Drawer
« Risposta #1 il: 04 Luglio 2014, 19:34:14 CEST »
0
Invece di

Codice (Java): [Seleziona]
WebView myWebView = (WebView) getView().findViewById(R.id.webview);
prova

Codice (Java): [Seleziona]
WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
Ohmnibus
Le mie app su Play Store

È stata trovata una soluzione al tuo problema? Evidenzia il post più utile premendo . È un ottimo modo per ringraziare chi ti ha aiutato.

Offline Ceciud

  • Nuovo arrivato
  • *
  • Post: 10
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Optimus G e Samsung Galaxy S3
  • Sistema operativo:
    Mac OS X
Re:WebView e Navigation Drawer
« Risposta #2 il: 07 Luglio 2014, 14:18:17 CEST »
0
Grazie mille!