我试图使用意图,但点击任何按钮,应用程序停止工作和终止。我是一个初学者,我找不到原因。我提供我的xml文件,java文件和menifest文件。请有人帮忙。 这是我的xml文件:
#include <iostream>
array[] = { "Obj1", "Obj2", "Obj3" };
int i;
class Class {
public:
string name; // the name of the object
int age; // the age of the object
}
int main() {
Class Obj1;
Class Obj2;
Class Obj3;
Obj1.name = "Judy";
Obj1.age = 29;
Obj2.name = "Mike";
Obj2.age = 38;
Obj3.name = "Dorothy";
Obj3.age = 19;
for (i = 0; i < (sizeof(array) / sizeof(array[0])); i++) {
cout << array[i].name << endl;
cout << array[i].age << endl;
}
}
这是我的.java文件:
<?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>
最后这是我的清单文件:
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);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
答案 0 :(得分:3)
您必须在View view
方法中添加参数onClick
。
例如:
public void setAlarm(View view){
}
将该参数添加到其他onClick
方法中:email
和seeMap
。
在幕后,系统使用反射来确定onClick
中的确切Activity
方法,并且系统使用以下确切模式来查找方法:public void
方法xml onClick
属性中指定的方法名称,并且具有单个参数View
。