通过按钮点击尝试多个活动

时间:2016-04-09 23:54:09

标签: java android android-layout android-studio

我正在编写一个应用程序,它将在vape商店中显示果汁菜单。 我有一个显示名称的主屏幕和一个点击按钮将用户发送到显示菜单的新屏幕。 我在设置按钮单击时遇到问题,当我运行应用程序时,单击按钮后它会崩溃

任何输入都将不胜感激!

MainActivity

package com.example.vitoriano_vaz.easybayvapes;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;




public class MainActivity extends AppCompatActivity {

    public void sendMessage(View view){
        Intent intent = new Intent(FromActivity.this, ToActivity.class);
        startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void startJuiceMenu(View view) {
        Log.d("MyApp", "button clicked");


    }
}

点击按钮后的第二个活动

package com.example.vitoriano_vaz.easybayvapes;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;

public class juiceMenu extends AppCompatActivity {
    ArrayList<String> juiceMenu = new ArrayList<String>(50);

    private static String VALUE = "myValue";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        juiceMenu.add("#1 Blueberry Bombshell");
        juiceMenu.add("#2 Richie Rich");
        juiceMenu.add("#3 Chiquita");
        juiceMenu.add("#4 Afternoon Delight");
        juiceMenu.add("#5 Poppin Otters");
        juiceMenu.add("#6 Viva La Sangria");
        juiceMenu.add("#7 Okole Maluna");
        juiceMenu.add("#8 Carmen Miranda");
        juiceMenu.add("#9 Pomalade");
        juiceMenu.add("#10 Izual");
        juiceMenu.add("#11 Butter Stotch ");
        juiceMenu.add("#12 Blue Bulls");
        juiceMenu.add("#13 Grape Ape");
        juiceMenu.add("#14 Bruce Juice");
        juiceMenu.add("#15 Doc Holiday");
        juiceMenu.add("#16 Peachy Keen");
        juiceMenu.add("#17 Hula");
        juiceMenu.add("#18 New York");
        juiceMenu.add("#19 Al Gore");
        juiceMenu.add("#20 Lux Charms");
        juiceMenu.add("#21 Sailor jack");
        juiceMenu.add("#22 Get Him to the Greek");
        juiceMenu.add("#23 Key We Lie Chi");
        juiceMenu.add("#24 Spring Fling");
        juiceMenu.add("#25 Gumby");
        juiceMenu.add("#26 Chai-Milk");
        juiceMenu.add("#27 Mr. Bean");
        juiceMenu.add("#28 50 Shades of Orange");
        juiceMenu.add("#29 Blue Waffles");
        juiceMenu.add("#30 Enigma");
        juiceMenu.add("#31 Mr. Freeze");
        //juiceMenu.add("#32 New Flavor"); need to update to get newest flavor



  }


}

Activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.vitoriano_vaz.easybayvapes.MainActivity"
    android:id="@+id/main_view">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:text="Welcome to East Bay Vapes"
        android:textAllCaps="true"
        android:textColor="#000000"
        android:textSize="20sp"
        android:id="@+id/textView" />
    <Button
        android:id="@+id/juicemenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sendMessage"
        android:text="@string/JuiceMenu"
        android:layout_marginTop="42dp"
        android:layout_alignParentTop="true" />


</RelativeLayout>

Activity_juice_menu 这里我有一个我在juicemenu.java类中声明的ArrayList的LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.vitoriano_vaz.easybayvapes.juiceMenu">

    <ListView
        android:id="@+id/JuiceMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        >

    </ListView>


</LinearLayout>

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.vitoriano_vaz.easybayvapes">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".juiceMenu"></activity>
    </application>

</

2 个答案:

答案 0 :(得分:0)

您正在构建的Intent需要引用您自己的Activity类。 第一个参数是Context,第二个参数是您要启动的Class的{​​{1}},因此它应该是以下内容。

Activity

答案 1 :(得分:0)

由于您的问题已经得到解答,我认为我建议使用更有效的方法填充数组,以消除代码中的瑕疵。例如,您可以使用字符串数组资源。有了它,您只需使用单个代码行添加值。举个例子:

  

String [] juiceArray = getResources()。getStringArray(R.array.JuiceTextArray);

数组值本身填充在arrays.xml文件中(在values文件夹中找到),如下所示:

<resources 
   xmlns:tools="http://schemas.android.com/tools" 
   tools:ignore="MissingTranslation"> 

    <string-array name="JuiceTextArray" tools:ignore="MissingTranslation">
        <item name="Juice1">#1 Blueberry Bombshell.</item>
        <item name="Juice2">#2 Richie Rich.</item>
        [etc., etc.]
   </string-array>
</resources>