来自数组的随机文本 - android

时间:2014-10-16 12:15:52

标签: android

我想创建一个从数组生成随机文本的应用程序。我有一些错误,我认为我的代码不好。

这是我的MainActivity。

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);
            }
        });
    }


}

这是mmenu / main.xml:

<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" >

    <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" />


    <item

        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />

    <item
        android:visible="true"
      android:id="@+id/action_more"
      android:icon="@drawable/icon"
      android:orderInCategory="1"
      app:showAsAction="always"
      android:title="More" >

      <menu>
          <item 
              android:id="@+id/action_one"
              android:title="Sync"
              />
          <item android:id="@+id/action_two"
              android:title="About"/>
      </menu>

</item>
</menu>

这是quotes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="quote">
        <item>Text random 1</item>
        <item>Text random 2</item>

    </string-array>

    <string-array name="quotetwo">
        <item>Text random 1</item>
        <item>Text random 2</item>


    </string-array>
</resources>

我需要一些帮助beucase我认为代码是错误的,我的意思是这两个方法:onClickOne()和onClickTwo()。

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)

2 个答案:

答案 0 :(得分:3)

 <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" />

作为setContentView参数提供的布局进入菜单布局内

答案 1 :(得分:2)

Button btn为空,因为它是在菜单xml文件中定义的,但是您调用了findViewById,它会在Activity的布局中查找ID R.id.btn,而不是在菜单中。

如果要使用图像定义菜单项,可以执行以下操作:

<item
    ...
    android:icon="@drawable/some_icon_name"/>