ontouchlistener不断破坏我的应用程序

jhdbpxl9  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(345)

这个问题在这里已经有答案了

什么是nullpointerexception,如何修复它(12个答案)
25天前关门了。
我目前正在为android制作一个带有自定义新按钮的音板应用程序。我的应用程序的目标是,当我使用ontouchlistener单击按钮时,它可以改变样式(看起来像被按下了一样)。但是,每当我尝试添加ontouchlistener(在mainacitvity.java上的initbutton()函数中)时,我的应用程序就会崩溃。我附上了我的代码以及我在下面使用的新媒体库的链接:

新视图库:

https://medium.com/@fornewid/neumorphism-in-android-9cf15e2122dc
https://github.com/fornewid/neumorphism

主活动.java

公共类mainactivity扩展了appcompatactivity{

private SectionsPageAdapter mSectionsPageAdapter;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = findViewById(R.id.titleText);
        String text = "R6 Soundboard";
        SpannableString ss = new SpannableString(text);
        ForegroundColorSpan fcsYellow = new ForegroundColorSpan(Color.rgb(255,236,141));
        ss.setSpan(fcsYellow, 1,2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(ss);

        mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.view_pager);

        setUpViewPager(mViewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setTabTextColors(Color.WHITE, Color.WHITE);
        tabLayout.setupWithViewPager(mViewPager);
        tabLayout.getTabAt(0).setText("Defenders");
        tabLayout.getTabAt(1).setText("Attackers");

        initButton();
    }

    private void setUpViewPager(ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new defendersTab(), "Defenders");
        adapter.addFragment(new attackersTab(), "Attackers");

        viewPager.setAdapter(adapter);

    }

    @SuppressLint("ClickableViewAccessibility")
    private void initButton(){
        final NeumorphCardView DefR1C1 = (NeumorphCardView) findViewById(R.id.buttonDR1C1);

        DefR1C1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP) {
                    DefR1C1.setShapeType(ShapeType.PRESSED);
                } else if(event.getAction() == MotionEvent.ACTION_DOWN) {
                    DefR1C1.setShapeType(ShapeType.FLAT);
                }
                return false;
            }

        });

        /*
        final MediaPlayer mp = MediaPlayer.create(this, R.raw.lionroar);
        DefR1C1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                if(mp.isPlaying()) {
                    //STOP PLAYING
                    mp.seekTo(0);
                }
                mp.start();
                return false;
            }

        });

         */
    }

}

活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        android:theme="@style/AppTheme.AppBarOverlay">

        <TextView
            android:id="@+id/titleText"
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/avenir"
            android:layout_marginTop="20dp"
            android:gravity="center_horizontal"
            android:text="R6 Soundboard"
            android:textColor="#FFF"
            android:textSize="30sp" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabTextColor="#fff"
            android:background="?attr/colorPrimaryDark" />
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

片段\u\u tab.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark">

        <soup.neumorphism.NeumorphCardView
            android:id="@+id/buttonDR1C1"
            android:layout_width="175dp"
            android:layout_height="175dp"
            android:layout_alignParentStart="true"
            android:layout_marginStart="20dp"

            android:layout_marginTop="20dp"
            app:neumorph_backgroundColor="@color/colorPrimaryDark"
            app:neumorph_inset="12dp"
            app:neumorph_insetBottom="12dp"

            app:neumorph_insetEnd="12dp"
            app:neumorph_insetStart="12dp"
            app:neumorph_insetTop="12dp"
            app:neumorph_lightSource="leftTop"
            app:neumorph_shadowColorDark="@color/solid_dark_color"
            app:neumorph_shadowColorLight="@color/solid_light_color"
            app:neumorph_shadowElevation="3dp"
            app:neumorph_shapeType="flat">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="62.5dp"
                    android:fontFamily="@font/proximanovasoft"
                    android:text="Lion Scan"
                    android:textColor="#ffffff"
                    android:textSize="20dp" />

            </LinearLayout>

        </soup.neumorphism.NeumorphCardView>

        <soup.neumorphism.NeumorphCardView
            android:id="@+id/buttonDR1C2"
            android:layout_width="175dp"
            android:layout_height="175dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"

            android:layout_marginTop="20dp"
            android:layout_marginEnd="20dp"

            app:neumorph_backgroundColor="@color/colorPrimaryDark"
            app:neumorph_inset="12dp"
            app:neumorph_insetBottom="12dp"
            app:neumorph_insetEnd="12dp"
            app:neumorph_insetStart="12dp"
            app:neumorph_insetTop="12dp"
            app:neumorph_lightSource="leftTop"
            app:neumorph_shadowColorDark="@color/solid_dark_color"
            app:neumorph_shadowColorLight="@color/solid_light_color"
            app:neumorph_shadowElevation="3dp"
            app:neumorph_shapeType="flat">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="62.5dp"
                    android:fontFamily="@font/proximanovasoft"
                    android:text="Thermite"
                    android:textColor="#ffffff"
                    android:textSize="20dp" />
            </LinearLayout>
        </soup.neumorphism.NeumorphCardView>

        <soup.neumorphism.NeumorphCardView
            android:id="@+id/buttonDR2C1"
            android:layout_width="175dp"
            android:layout_height="175dp"

            android:layout_below="@+id/buttonDR1C2"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="11dp"
            android:layout_marginEnd="20dp"

            app:neumorph_backgroundColor="@color/colorPrimaryDark"
            app:neumorph_inset="12dp"
            app:neumorph_insetBottom="12dp"
            app:neumorph_insetEnd="12dp"
            app:neumorph_insetStart="12dp"
            app:neumorph_insetTop="12dp"
            app:neumorph_lightSource="leftTop"
            app:neumorph_shadowColorDark="@color/solid_dark_color"
            app:neumorph_shadowColorLight="@color/solid_light_color"
            app:neumorph_shadowElevation="3dp"
            app:neumorph_shapeType="flat">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="62.5dp"
                    android:fontFamily="@font/proximanovasoft"
                    android:text="Barricade"
                    android:textColor="#ffffff"
                    android:textSize="20dp" />
            </LinearLayout>
        </soup.neumorphism.NeumorphCardView>

        <soup.neumorphism.NeumorphCardView
            android:id="@+id/buttonDR2C2"
            android:layout_width="175dp"
            android:layout_height="175dp"

            android:layout_below="@+id/buttonDR1C1"
            android:layout_alignParentStart="true"
            android:layout_marginStart="20dp"
            android:layout_marginTop="12dp"
            app:neumorph_backgroundColor="@color/colorPrimaryDark"
            app:neumorph_inset="12dp"

            app:neumorph_insetBottom="12dp"
            app:neumorph_insetEnd="12dp"
            app:neumorph_insetStart="12dp"
            app:neumorph_insetTop="12dp"
            app:neumorph_lightSource="leftTop"
            app:neumorph_shadowColorDark="@color/solid_dark_color"
            app:neumorph_shadowColorLight="@color/solid_light_color"
            app:neumorph_shadowElevation="3dp"
            app:neumorph_shapeType="flat">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="62.5dp"
                    android:fontFamily="@font/proximanovasoft"
                    android:text="Planting"
                    android:textColor="#ffffff"
                    android:textSize="20dp" />
            </LinearLayout>
        </soup.neumorphism.NeumorphCardView>

        <soup.neumorphism.NeumorphCardView
            android:id="@+id/buttonDR3C1"
            android:layout_width="175dp"
            android:layout_height="175dp"

            android:layout_below="@+id/buttonDR2C2"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="11dp"
            android:layout_marginEnd="20dp"

            app:neumorph_backgroundColor="@color/colorPrimaryDark"
            app:neumorph_inset="12dp"
            app:neumorph_insetBottom="12dp"
            app:neumorph_insetEnd="12dp"
            app:neumorph_insetStart="12dp"
            app:neumorph_insetTop="12dp"
            app:neumorph_lightSource="leftTop"
            app:neumorph_shadowColorDark="@color/solid_dark_color"
            app:neumorph_shadowColorLight="@color/solid_light_color"
            app:neumorph_shadowElevation="3dp"
            app:neumorph_shapeType="flat">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="62.5dp"
                    android:fontFamily="@font/proximanovasoft"
                    android:text="Fuze"
                    android:textColor="#ffffff"
                    android:textSize="20dp" />
            </LinearLayout>
        </soup.neumorphism.NeumorphCardView>

        <soup.neumorphism.NeumorphCardView
            android:id="@+id/buttonDR3C2"
            android:layout_width="175dp"
            android:layout_height="175dp"

            android:layout_below="@+id/buttonDR2C1"
            android:layout_alignParentStart="true"
            android:layout_marginStart="20dp"
            android:layout_marginTop="12dp"
            app:neumorph_backgroundColor="@color/colorPrimaryDark"
            app:neumorph_inset="12dp"

            app:neumorph_insetBottom="12dp"
            app:neumorph_insetEnd="12dp"
            app:neumorph_insetStart="12dp"
            app:neumorph_insetTop="12dp"
            app:neumorph_lightSource="leftTop"
            app:neumorph_shadowColorDark="@color/solid_dark_color"
            app:neumorph_shadowColorLight="@color/solid_light_color"
            app:neumorph_shadowElevation="3dp"
            app:neumorph_shapeType="flat">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="62.5dp"
                    android:fontFamily="@font/proximanovasoft"
                    android:text="Drone"
                    android:textColor="#ffffff"
                    android:textSize="20dp" />
            </LinearLayout>
        </soup.neumorphism.NeumorphCardView>

    </RelativeLayout>

</FrameLayout>

sectionspageadapter.java节

public class SectionsPageAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    public SectionsPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return super.getPageTitle(position);
    }
    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }
    @Override
    public int getCount() {
        return mFragmentList.size();
    }
}

编辑:在请求时,我添加了崩溃日志和我的应用程序工作的图片,然后我尝试使用ontouchlistener使按钮实际工作

2020-12-14 10:54:31.781 8217-8217/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jkcarraher.rainbowsixsoundboard, PID: 8217
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jkcarraher.rainbowsixsoundboard/com.jkcarraher.rainbowsixsoundboard.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void soup.neumorphism.NeumorphCardView.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void soup.neumorphism.NeumorphCardView.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
    at com.jkcarraher.rainbowsixsoundboard.MainActivity.initButton(MainActivity.java:70)
    at com.jkcarraher.rainbowsixsoundboard.MainActivity.onCreate(MainActivity.java:54)
    at android.app.Activity.performCreate(Activity.java:7995)
    at android.app.Activity.performCreate(Activity.java:7979)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 


感谢您提供的任何见解!

vql8enpb

vql8enpb1#

您的neumorphcardview未正确初始化,这就是它引发nullpointerexception的原因。我猜你没有在activity\u main.xml中正确设置tablayout。具体来说,你的目标是什么android:id=“@+id/tabs”指的是什么?我认为您没有成功地将碎片\u选项卡连接到活动\u主目录。

0g0grzrc

0g0grzrc2#

您的错误是您正在mainactivity.java中使用activity\ u main.xml。但是,您提供的xml文件称为fragment\u\u tab。你根本没有在代码中引用这个。
所以,简单的解决方法应该是改变这一点:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

对此:

super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_defenders_tab);

相关问题