Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't understand NullPointerException
    text
    copied!<p>I'm experimenting with ListViews and ArrayAdapters, and i've come into a runTimeException issue, the DDMS error report is telling me that i'm having a NullPointerException at the line containing "public void onItemClick", I cant see why it is giving me a issue.</p> <pre><code> package com.example.worksheet2; import android.os.Bundle; import android.app.ListActivity; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends ListActivity { ListView listview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listview = (ListView) findViewById(R.layout.activity_main); String[] values = { "A", "B", "C" }; ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, R.id.menu_settings, values); // asign adapter to listview listview.setAdapter(adapter); listview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id) { Toast.makeText(getApplicationContext(), "Click ListItem Number " + position, Toast.LENGTH_LONG) .show(); } }); } } </code></pre> <p>Here's the error report</p> <pre><code> Caused by: java.lang.NullPointerException at com.example.worksheet2.MainActivity.onCreate(MainActivity.java:31) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload