Summarize the problem
Hello developers,
So.. I'm looking to do a PopupMenù that it appears on click of the following icon that stays on a item in a ListView. I want to do that on click appears a Popup bar with some options
Describe what you’ve tried
I tried to follow tutorials about creating Popup Menù but the way is always the same and I have no problem about it. But it shows me this Null Pointer error on Logcat and I tried to fix with initialize the ImageView but nothing goes on the right way for the moment..
I tried to do for example things like "image = (Imageview)findviewbyId(R.id.imagesrc)" but it always appears the following error :
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
When appropriate, show some code
I have this situation on coding :
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
}
in 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>
And then I've got this one that it is the Item on 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>
Someone says that it can be an error occurred about the button is not in the main activity but it is in another but in any case, what do you suggest me to do?
Ps: this is the 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
Please add this
<ImageView
android:id="@+id/imageview_puntini"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_trepuntini"
/>
to activity_home_page.xml
where you have defined list view, instead of list view item songs-item-listview.xml
, since you are trying to access it from the activity.