Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawing listView on Canvas - setAdapter causes NullPointerException
    primarykey
    data
    text
    <p>I've been trying to draw ListView on Canvas, and I get a NullPointerException when I try to set adapter to that listView. I checked it, and it seems listView is null after initialization and I have no idea why.</p> <p>Here is the code:</p> <pre><code>public class Testing extends Activity { MySurface ms; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ms = new MySurface(this); setContentView(ms); String[] stuffArray = {"stuff1", "stuff2"}; ListView listView = (ListView) findViewById(R.id.lvListView); // after this listView is null ArrayAdapter&lt;String&gt; listViewAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, stuffArray); //listView.setAdapter(listViewAdapter); // This line crashes program } @Override protected void onResume() { super.onResume(); ms.resume(); } @Override protected void onPause() { super.onPause(); ms.pause(); } public class MySurface extends SurfaceView implements Runnable { SurfaceHolder holder; Thread t = null; volatile boolean isRunning = false; public MySurface(Context context) { super(context); holder = getHolder(); } public void pause(){ isRunning = false; while(true) { try { t.join(); break; } catch (InterruptedException e) { } } } public void resume(){ isRunning = true; t = new Thread(this); t.start(); } public void run() { LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = li.inflate(R.layout.testing, null); v.measure(MeasureSpec.getSize(v.getMeasuredWidth()), MeasureSpec.getSize(v.getMeasuredHeight())); v.layout(0, 0, 0, 0); while(isRunning){ if(!holder.getSurface().isValid()) continue; Canvas canvas = holder.lockCanvas(); canvas.translate(50, 50); v.draw(canvas); holder.unlockCanvasAndPost(canvas); } } } } </code></pre> <p>And the contents of testing.xml layout:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/tvTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="List of stuff:" /&gt; &lt;ListView android:id="@+id/lvListView" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>LogCat window throws:</p> <pre><code>02-17 12:14:56.359: E/AndroidRuntime(9226): FATAL EXCEPTION: main 02-17 12:14:56.359: E/AndroidRuntime(9226): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testing/com.example.testing.Testing}: java.lang.NullPointerException 02-17 12:14:56.359: E/AndroidRuntime(9226): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 02-17 12:14:56.359: E/AndroidRuntime(9226): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 02-17 12:14:56.359: E/AndroidRuntime(9226): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 02-17 12:14:56.359: E/AndroidRuntime(9226): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 02-17 12:14:56.359: E/AndroidRuntime(9226): at android.os.Handler.dispatchMessage(Handler.java:99) 02-17 12:14:56.359: E/AndroidRuntime(9226): at android.os.Looper.loop(Looper.java:123) 02-17 12:14:56.359: E/AndroidRuntime(9226): at android.app.ActivityThread.main(ActivityThread.java:3687) 02-17 12:14:56.359: E/AndroidRuntime(9226): at java.lang.reflect.Method.invokeNative(Native Method) 02-17 12:14:56.359: E/AndroidRuntime(9226): at java.lang.reflect.Method.invoke(Method.java:507) 02-17 12:14:56.359: E/AndroidRuntime(9226): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 02-17 12:14:56.359: E/AndroidRuntime(9226): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 02-17 12:14:56.359: E/AndroidRuntime(9226): at dalvik.system.NativeStart.main(Native Method) 02-17 12:14:56.359: E/AndroidRuntime(9226): Caused by: java.lang.NullPointerException 02-17 12:14:56.359: E/AndroidRuntime(9226): at com.example.testing.Testing.onCreate(Testing.java:30) 02-17 12:14:56.359: E/AndroidRuntime(9226): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 02-17 12:14:56.359: E/AndroidRuntime(9226): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 02-17 12:14:56.359: E/AndroidRuntime(9226): ... 11 more 02-17 12:14:56.369: E/(160): Dumpstate &gt; /data/log/dumpstate_app_error </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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