title: Random text from array - android
body: <p>I want to make an app which generate random text from an array. I have some errors, I think my code is not good.</p>
  
  <p>This is my MainActivity.</p>
  
  <pre><code>package com.example.bestquotes;
  
  
  import java.util.Random;
  
  import android.content.res.Resources;
  import android.os.Bundle;
  import android.support.v7.app.ActionBarActivity;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  
  
  
  
  public class MainActivity extends ActionBarActivity {
  
      private String [] myString;
      private static final Random rgenerator = new Random();
      int index = 0;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          MenuInflater inflater = getMenuInflater();
          inflater.inflate (R.menu.main, menu);
          return true;
      }
  
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch(item.getItemId()){
          case R.id.action_one:
              onClickOne();
  
          case R.id.action_two:
              onClickTwo();
  
          }
          return super.onOptionsItemSelected(item);
      }
  
      public void onClickOne(){
          Resources res = getResources();
          myString = res.getStringArray(R.array.quote);
          final TextView tv = (TextView) findViewById(R.id.quote);
          final String q = myString[rgenerator.nextInt(myString.length)];
          tv.setText(q);
          Button btn = (Button) findViewById(R.id.btn);
          btn.setOnClickListener(new View.OnClickListener(){
              public void onClick(View v){
                  myString = getResources().getStringArray(R.array.quote);
                  index = rgenerator.nextInt(myString.length);
                  String q = myString[index];
                  tv.setText(q);
              }
          });
      }
  
      public void onClickTwo(){
          Resources res = getResources();
          myString = res.getStringArray(R.array.quotetwo);
          final TextView tv2 = (TextView) findViewById(R.id.quote);
          final String q = myString[rgenerator.nextInt(myString.length)];
          tv2.setText(q);
          Button btn = (Button) findViewById(R.id.btn);
          btn.setOnClickListener(new View.OnClickListener(){
              public void onClick(View v){
                  myString = getResources().getStringArray(R.array.quotetwo);
                  index = rgenerator.nextInt(myString.length);
                  String q = myString[index];
                  tv2.setText(q);
              }
          });
      }
  
  
  }
  </code></pre>
  
  <p>This is mmenu/main.xml :</p>
  
  <pre><code>&lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context="com.example.bestquotes.MainActivity" &gt;
  
      &lt;Button
          android:id="@+id/btn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:layout_alignParentBottom="true"
          android:background="@drawable/btn" /&gt;
  
  
      &lt;item
  
          android:id="@+id/action_settings"
          android:orderInCategory="100"
          android:title="@string/action_settings"
          app:showAsAction="never" /&gt;
  
      &lt;item
          android:visible="true"
        android:id="@+id/action_more"
        android:icon="@drawable/icon"
        android:orderInCategory="1"
        app:showAsAction="always"
        android:title="More" &gt;
  
        &lt;menu&gt;
            &lt;item 
                android:id="@+id/action_one"
                android:title="Sync"
                /&gt;
            &lt;item android:id="@+id/action_two"
                android:title="About"/&gt;
        &lt;/menu&gt;
  
  &lt;/item&gt;
  &lt;/menu&gt;
  </code></pre>
  
  <p>And here is quotes.xml </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;string-array name="quote"&gt;
          &lt;item&gt;Text random 1&lt;/item&gt;
          &lt;item&gt;Text random 2&lt;/item&gt;
  
      &lt;/string-array&gt;
  
      &lt;string-array name="quotetwo"&gt;
          &lt;item&gt;Text random 1&lt;/item&gt;
          &lt;item&gt;Text random 2&lt;/item&gt;
  
  
      &lt;/string-array&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>I need some help beucase I am thinking the code is wrong, I mean these 2 methods: onClickOne() and onClickTwo().</p>
  
  <pre><code>10-16 15:14:34.902: E/AndroidRuntime(8626): FATAL EXCEPTION: main
  10-16 15:14:34.902: E/AndroidRuntime(8626): java.lang.NullPointerException
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at com.example.bestquotes.MainActivity.onClickOne(MainActivity.java:63)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at com.example.bestquotes.MainActivity.onOptionsItemSelected(MainActivity.java:47)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at android.app.Activity.onMenuItemSelected(Activity.java:2610)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:373)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at android.support.v7.app.ActionBarActivity.superOnMenuItemSelected(ActionBarActivity.java:245)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at android.support.v7.app.ActionBarActivityDelegateICS.onMenuItemSelected(ActionBarActivityDelegateICS.java:164)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:131)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onMenuItemSelected(ActionBarActivityDelegateICS.java:308)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1144)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at com.android.internal.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:81)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:175)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at android.widget.AdapterView.performItemClick(AdapterView.java:301)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at android.widget.AbsListView.performItemClick(AbsListView.java:1490)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3275)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at android.widget.AbsListView$1.run(AbsListView.java:4518)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at android.os.Handler.handleCallback(Handler.java:725)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at android.os.Handler.dispatchMessage(Handler.java:92)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at android.os.Looper.loop(Looper.java:137)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at android.app.ActivityThread.main(ActivityThread.java:5283)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at java.lang.reflect.Method.invokeNative(Native Method)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at java.lang.reflect.Method.invoke(Method.java:511)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
  10-16 15:14:34.902: E/AndroidRuntime(8626):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>