2015-07-21 159 views
-3

我是相當新的編碼,但決定我會採取巨大的跳水進入海洋而不知道如何游泳,無論如何,當我點擊時,我的按鈕不斷打開我的新活動它,而只是崩潰。當我在模擬器上運行它時,我收到一條消息說它已停止。我正在使用genymotion模擬器和darcula android studio版本。當我點擊我的按鈕我的應用程序崩潰

我主要activity.xml:

<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="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 
    android:background="@drawable/background_black"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/unpressed_button" 
     android:background="@drawable/unpressed" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     style="?android:attr/borderlessButtonStyle" /> 

</RelativeLayout> 

我的第二個Activity.xml:

<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:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:background="@drawable/background_black" 
    tools:context="quirkykoders.flash_flash.Pressed_Button"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/pressed_button" 
     android:background="@drawable/pressed" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     style="?android:attr/borderlessButtonStyle" /> 


</RelativeLayout> 

我的清單

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

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      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=".Pressed_Button" 
      android:label="@string/title_activity_pressed__button" > 
      <intent-filter> 
       <action android:name="android.intent.action.PRESSED" /> 

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

</manifest> 

我的Java

package quirkykoders.flash_flash; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 


public class MainActivity extends AppCompatActivity { 
    private static Button button_sbm; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     OnClickButtonListener(); 
    } 

    public void OnClickButtonListener() { 
    button_sbm = (Button)findViewById(R.id.unpressed_button); 
    button_sbm.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Intent intent = new Intent ("quirkykoders.flash_flash.Pressed_Button"); 
        startActivity(intent); 
       } 
      } 
    ); 

    } 
} 

任何幫助,將不勝感激

+0

你在android studio中收到錯誤嗎?如果是這樣,請發貼 – GregH

+0

我認爲問題出在Intent上,爲什麼要使用這個意圖?它是否打開secondActivity? –

+0

請參閱此鏈接:http://developer.android.com/reference/android/content/Intent.html#Intent(java.lang.String) 您正在傳遞活動的名稱作爲操作。你必須使用不同的構造函數:new Intent(MainActivity.this,Pressed_Button.class); – krystian71115

回答

1

當你想創建你必須發送兩個參數在意向對象首先是currentActivity名字一個新的活動,第二個是CallingActivity名

Intent intent=new Intent(MainActivity.this,Pressed_Button.class); 
    startActivity(intent); 
相關問題