2017-08-11 75 views
-1

我試圖使用意圖,但點擊任何按鈕,應用程序停止工作,終止。我是一個初學者,我找不到原因。我提供我的XML文件,Java文件和清單文件。請有人幫忙。 這裏是我的xml文件:爲什麼我的應用程序在點擊任何按鈕時崩潰?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.example.shreya.intents.MainActivity"> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Set Alarm" 
     android:id="@+id/alarm" 
     android:onClick="setAlarm"/> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Map" 
     android:id="@+id/map" 
     android:onClick="seeMap"/> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Mail" 
     android:id="@+id/mail" 
     android:onClick="email"/> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Email ID" 
     android:inputType="textCapWords" 
     android:id="@+id/address"/> 

</LinearLayout> 

這裏是我的.java文件:

package com.example.shreya.intents; 

import android.content.Intent; 
import android.net.Uri; 
import android.provider.AlarmClock; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import java.util.Locale; 



import static com.example.shreya.intents.R.id.alarm; 

public class MainActivity extends AppCompatActivity { 

    private Button mail; 

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

    public void setAlarm(){ 
     String message="Wake Up"; 
     int hour=7; 
     int minutes=0; 
     Intent intent = new Intent(AlarmClock.ACTION_SHOW_ALARMS); 
     if (intent.resolveActivity(getPackageManager()) != null) { 
      startActivity(intent); 
     } 
    } 

    public void seeMap(){ 
     String uri = String.format(Locale.ENGLISH, "geo:%f,%f", 28.699884, 77.273075); 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setData(Uri.parse(uri)); 
     if(intent.resolveActivity(getPackageManager())!=null) 
      startActivity(intent); 
    } 

    public void email(){ 
     EditText mail=(EditText)findViewById(R.id.address); 
     String add=mail.getText().toString(); 
     composeEmail(add,"shreya"); 
    } 

    public void composeEmail(String addresses, String subject) { 
     Intent intent = new Intent(Intent.ACTION_SENDTO); 
     intent.setData(Uri.parse("mailto:")); // only email apps should handle this 
     intent.putExtra(Intent.EXTRA_EMAIL, addresses); 
     intent.putExtra(Intent.EXTRA_SUBJECT, subject); 
     intent.putExtra(Intent.EXTRA_TEXT,"hello"); 
     if (intent.resolveActivity(getPackageManager()) != null) { 
      startActivity(intent); 
     } 
    } 

} 

最後,這是我menifest文件:

<?xml version="1.0" encoding="utf-8"?> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    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> 
</application> 

+2

請提供崩潰的stacktrace/logcat – Thomas

回答

3

您必須將參數View view添加到onClick方法中。

例如:

public void setAlarm(View view){ 

} 

該參數添加到其他onClick方法也:emailseeMap

引擎蓋下,系統使用反射來找出在Activity確切onClick方法和系統使用以下精確的模式找到方法:一個public void方法具有指定方法名在XML onClick屬性,並且具有一個參數View

相關問題