简体   繁体   中英

The app I am working on crashes when i try to go from one intent to the other, I've tried everything. code:

The Start class: It as a very simple program, i designed two screen, and by pressing a button on the main screen i wanted the app to open the second screen, but unfortunately its not happening, the app keeps crushing over and over again.

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

}

The Set class:

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

}

The manifest:

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

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

Change this

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

To

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

And Change this

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

To

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

Use Explicit Intent's

To know why read

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM