私はアンドロイドウェアにはかなり新しいです。私はSampleGridPagerAdapterサンプルを見て、このコードを生成しようとしました。私はまた、スタックオーバーフローからの同様の質問(What is the correct way to implement a GridViewPager on Android Wear?)を審査し、手順に従った。しかし、私はnullpointExceptionを取得しています。私は見た両方のサンプルで余分なものが見つかりました。私は自分のコードとlogcatトレースを添付しています。前もって感謝します。アンドロイドウェアの2つの異なるレイアウト(2つのXMLファイル)の間を水平にナビゲート
コード:
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)