当我将 onClickListener 放在 ImageView 上时,它使我的应用程序崩溃

It crashes my app when I put onClickListener on a ImageView

总结问题
开发者您好, 所以.. 我正在寻找一个 PopupMenù,它会在单击以下图标时出现,该图标停留在 ListView 中的项目上。我想在点击时出现带有一些选项的弹出栏

描述您尝试过的内容
我尝试按照有关创建 Popup Menù 的教程进行操作,但方法始终相同,我对此没有任何问题。但它向我显示 Logcat 上的空指针错误,我尝试通过初始化 ImageView 来修复,但目前没有任何正确的方法。 我尝试做一些类似“image = (Imageview)findviewbyId(R.id.imagesrc)”的事情,但它总是出现以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.isilente/com.example.isilente.HomePage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

在适当的时候,显示一些代码
我在编码时遇到这种情况: Class -> Homepage.java

public class HomePage extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;
private AppCompatButton getstart, augurio;


//new stuff
private ArrayList<Music> my_mainarraylist;
private MusicAdapter my_musicadapter;
private ListView listView_songs;
ImageView imageView_puntini;
//end new stuff



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);
    getstart=findViewById(R.id.GetStart);
    imageView_puntini=findViewById(R.id.imageview_puntini);
    listView_songs=findViewById(R.id.listview_buongiorno);


    //START NEW STUFF
    my_mainarraylist=new ArrayList<>();
    my_mainarraylist.add(new Music("Buongiorno#1", "Pozione Polisucchio", R.raw.silente, R.id.imageview_play, R.id.imageview_stop, R.id.imageview_puntini));
    my_mainarraylist.add(new Music("Augurio", "Hai dato il massimo", R.raw.massimo, R.id.imageview_play, R.id.imageview_stop, R.id.imageview_puntini));

    MusicAdapter adapter = new MusicAdapter(this, R.layout.songs_item_listview,my_mainarraylist);
    listView_songs.setAdapter(adapter);



    listView_songs.setVisibility(View.GONE);
    //FINE NEW STUFF

    /*----Hooks----*/
    drawerLayout = findViewById(R.id.drawerLayout);
    navigationView = findViewById(R.id.nav_view);
    toolbar = findViewById(R.id.toolbar);
    /*----Toolbar----*/
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    /*----NavigationDrawerMenu----*/

    //Menu menu = navigationView.getMenu();

    navigationView.bringToFront();
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();
    //navigationView.setCheckedItem(R.id.nav_home);

    navigationView.setNavigationItemSelectedListener(this);

    getstart.setOnClickListener(v -> listView_songs.setVisibility(View.VISIBLE));

    //popup
    imageView_puntini.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popupMenu = new PopupMenu(HomePage.this, v);
            MenuInflater menuInflater = popupMenu.getMenuInflater();
            menuInflater.inflate(R.menu.menuset, popupMenu.getMenu());
            popupMenu.show();
        }
    });
    //popup end
}

在 res/menu 中:

   <menu><item
    android:id="@+id/item_settings"
    android:title="Settings"
    android:showAsAction="ifRoom|withText"
    />
<item
    android:id="@+id/item_about"
    android:title="About"
    android:showAsAction="ifRoom|withText"
    /></menu>

然后我得到了这个,它是 ListView 上的项目.. songs-item-listview.xml:

 <?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:background="#152238"
    android:paddingLeft="5dp"
    android:paddingRight="16dp">

    <ImageView
        android:id="@+id/audio_microfono"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic__431742_audio_communication_instrument_media_microphone_icon"
        />

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textview_songs"
        android:text="Current song"
        android:textStyle="bold"
        android:textColor="#eee86b"
        android:textSize="16sp"
        android:layout_toEndOf="@+id/audio_microfono"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textview_artist"
        android:text="Current artist"
        android:textStyle="italic"
        android:textColor="#eee86b"
        android:layout_toEndOf="@+id/audio_microfono"
        android:layout_below="@+id/textview_songs"
        android:textSize="16sp"
        />
    </androidx.appcompat.widget.LinearLayoutCompat>

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageview_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="10dp"
        android:src="@drawable/ic_play"/>

    <ImageView
        android:id="@+id/imageview_stop"
        android:layout_width="wrap_content"
        android:layout_alignParentEnd="true"
        android:paddingRight="5dp"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_stop"
        />

        <ImageView
            android:id="@+id/imageview_puntini"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_trepuntini"
            />
    </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>

有人说按钮不在主按钮中可能是错误 activity 但它在另一个主按钮中,但无论如何,你建议我做什么?

Ps: 这是 LogCat :

2021-09-05 20:20:19.629 9883-9883/com.example.isilente E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.isilente, PID: 9883 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.isilente/com.example.isilente.HomePage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' 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 android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.isilente.HomePage.onCreate(HomePage.java:94) at android.app.Activity.performCreate(Activity.java:8000) at android.app.Activity.performCreate(Activity.java:7984) 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)

Blockquote Blockquote Blockquote

请添加这个

<ImageView
            android:id="@+id/imageview_puntini"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_trepuntini"
            />

activity_home_page.xml 您定义了列表视图而不是列表视图项 songs-item-listview.xml,因为您正试图从 activity.

访问它

尝试像这样初始化你的 PopupMenu

popup.getMenu().add(groupId, itemId, order, title);

对于您要添加的每个 MenuItem。 您还可以查看此 link 以查看其他解决方案
(我的解决方案来自 this man