java - Have very big trouble with my choice generator -


i tried make button random picks answer if click on it; there no errors shown, don't know if made mistakes.

activity main java

package infuso.choicemaster; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview;  import infuso.choicemaster.r; public class activity_main extends appcompatactivity  {     public button generate;     public textview einleitung;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         final string[] mynames = {"call 3rd  person in phonebook , ask activities", "drive 5 miles car fun", "call 14th person in phonebook , ask activities ",                 "send message @ whatsapp random people in contacts", "do workout",                 "play old games n64,playstation 1 or whatever", "make funny selfies", "cook want", "watch films", "make fresh orange juice ",                 "climb on tree", "see how long can hold note", "try not think penguins", "pretend car", "make star trek door noises", "watch tv, repeat said in italian accent",                 "buy postcard send friend", "buy unicorn", "put walkie talkie in mail box , shout @ walks by", "sing llama song till you've annoyed in room",                 "start talking in made language", "call friend, , tell them can't talk right now, cause busy", "go elevator piece of chalk, draw box , declare area personal space"                 , "put vanilla pudding in jar says mayo , eat in public place", "go kfc dressed gangster , ask if have black fried chicken until kick out"                 , "dance around house"};         generate.setonclicklistener(new onclicklistener() {             @override             public void onclick(view v) {                 //todo auto-generated method sub                 int rando = (int) (math.random() * 26);                 einleitung.settext(mynames[rando]);             }         });     }     @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_main, menu);         generate = (button) findviewbyid(r.id.button);         einleitung = (textview) findviewbyid(r.id.textview1);         final textview textone = (textview) findviewbyid(r.id.textview1);         button generate = (button) findviewbyid(r.id.button);         generate.setonclicklistener((onclicklistener) this);         return true;     } } 

activity main xml

<textview     android:id="@+id/textview1"     android:text="@string/einleitung"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:gravity="center"     android:textsize="32dp"     android:layout_marginbottom="288dp"     android:layout_alignparentbottom="true"     android:layout_alignparentleft="true"     android:layout_alignparentstart="true" />  <button     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:text="@string/button"     android:textsize="32sp"     android:id="@+id/button"     android:layout_alignparentbottom="true"     android:layout_centerhorizontal="true"     android:layout_marginbottom="97sp" /> 

strings xml

<resources>  <string name="app_name">choice master</string> <string name="button">generate</string> <string name="action_settings">settings</string> <string name="einleitung">welcome choice master 1.0</string> </resources> 

android manifest xml

    <?xml version="1.0" encoding="utf-8"?> <!--suppress --> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="infuso.choicemaster" >      <application         android:allowbackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:theme="@style/theme.appcompat.light" >         <activity             android:name="com.infuso.choicemaster.activity_main" >             android:label="@string/app_name" >              <intent-filter>                 <action android:name="android.intent.action.main" />                 <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>     </application>  </manifest> 

i don't know need, included all.

you using findviewbyid inside oncreateoptionsmenu initialize local variable. not assign object instance variables nullpointerexception in following line:

generate.setonclicklistener(new onclicklistener() { 

modify code given below, work.

package infuso.choicemaster; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview;  public class activity_main extends appcompatactivity  {     public button generate;     public textview einleitung;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         // initialize instance variables         generate = (button) findviewbyid(r.id.button);         einleitung = (textview) findviewbyid(r.id.textview1);          final string[] mynames = {"call 3rd  person in phonebook , ask activities", "drive 5 miles car fun", "call 14th person in phonebook , ask activities ",                 "send message @ whatsapp random people in contacts", "do workout",                 "play old games n64,playstation 1 or whatever", "make funny selfies", "cook want", "watch films", "make fresh orange juice ",                 "climb on tree", "see how long can hold note", "try not think penguins", "pretend car", "make star trek door noises", "watch tv, repeat said in italian accent",                 "buy postcard send friend", "buy unicorn", "put walkie talkie in mail box , shout @ walks by", "sing llama song till you've annoyed in room",                 "start talking in made language", "call friend, , tell them can't talk right now, cause busy", "go elevator piece of chalk, draw box , declare area personal space"                 , "put vanilla pudding in jar says mayo , eat in public place", "go kfc dressed gangster , ask if have black fried chicken until kick out"                 , "dance around house"};         generate.setonclicklistener(new onclicklistener() {             @override             public void onclick(view v) {                 //todo auto-generated method sub                 int rando = (int) (math.random() * 26);                 einleitung.settext(mynames[rando]);             }         });     }     @override     public boolean oncreateoptionsmenu(menu menu) {         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     } } 

edit 1: have not notice this: in manifest file, activity name provided incorrectly. check class name.

<activity             android:name="infuso.choicemaster.activity_main" >             android:label="@string/app_name" >              <intent-filter>                 <action android:name="android.intent.action.main" />                 <category android:name="android.intent.category.launcher" />             </intent-filter> </activity> 

additional that, delete "import infuso.choicemaster.r;" java code.

edit 2: working sample of project: choice master


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -