java - 当我尝试从一种 Intent 转向另一种 Intent 时,我正在开发的应用程序崩溃了,我已经尝试了一切。代码 :

标签 java android

开始类(class): 它是一个非常简单的程序,我设计了两个屏幕,通过按主屏幕上的按钮,我希望应用程序打开第二个屏幕,但不幸的是它没有发生,应用程序不断地一遍又一遍地崩溃。

package com.example.snakesnladders;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Start extends Activity implements OnClickListener {
    Button start, settings;
    TextView snakes, and, ladders;
    ImageView snakePic;

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

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    private void init() {
        start = (Button) findViewById(R.id.btStart);
        settings = (Button) findViewById(R.id.btSettings);
        snakes = (TextView) findViewById(R.id.tvSnakes);
        and = (TextView) findViewById(R.id.tvAnd);
        ladders = (TextView) findViewById(R.id.tvLadders);
        snakePic = (ImageView) findViewById(R.id.snakePic);
        start.setOnClickListener(this);
        settings.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.btStart:

            break;
        case R.id.btSettings:
             Intent i = new Intent("com.example.snakesnladders.SET");
             startActivity(i);
            break;
        default: break;
        }
    }

}

Set 类:

package com.example.snakesnladders;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Set extends Activity implements OnClickListener {
    Button sound, difficulty, back;
    TextView settings;
    ImageView snakePic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setscreen);
        init();
    }

    private void init() {
        sound = (Button) findViewById(R.id.btSound);
        difficulty = (Button) findViewById(R.id.btDifficulty);
        back = (Button) findViewById(R.id.btBack);
        settings = (TextView) findViewById(R.id.tvSetPage);
        snakePic = (ImageView) findViewById(R.id.setSnakePic);
        sound.setOnClickListener(this);
        difficulty.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        switch (view.getId()) {
        case R.id.btSound:
            String s = sound.getText().toString();
            if (s.equals("Sound:on")) {
                sound.setText("Sound:off");
                ControlSounds.player.stop();
            } else {
                sound.setText("Sound:on");
                ControlSounds.player.start();
            }
            break;
        case R.id.btDifficulty:

            break;
        case R.id.btBack:
            Intent i = new Intent(Set.this, Start.class);
            startActivity(i);
            finish();
            break;
        }

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

}

list :

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Start"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Set"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.snakesnladders.SET" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

ma​​inscreen.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="175dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvSnakes"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:text="Snakes"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/green"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/tvAnd"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:text="@string/and"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/green"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/tvLadders"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:text="Ladders"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/green"
            android:textSize="30sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="125dp"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btStart"
            android:layout_width="250dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:background="@drawable/buttonshape"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="10"
            android:text="Start New Game"
            android:textColor="#FFFFFF"
            android:textSize="30sp" />

        <Button
            android:id="@+id/btSettings"
            android:layout_width="250dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:background="@drawable/buttonshape"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="10"
            android:text="Settings"
            android:textColor="#FFFFFF"
            android:textSize="30sp" />
    </LinearLayout>

    <ImageView
        android:id="@+id/snakePic"
        android:layout_width="wrap_content"
        android:layout_height="125dp"
        android:layout_gravity="center"
        android:layout_weight="0.47"
        android:background="@color/black"
        android:src="@drawable/snake" />

</LinearLayout>

setscreen.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvSetPage"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:text="Settings"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/green"
            android:textSize="40sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btSound"
            android:layout_width="250dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:background="@drawable/buttonshape"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="10"
            android:text="Sound:on"
            android:textColor="#FFFFFF"
            android:textSize="30sp" />

        <Button
            android:id="@+id/btDifficulty"
            android:layout_width="250dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:background="@drawable/buttonshape"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="10"
            android:text="Difficulty:easy"
            android:textColor="#FFFFFF"
            android:textSize="30sp" />

        <Button
            android:id="@+id/btBack"
            android:layout_width="250dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:background="@drawable/buttonshape"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="10"
            android:text="Back To Menu"
            android:textColor="#FFFFFF"
            android:textSize="30sp" />
    </LinearLayout>

    <ImageView
        android:id="@+id/setSnakePic"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.25"
        android:background="@color/black"
        android:src="@drawable/snake1" />

</LinearLayout>

最佳答案

改变这个

 Intent i = new Intent("com.example.snakesnladders.SET");
 startActivity(i);

Intent i = new Intent(Start.this,Set.class);
startActivity(i);

并改变这个

<activity
        android:name=".Set"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.snakesnladders.SET" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

 <activity
        android:name=".Set"
        android:label="@string/app_name" >
</activity>

使用显式 Intent

要知道为什么阅读

http://developer.android.com/guide/components/intents-filters.html

Explicit intents specify the component to start by name (the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. For example, start a new activity in response to a user action or start a service to download a file in the background.

关于java - 当我尝试从一种 Intent 转向另一种 Intent 时,我正在开发的应用程序崩溃了,我已经尝试了一切。代码 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22734649/

相关文章:

java - 用java实现 vector 乘法

Java:JVM运行main()方法时是否隐式调用构造函数?

java - 从字符串中删除相邻重复项,仅保留一个实例 : java

java - 无法将 map 加载到 Android(三星)手机中

java - 使用 PopupWindow 的 showAtLocation(View, int, int, int) 时遇到问题

Java:打印 int[] 时出现奇怪的异常

java - 构建自定义视频播放器网络浏览器插件的最佳方法是什么?

java - 在启动画面 Android 上包含文本

javascript - 是否可以用 JavaScript 解析 .apk 文件?

android - 如何让用户在应用程序启动时选择启动哪个Activity?