编写此代码后,应用程序崩溃。错误在哪里?

时间:2019-02-10 18:26:55

标签: javascript java android android-studio android-fragments

我想编写一个简单的柜台应用程序。我想稍后再实现其他一些工具。因此,我选择了“ BottomNavigationView”。在我的片段之一中,我想实现计数器。布局是正确的并且可以工作,但是实际上增加/减少类HomeActivity中的数字的代码在启动时崩溃了我的应用程序。也许有人知道答案? :D

我从HomeActivity中删除了代码,以检查崩溃是否来自新代码,并且一切正常。因此,计数器代码有误。

公共类HomeFragment扩展了片段{

private TextView counterText;
private Button plusButton;
private Button minusButton;
private Button resetButton;
private int counter;

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

    counterText = getView().findViewById(R.id.counterText);
    minusButton = getView().findViewById(R.id.minusButton);
    minusButton.setOnClickListener(clickListener);
    plusButton = getView().findViewById(R.id.plusButton);
    plusButton.setOnClickListener(clickListener);
    resetButton = getView().findViewById(R.id.resetButton);
    resetButton.setOnClickListener(clickListener);

    initCounter();

}

private View.OnClickListener clickListener = new View.OnClickListener() {
    @Override
    public void onClick(@org.jetbrains.annotations.NotNull View view) {
        switch (view.getId()) {
            case R.id.minusButton:
                minusCounter();
                break;
            case R.id.plusButton:
                plusCounter();
                break;
            case R.id.resetButton:
                initCounter();
                break;
        }
    }
};

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return  inflater.inflate(R.layout.activity_home, container, false);



}

private void initCounter() {
    counter = 0;
    counterText.setText(counter + "");
}


private void minusCounter() {
    counter--;
    counterText.setText(counter + "");
}

private void plusCounter() {
    counter++;
    counterText.setText(counter + "");
}

}

这是Logcat:

2019-02-10 19:35:22.065 15241-15241/? E/Zygote: v2
2019-02-10 19:35:22.065 15241-15241/? I/libpersona: KNOX_SDCARD checking this for 10229
2019-02-10 19:35:22.065 15241-15241/? I/libpersona: KNOX_SDCARD not a persona
2019-02-10 19:35:22.066 15241-15241/? E/Zygote: accessInfo : 0
2019-02-10 19:35:22.067 15241-15241/? W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2],  Con:u:r:zygote:s0 RAM:SEPF_SECMOBILE_7.0_0009, [-1 -1 -1 -1 0 1]
2019-02-10 19:35:22.068 15241-15241/? I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=com.example.counter1 
2019-02-10 19:35:22.072 15241-15241/? I/art: Late-enabling -Xcheck:jni
2019-02-10 19:35:22.097 15241-15241/? D/TimaKeyStoreProvider: TimaKeyStore is not enabled: cannot add TimaSignature Service and generateKeyPair Service
2019-02-10 19:35:22.397 15241-15241/com.example.counter1 W/System: ClassLoader referenced unknown path: /data/app/com.example.counter1-2/lib/arm
2019-02-10 19:35:22.433 15241-15241/com.example.counter1 I/InstantRun: starting instant run server: is main process
2019-02-10 19:35:22.579 15241-15241/com.example.counter1 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.support.v4.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2341)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.support.v7.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:62)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at java.lang.Object java.lang.reflect.Constructor.newInstance0!(java.lang.Object[]) (Constructor.java:-2)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:430)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:652)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:794)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:734)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:865)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:828)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:525)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:427)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:378)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at android.view.ViewGroup android.support.v7.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.support.v7.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.support.v7.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void com.example.counter1.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:16)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6955)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1126)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2927)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:3045)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.app.ActivityThread.-wrap14(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1642)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.os.Looper.loop() (Looper.java:154)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6776)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:1496)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:1386)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art: Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.example.counter1-2/base.apk", zip file "/data/app/com.example.counter1-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.counter1-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.counter1-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.counter1-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.counter1-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.counter1-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.counter1-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.counter1-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.counter1-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.counter1-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.counter1-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.co
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.support.v4.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2341)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at void android.support.v7.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:62)
2019-02-10 19:35:22.664 15241-15241/com.example.counter1 I/art:     at java.lang.Object java.lang.reflect.Constructor.newInstance0!(java.lang.Object[]) (Constructor.java:-2)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:430)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:652)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:794)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:734)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:865)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:828)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:525)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:427)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:378)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at android.view.ViewGroup android.support.v7.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void android.support.v7.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void android.support.v7.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void com.example.counter1.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:16)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6955)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1126)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2927)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:3045)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void android.app.ActivityThread.-wrap14(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1642)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void android.os.Looper.loop() (Looper.java:154)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6776)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:1496)
2019-02-10 19:35:22.665 15241-15241/com.example.counter1 I/art:     at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:1386)
2019-02-10 19:35:22.751 15241-15241/com.example.counter1 D/TextView: setTypeface with style : 0
2019-02-10 19:35:22.752 15241-15241/com.example.counter1 D/TextView: setTypeface with style : 0
2019-02-10 19:35:22.874 15241-15241/com.example.counter1 D/TextView: setTypeface with style : 0
2019-02-10 19:35:22.875 15241-15241/com.example.counter1 D/TextView: setTypeface with style : 0
2019-02-10 19:35:22.893 15241-15241/com.example.counter1 D/TextView: setTypeface with style : 0
2019-02-10 19:35:22.894 15241-15241/com.example.counter1 D/TextView: setTypeface with style : 0
2019-02-10 19:35:22.909 15241-15241/com.example.counter1 D/TextView: setTypeface with style : 0
2019-02-10 19:35:22.911 15241-15241/com.example.counter1 D/TextView: setTypeface with style : 0
2019-02-10 19:35:22.912 15241-15241/com.example.counter1 D/TextView: setTypeface with style : 0
2019-02-10 19:35:22.912 15241-15241/com.example.counter1 D/TextView: setTypeface with style : 0
2019-02-10 19:35:22.918 15241-15241/com.example.counter1 D/TextView: setTypeface with style : 0
2019-02-10 19:35:22.920 15241-15241/com.example.counter1 D/TextView: setTypeface with style : 0
2019-02-10 19:35:22.920 15241-15241/com.example.counter1 D/TextView: setTypeface with style : 0
2019-02-10 19:35:22.921 15241-15241/com.example.counter1 D/TextView: setTypeface with style : 0
2019-02-10 19:35:22.996 15241-15241/com.example.counter1 D/AndroidRuntime: Shutting down VM
2019-02-10 19:35:22.998 15241-15241/com.example.counter1 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.counter1, PID: 15241
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.counter1/com.example.counter1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
        at android.app.ActivityThread.-wrap14(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
        at com.example.counter1.HomeFragment.onCreate(HomeFragment.java:38)
        at android.support.v4.app.Fragment.performCreate(Fragment.java:2414)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
        at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1195)
        at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1078)
        at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:117)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2408)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
        at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
        at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1256)
        at android.app.Activity.performStart(Activity.java:6972)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2937)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
        at android.app.ActivityThread.-wrap14(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6776) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386) 

如果按钮被按下,我希望计数器计数为+1,而另一个按钮被按下时,计数器计数为-1。

P.S如果有错误,请说我的英语。感谢所有答案!

3 个答案:

答案 0 :(得分:0)

由于您以getView()方法调用onCreate()且尚未创建视图,因此应用程序崩溃;您只能在getView()完成后调用onCreateView(),因为onCreateView()会膨胀并返回此视图。 您可以参考Fragment lifecycle了解更多信息。

答案 1 :(得分:0)

尝试将getView()替换为普通的View。替换为以下代码:

    public class HomeFragment extends Fragment {
    private TextView counterText;
    private Button plusButton;
    private Button minusButton;
    private Button resetButton;
    private int counter;



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_home, container, false);

        counterText = view.findViewById(R.id.counterText);
        minusButton = view.findViewById(R.id.minusButton);
        minusButton.setOnClickListener(clickListener);
        plusButton = view.findViewById(R.id.plusButton);
        plusButton.setOnClickListener(clickListener);
        resetButton = view.findViewById(R.id.resetButton);
        resetButton.setOnClickListener(clickListener);

        initCounter();
        return  view;



    }

    private void initCounter() {
        counter = 0;
        counterText.setText(counter + "");
    }


    private void minusCounter() {
        counter--;
        counterText.setText(counter + "");
    }

    private void plusCounter() {
        counter++;
        counterText.setText(counter + "");
    }
    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(@org.jetbrains.annotations.NotNull View view) {
            switch (view.getId()) {
                case R.id.minusButton:
                    minusCounter();
                    break;
                case R.id.plusButton:
                    plusCounter();
                    break;
                case R.id.resetButton:
                    initCounter();
                    break;
            }
        }
    };
    }

答案 2 :(得分:0)

正如赞因先生所说,从getView()移除onCreate()

您可以像这样从onCreate()获取视图:

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

    counterText = findViewById(R.id.counterText);
    minusButton = findViewById(R.id.minusButton);
    minusButton.setOnClickListener(clickListener);
    plusButton = findViewById(R.id.plusButton);
    plusButton.setOnClickListener(clickListener);
    resetButton = findViewById(R.id.resetButton);
    resetButton.setOnClickListener(clickListener);

    initCounter();
}

或者只是尝试Kotlin,在Kotlin中,您不需要执行任何findViewByID()操作。