在使用CardScrollView时将卡定义为WebView:Google Glass

时间:2015-07-13 22:04:03

标签: android webview google-glass google-gdk cardscrollview

我正在尝试开发一个简单的Glass示例( Immersion ),其中每张卡都在全屏显示 WebView 在CardBuilder中。 Layout.EMBED_INSIDE)。

我正在回答这个问题: Google Glass Immersion Custom Layout without CardBuilder.Layout.EMBED_INSIDE但不清楚在哪里设置网络视图

我的代码如下:

public class MainActivity extends Activity
{
    private String cardPageOne = "file:///android_asset/one.html";
    private String cardPageTwo = "file:///android_asset/two.html";

    private CardScrollView mCardScrollView;
    private ExampleCardScrollAdapter mAdapter;
    private List<View> mCards;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        createCards();        // Setup webviews

        mCardScrollView = new CardScrollView(this);
        mAdapter = new ExampleCardScrollAdapter();
        mCardScrollView.setAdapter(mAdapter);
        mCardScrollView.activate();
        setContentView(mCardScrollView);
    }

    private void createCards() 
    {
        mCards = new ArrayList<>();

        WebView myWeb = (WebView)findViewById(R.id.webView);
        myWeb.getSettings().setJavaScriptEnabled(false);
        myWeb.loadUrl(filePath);

        mCards.add(myWeb);

        WebView myWeb2 = (WebView)findViewById(R.id.webView);
        myWeb2.getSettings().setJavaScriptEnabled(false);
        myWeb2.loadUrl(filePathAlt);

        mCards.add(myWeb2);
    }

    private class ExampleCardScrollAdapter extends CardScrollAdapter
    {
        @Override
        public int getPosition(Object item) 
        {
            return mCards.indexOf(item);
        }

        @Override
        public int getCount() 
        {
            return mCards.size();
        }

        @Override
        public Object getItem(int position) 
        {
            return mCards.get(position);
        }

        @Override
        public int getViewTypeCount()
        {
            // Is this correct? Each card will have a different (web)view
            return mCards.size();  
        }

        @Override
        public int getItemViewType(int position)
        {
            // Is this correct?
            return position;      
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
             return mCards.get(position);
        }
    }
}

最后,我的 webview.xml 如下:

<?xml version="1.0" encoding="utf-8"?>

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></WebView>

一旦代码到达,我就会收到 java.lang.NullPointerException

myWeb.getSettings().setJavaScriptEnabled(false);

我相信这是因为 myWeb 尚未设置为内容视图,因为我正在使用 mCardScrollView 。如果是,我在哪里以及如何设置我的 webviews

详细错误是:

07-13 17:24:05.337  18117-18117/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.testwebviewscroller, PID: 18117
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testwebviewscroller/com.testwebviewscroller.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2273)
        at android.app.ActivityThread.access$800(ActivityThread.java:138)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1236)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:149)
        at android.app.ActivityThread.main(ActivityThread.java:5045)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.testwebviewscroller.MainActivity$ExampleCardScrollAdapter.getView(MainActivity.java:101)
        at com.google.android.glass.widget.CardScrollView.loadView(CardScrollView.java:1351)
        at com.google.android.glass.widget.CardScrollView.addChildView(CardScrollView.java:1239)
        at com.google.android.glass.widget.CardScrollView.updateViewsInternal(CardScrollView.java:1131)
        at com.google.android.glass.widget.CardScrollView.updateViews(CardScrollView.java:1193)
        at com.google.android.glass.widget.CardScrollView.activate(CardScrollView.java:1589)
        at com.testwebviewscroller.MainActivity.onCreate(MainActivity.java:40)
        at android.app.Activity.performCreate(Activity.java:5235)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1089)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2188)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2273)
            at android.app.ActivityThread.access$800(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1236)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5045)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

如果您想将网页视图放入卡中,为什么不将自己的视图扩展到getView()覆盖方法中的每个项目?如果您不打算使用卡片提供的布局,您可以创建一个新布局并使用它。您的getView()看起来像这样:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = layoutInflater.inflate(R.layout.webview, parent, false);

    //enter code here to handle custom view loading

    return convertView;
}