android - NullPointerException when trying to load a button -


i trying open new activity. when null pointer exception. works untill following code gets executed.

    button rmtests =(button)findviewbyid(r.id.btnrmtests);     rmtests.setonclicklistener(ansrmtest); 

here better @ code.

public class displaytests extends listactivity {     private dbmanagement mdbmanager;     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         //mdbmanager = new dbmanagement(this);         //mdbmanager.open();         button rmtests =(button)findviewbyid(r.id.btnrmtests);         rmtests.setonclicklistener(ansrmtest);          //filldata();     }     private onclicklistener ansrmtest = new onclicklistener(){          @override         public void onclick(view v) {             try{                               system.out.println("fart");             }             catch(exception ex){               system.out.println(ex.tostring());             }         }            }; 

i not sure why activity bomb out when reaches creation of button.

here xml well

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent" >      <framelayout         android:id="@+id/framelayout1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentleft="true"         android:layout_alignparentright="true"         android:layout_alignparenttop="true" >          <listview             android:id="@+id/listview1"             android:layout_width="match_parent"             android:layout_height="194dp" >         </listview>     </framelayout>      <button         android:id="@+id/btnnewexam"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentleft="true"         android:layout_centervertical="true"         android:text="@string/strnewexam" />      <button         android:id="@+id/btnrmexams"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignbaseline="@+id/btnrmtests"         android:layout_alignbottom="@+id/btnrmtests"         android:layout_alignparentright="true"         android:text="@string/strrmexams" />      <button         android:id="@+id/btnrmtests"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignbaseline="@+id/btnnewexam"         android:layout_alignbottom="@+id/btnnewexam"         android:layout_toleftof="@+id/btnrmexams"         android:text="@string/strrmtest" />  </relativelayout> 

so being said following code

setcontentview(r.layout.moretime); 

when did following error: java.lang.runtimeexception: content must have listview id attribute 'android.r.id.list'

i have list view can see called listview1 , not sure why getting error message. in advance.

you need set content of layout activity first initialize views.

   privae button rmtests;    public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.mylayout); // missing     rmtests =(button)findviewbyid(r.id.btnrmtests);     rmtests.setonclicklistener(ansrmtest);     ... // rest of code     }      

you can findviewbyid of current view hierarchy set activity. since have not set content activity, when initialize button nullpointerexception.

also

listactivity has default layout consists of single, full-screen list in center of screen. however, if desire, can customize screen layout setting own view layout setcontentview() in oncreate(). to this, own view must contain listview object id "@android:id/list" (or list if it's in code)

in xml replace

 <listview         android:id="@+id/listview1" 

by

 <listview android:id="@android:id/list" 

also require framelayout? why not have listview , buttons in relative layout.

http://developer.android.com/reference/android/app/listactivity.html

edit:

from comment below

you can below

public class mainactivity extends listactivity { string s[] = {"a","b","c","d","e"}; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     arrayadapter<string> adapter = new arrayadapter<string>(this,android.r.layout.simple_spinner_dropdown_item, s);     setlistadapter(adapter);  } } 

listactivity has default layout consists of single, full-screen list in center of screen. however, if desire, can customize screen layout setting own view layout setcontentview() in oncreate().

if need have other views can define them in xml extend listactivity in case need have listview id "@android:id/list". can have list , button @ button. need have listview id "@android:id/list".


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -