_
您好stackoverflow的成员!
在“正常”活动中,我与ViewPager(VP)成功合作。
现在,我尝试对Android Wear应用程序使用几乎相同的方式。
在包含VP的MainActivity中,我遇到了类似的错误:
我找到了这个线程,并且能够通过更改导入+更改build.gradle来修复这些错误。
How to implement view pager in Android watch?
这些现在是我在MainActivity.java中导入的内容:
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.wearable.activity.WearableActivity;
import java.util.ArrayList;
import java.util.List;
这是MainActivity的其余部分:
public class MainActivity extends WearableActivity
{
ViewPager vp; // Variable for Viewpager
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Link to layout
ViewPager vp = findViewById(R.id.vp); // Link variable to ID
SetUpViewPager(vp); // Run viewpager method
}
public void SetUpViewPager(ViewPager viewpager)
{
MyViewPagerAdapter Adapter = new MyViewPagerAdapter(getFragmentManager());
Adapter.AddPageFragment(new Fragment1(), "Fragment1"); // Load page 1 inside of ViewPager
Adapter.AddPageFragment(new Fragment2(), "Fragment2"); // Load page 2 inside of ViewPager
viewpager.setAdapter(Adapter);
}
public class MyViewPagerAdapter extends android.support.v4.app.FragmentPagerAdapter
{
private List<Fragment> MyFragment = new ArrayList<>();
private List<String> MyPageTitle = new ArrayList<>();
public MyViewPagerAdapter(FragmentManager manager)
{
super(manager);
}
public void AddPageFragment(Fragment Frag, String Title)
{
MyFragment.add(Frag);
MyPageTitle.add(Title);
}
@Override
public Fragment getItem(int i)
{
return MyFragment.get(i);
}
@Nullable
@Override
public CharSequence getPageTitle(int position)
{
return MyPageTitle.get(position);
}
@Override
public int getCount()
{
return 2; // 2 pages total
}
}
}
奇怪的是说ViewPager vp; -> 从不使用字段'vp'!
Fragment1 :(除了2/2,Fragment2相同)
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment
{
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
View fragmentOne = inflater.inflate(R.layout.fragment1, container, false); // Link view and layout
return fragmentOne;
}
}
片段的XML文件只是每个1按钮的约束布局。 content_main.xml包含用于访问activity_main.xml的VP。 我在普通的智能手机设备上使用了相同的方法,并且运行正常。
问题在于,每次我在Wear Emulator上启动应用程序时,它都会立即崩溃。
这是Logcat所说的:
10-23 14:40:32.909 2203-2203/com.example.user.wearvp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.wearvp, PID: 2203
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.wearvp/com.example.user.wearvp.MainActivity}: java.lang.NullPointerException:
Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' 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.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at com.example.user.wearvp.MainActivity.SetUpViewPager(MainActivity.java:35)
at com.example.user.wearvp.MainActivity.onCreate(MainActivity.java:24)
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)
任何帮助将不胜感激!
PS:
如果我忘记提及任何重要的事情,请问一下,我会给您所需的信息!
最诚挚的问候
答案 0 :(得分:1)
堆栈跟踪指示没有vp
布局文件中定义的ID为activity_main.xml
的视图。我不确定您的意思:
content_main.xml包含用于访问activity_main.xml的VP。
如果在vp
布局中定义了content_main.xml
,则可能是您应该与setContentView()
(而不是当前的activity_main.xml
)一起使用的那个。
奇怪的是说ViewPager vp; ->永远不会使用字段'vp'!
局部变量vp
(在onCreate方法中定义)遮盖了实例变量vp
。如果需要它具有对象级作用域,只需在为其分配值时删除ViewPager
。