2014-03-29 71 views
0

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

} 

清單:

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

看看logcat中的異常stacktrace並編輯你的問題來包含它。 – laalto

回答

3

更改此

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> 

使用明確意向的

知道爲什麼讀

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

明確意圖指定組件的名稱( 完全合格的類名)開始。您通常會使用明確的目標 在您自己的應用中啓動組件,因爲您知道要啓動的活動或服務的類名 。例如,啓動新的 活動以響應用戶操作或啓動服務以在後臺下載 文件。

+0

'Intent i = new Intent(「Start.this,Set.class」);'不應該沒有''' – donfuxx

+0

@donfuxx它的錯誤將修復 – Raghunandan

+0

我已經改變了程序,但它仍然不起作用,你還有什麼建議嗎? – user3476222

相關問題