在android中,在两个不同的布局(2个xml文件)之间水平导航

时间:2016-05-25 18:00:02

标签: java android android-studio wear-os

我对安装很新。我查看了SampleGridPagerAdapter示例并尝试生成此代码。我还从堆栈溢出中查看了一个类似的问题(What is the correct way to implement a GridViewPager on Android Wear?),然后按照过程进行操作。但我得到一个nullpointException。我发现在我看过的两个样本中都添加了额外的内容。我附加了我的代码和logcat跟踪。提前谢谢。

代码:

public class MainActivity extends Activity {

    private BoxInsetLayout mContainerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final DotsPageIndicator mPageIndicator;
        final GridViewPager  mViewPager;
        mPageIndicator = (DotsPageIndicator)              findViewById(R.id.page_indicator);
        mViewPager = (GridViewPager)findViewById(R.id.pager);
        mViewPager.setAdapter(new MyGridPagerAdapter());
        mPageIndicator.setPager(mViewPager);
    }

    private class MyGridPagerAdapter extends GridPagerAdapter{

        @Override
        public int getRowCount() {
            return 2;
        }

        @Override
        public int getColumnCount(int i) {
            return 2;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int row, int col) {
            View view = null;
            view =   LayoutInflater.from(getApplicationContext()).inflate(R.layout.UI1,container,false);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int row, int col, Object view)     {
            container.removeView((View)view);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
    }
}

Logcat跟踪是:

com.example.wearable E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.wearable, PID: 2238
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wearable/com.example.wearable.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.wearable.view.GridViewPager.setAdapter(android.support.wearable.view.GridPagerAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.wearable.view.GridViewPager.setAdapter(android.support.wearable.view.GridPagerAdapter)' on a null object reference
at com.example.wearable.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5422) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:0)

查看堆栈跟踪,看来mViewPager在您尝试在其上调用setAdapter时为空。这意味着findViewById并未在您的布局中找到它,这可能意味着R.layout.activity_main并未像您期望的那样被充气。

这是否与您在调试器中单步调用onCreate时看到的内容相符?