2016-10-26 119 views
0

任務:我創建了一個片段抽屜,我有我的textview文本「我的名字」正好在我的圖像下方,然後有nav_row項目,所以我想要做的是當這個文字點擊它應該從MainActivity切換活動profileinfo和我的WebView創建,其profileinfo該活動將載入我的HTML文件...所以我所做的是在這裏 - >Android從本地文件夾加載html文件到webview

這是我activity_main XML

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:id="@+id/container_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <include 
       android:id="@+id/toolbar" 
       layout="@layout/toolbar" /> 
     </LinearLayout> 

     <FrameLayout 
      android:id="@+id/container_body" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

     <webview xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/aboutYou" 
      android:orientation="vertical" android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
     </webview> 
    </LinearLayout> 


    <fragment 
     android:id="@+id/fragment_navigation_drawer" 
     android:name="com.medmainfomatix.mdesign.FragmentDrawer" 
     android:layout_width="@dimen/nav_drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:layout="@layout/fragment_navigation_drawer" 
     tools:layout="@layout/fragment_navigation_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

webview xml is here - >

<?xml version="1.0" encoding="utf-8"?> 
<webview xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/aboutYou" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</webview> 

profileinfo活動

public class profileinfo extends AppCompatActivity{ 
    private WebView aboutYou; 


    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.webview); 
     aboutYou = (WebView) findViewById(R.id.aboutYou); 
     aboutYou.loadUrl("file:///android_assets/profile_info.html"); 
    } 
} 

這是我的MainActivity類別

公共類MainActivity擴展AppCompatActivity實現FragmentDrawer.FragmentDrawerListener {

private static String TAG = MainActivity.class.getSimpleName(); 
TextView pname; 
private Toolbar mToolbar; 

private FragmentDrawer drawerFragment; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mToolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(mToolbar); 
    getSupportActionBar().setDisplayShowHomeEnabled(true); 
    drawerFragment = (FragmentDrawer) 
      getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer); 
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar); 
    drawerFragment.setDrawerListener(this); 

    displayView(0); 
    pname = (TextView) findViewById(R.id.pname); 


    pname.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent inent = new Intent(MainActivity.this,profileinfo.class); 

      startActivity(inent); 
     } 
    }); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.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(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show(); 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onDrawerItemSelected(View view, int position) { 
    displayView(position); 
} 
private void displayView(int position) { 
    Fragment fragment = null; 
    String title = getString(R.string.app_name); 
    switch (position) { 
     case 0: 
      fragment = new HomeFragment(); 
      title = getString(R.string.title_home); 
      break; 
     case 1: 
      fragment = new FriendsFragment(); 
      title = getString(R.string.title_friends); 
      break; 
     case 2: 
      fragment = new MessagesFragment(); 
      title = getString(R.string.title_messages); 
      break; 
     default: 
      break; 
    } 

    if (fragment != null) { 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.container_body, fragment); 
     fragmentTransaction.commit(); 

     // set the toolbar title 
     getSupportActionBar().setTitle(title); 
    } 
} 

}

這裏是我的TextView信息這個XML片段導航drawe r

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white"> 


    <RelativeLayout 
     android:id="@+id/nav_header_container" 
     android:layout_width="match_parent" 
     android:layout_height="140dp" 
     android:layout_alignParentTop="true" 
     android:background="@color/colorPrimary"> 

     <ImageView 
      android:layout_width="70dp" 
      android:layout_height="70dp" 
      android:src="@drawable/profile" 
      android:scaleType="fitCenter" 
      android:layout_centerInParent="true" /> 

    </RelativeLayout> 


    <android.support.v7.widget.RecyclerView 
     android:id="@+id/drawerList" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/nav_header_container" 
     android:layout_marginTop="15dp" /> 

    <TextView 
     android:id="@+id/pname" 
     android:textStyle="bold" 
     android:layout_marginTop="110dp" 
     android:gravity="center" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/profile_name" 
     android:clickable="true" 
     android:onClick="onClick"/> 
</RelativeLayout> 

不要擔心其他類和xml請看看我錯了。

我正在此 - > AndroidRuntime:致命異常:主 工藝:com.medmainfomatix.mdesign,PID:27497 了java.lang.RuntimeException:無法啓動活動ComponentInfo {com.medmainfomatix.mdesign/COM .medmainfomatix.mdesign.profileinfo}:android.view.InflateException:二進制XML文件行#2:錯誤膨脹類webview at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2455) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2519) at android.app.ActivityThread.access $ 800(ActivityThread.java:162) 在android.app.ActivityThread $ h.handleMessage(ActivityThread.java:1412) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:189) at java.lang.reflect.Method.invoke(Method.java:372) at android.app.ActivityThread.main(ActivityThread.java:5532) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:950) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 引起:android.view.InflateException:二進制XML文件行#2:錯誤膨脹類w ebview 在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757) 在android.view.LayoutInflater.inflate(LayoutInflater.java:482) 在android.view.LayoutInflater.inflate(LayoutInflater.java:414) 在android.support.android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 處的android.view.LayoutInflater.inflate(LayoutInflater.java:365) 。v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at com.medmainfomatix.mdesign.profileinfo.onCreate(profileinfo.java:16) at android.app.Activity.performCreate(Activity.java:5966) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2519) 在機器人。 app.ActivityThread.access $ 800(ActivityThread.java:162) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1412) at andro在android.app.ActivityThread.main(ActivityThread.java:5532) at java.android.os.Looper.loop(Looper.java:189) (id.os.Handler.dispatchMessage(Handler.java:106) )。 lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:950 ) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 引起:java.lang.ClassNotFoundException:在路徑中找不到類「android.view.webview」:DexPathList [[zip文件「/data/app/com.medmainfomatix.mdesign-2/base.apk"],nativeLibraryDirectories=[/vendor/lib,/ system/lib]] 在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) 在java.lang.ClassLoader.loadClass(ClassLoader.java:511) 在java.lang.ClassLoader.loadClass(ClassLoader.java:469) 在android.view.LayoutInflater.createView(LayoutInflater.java:571) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:665) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java: 65) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 在android.view.LayoutInflater.inflate(LayoutInflater.java:482) 在android.view.LayoutInflater.inflate(LayoutInflater.java:414) 在android.view.LayoutInflater.inflate(LayoutInflater.java:365) 在android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at com.medmainfomatix.mdesign.profileinfo.onCreate( profileinfo.java:16) at android.app.Activity.performCreate(Activity.java:5966) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2519) 在android.app.ActivityThread.access $ 800(ActivityThread.java:162) 在android.app.ActivityThread $ h.handleMessage(ActivityThread.java:1412) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:189) android.app.ActivityThread.main(ActivityThread.java:5532) at java.lang.reflect.Method.invoke(Native Method) at java.lang.r eflect.Method。調用(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:950) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) Suppressed:java.lang.ClassNotFoundException:未在路徑中找到類「android.view.webview」:DexPathList [[dex file] /data/data/com.medmainfomatix.mdesign/files/instant-run/dex/slice -support-annotations-24.2.1_0a328bfef112abe9401e5ad651d5b475868a0fa2-classes.dex「,dex file」/data/data/com.medmainfomatix.mdesign/files/instant-run/dex/slice-slice_9-classes.dex「,dex file」/ data /data/com.medmainfomatix.mdesign/files/instant-run/dex/slice-slice_8-classes.dex「,dex file」/data/data/com.medmainfomatix.mdesign/files/instant-run/dex/slice- slice_7-classes.dex「,dex文件」/data/data/com.medmainfomatix.mdesign/files/instant-run/dex/slice-slice_6-classes.dex「,dex文件「/data/data/com.medmainfomatix.mdesign/files/instant-run/dex/slice-slice_5-classes.dex」,dex file「/data/data/com.medmainfomatix.mdesign/files/instant-run/dex /slice-slice_4-classes.dex「,dex文件」/data/data/com.medmainfomatix.mdesign/files/instant-run/dex/slice-slice_3-classes.dex「,dex文件」/ data/data/com .medmainfomatix.mdesign/files/instant-run/dex/slice-slice_2-classes.dex「,dex file」/data/data/com.medmainfomatix.mdesign/files/instant-run/dex/slice-slice_1-classes。 dex「,dex文件」/data/data/com.medmainfomatix.mdesign/files/instant-run/dex/slice-slice_0-classes.dex「,dex文件」/data/data/com.medmainfomatix.mdesign/files/即時運行/ DEX /切片internal_impl-24.2.1_c5d6f1d87c5057b6eaca56a24d54d014044b8659-CLA

+0

您的問題是什麼? – earthw0rmjim

+0

我編輯併發布了錯誤看看 – SameerKhan1406

+0

,這是在fragment_navigation_drawer xml中提到的textview,當我點擊它時,應該開始配置文件信息活動,然後加載我的html – SameerKhan1406

回答

1

試試這個
它爲我

WebView webView = (WebView)findViewById(R.id.webView1); 
webview.loadUrl("file:///android_asset/file.html"); 

使用(file:///)完整地址

+0

做到這一點已經不適合我,問題來自可點擊在fragment_navigation_drawer中的textview xml – SameerKhan1406

+0

看看這個使用這個代碼---> http://www.androidhive.info/2015/04/android-getting-started-with-material-design/ 我唯一的東西添加是文本下面的抽屜中的配置文件圖像應該加載我的HTML文件在同一屏幕上的活動或不同的方式是好的 – SameerKhan1406

+0

@ SameerKhan1406 Iam不知道,但你必須嘗試.setselected(true) – Sukhmanpreet

相關問題