我的选择发电机有很大的麻烦

时间:2015-06-19 04:32:28

标签: java android xml

如果点击它,我试图制作一个随机选择答案的按钮;没有显示错误,我不知道我是否犯了错误。

活动主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 the 3rd  person in your Phonebook and ask for activities", "Drive 5 miles with your car just for fun", "Call the 14th person in your Phonebook and ask for activities ",
                "Send a message at whatsapp to a random people in your contacts", "Do a workout",
                "Play old games like n64,playstation 1 or whatever", "Make funny selfies", "Cook something you want", "Watch some films", "Make fresh orange juice ",
                "Climb on a tree", "See how long you can hold a note", "Try to not think about penguins", "Pretend to be a car", "Make Star Trek door noises", "Watch TV, repeat everything said in Italian accent",
                "Buy a postcard an send it to your friend", "Buy a unicorn", "Put a walkie talkie in your mail box and shout at everybody who walks by", "Sing the Llama song till you've annoyed everyone in the room",
                "Start talking in a made up language", "Call your friend, and tell them you can't talk right now, cause your busy", "Go into an elevator with a piece of chalk, draw a box and declare the area your personal space"
                , "Put vanilla pudding in a jar that says MAYO and then eat it in a public place", "go to kfc dressed like a gangster and ask if they have black fried chicken until they kick you out"
                , "Dance around your 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 the menu; this adds items to the action bar if it is 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;
    }
}

活动主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" />

字符串xml

<resources>

<string name="app_name">Choice Master</string>
<string name="button">Generate</string>
<string name="action_settings">Settings</string>
<string name="einleitung">Welcome to Choice Master 1.0</string>
</resources>

Android Manifest xml

    <?xml version="1.0" encoding="utf-8"?>
<!--suppress ALL -->
<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>

我不知道你需要什么,所以我把它全部包括在内。

2 个答案:

答案 0 :(得分:0)

您从未向Generateeinleitung分配任何内容。这两个都是null,当你调用它们的任何方法时,你将获得NullPointerException

致电setContentView(R.layout.activity_main)后,您需要

generate = (Button) findViewById(R.id.button);
einleitung = (TextView) findViewById(R.id. textview1);

编辑:在OnCreateOptionsMenu中摆脱那些东西。它只应用于菜单内容,而不是获取对视图的引用。

(除此之外:您还应该在活动中将按钮名称更改为generate,因为大写单词通常表示班级名称。)

答案 1 :(得分:0)

您正在onCreateOptionsMenu中使用findViewById来初始化本地变量。它不会为您的实例变量分配对象,因此您将在以下行中获得NullPointerException:

Generate.setOnClickListener(new OnClickListener() {

修改下面给出的代码,它会起作用。

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 the instance variables
        Generate = (Button) findViewById(R.id.button);
        einleitung = (TextView) findViewById(R.id.textview1);

        final String[] myNames = {"Call the 3rd  person in your Phonebook and ask for activities", "Drive 5 miles with your car just for fun", "Call the 14th person in your Phonebook and ask for activities ",
                "Send a message at whatsapp to a random people in your contacts", "Do a workout",
                "Play old games like n64,playstation 1 or whatever", "Make funny selfies", "Cook something you want", "Watch some films", "Make fresh orange juice ",
                "Climb on a tree", "See how long you can hold a note", "Try to not think about penguins", "Pretend to be a car", "Make Star Trek door noises", "Watch TV, repeat everything said in Italian accent",
                "Buy a postcard an send it to your friend", "Buy a unicorn", "Put a walkie talkie in your mail box and shout at everybody who walks by", "Sing the Llama song till you've annoyed everyone in the room",
                "Start talking in a made up language", "Call your friend, and tell them you can't talk right now, cause your busy", "Go into an elevator with a piece of chalk, draw a box and declare the area your personal space"
                , "Put vanilla pudding in a jar that says MAYO and then eat it in a public place", "go to kfc dressed like a gangster and ask if they have black fried chicken until they kick you out"
                , "Dance around your 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;
    }
}

编辑1: 我没注意到这个: 在您的清单文件中,活动名称提供不正确。检查班级名称。

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

除此之外,删除“import infuso.choicemaster.R;”来自Java代码。

编辑2: 项目的工作样本: Choice Master