我正在尝试编写一个按钮,首先创建一个文件,然后使用电子邮件提供程序发送它,但我一直在logcat中收到此错误:
Caused by: java.lang.NoSuchMethodException: clickedUpdate [class android.view.View]
我确信我得到了一些真正微不足道的错误,但我似乎无法找到它是什么,我浏览了其他问题,但它们并不适用于我的情况。顺便说一句,这段代码放在onCreate方法之上和常规活动中。
的java:
public void clickedUpdate(Context cn, View view)
{
TextView dLong = (TextView) findViewById (R.id.textLong);
TextView dLat = (TextView) findViewById (R.id.textLat);
String dataLat = dLat.getText().toString();
String dataLong = dLong.getText().toString();
boolean UpdateResume;
if(!(dataLat.equals("") && !(dataLong.equals(""))))
{
UpdateResume = true;
}
else
{
UpdateResume = false;
}
TelephonyManager telephonemanager =(TelephonyManager)cn.getSystemService(Context.TELEPHONY_SERVICE);
String PhoneNumber = telephonemanager.getLine1Number();
File DataDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"Android/LocationData");
if(!DataDir.exists())
{
try
{
DataDir.mkdir();
}
catch (Exception e)
{
e.printStackTrace();
}
}
File Data = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"BlogData" + File.separator+"Locationer.txt");
if(!Data.exists()) {
try {
Data.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
while (UpdateResume = true)
{
if (Data.exists())
{
try
{
FileWriter fileWriter = new FileWriter(Data);
BufferedWriter bfWriter = new BufferedWriter(fileWriter);
bfWriter.write(PhoneNumber + "," + dataLat + "," + dataLong);
bfWriter.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
break;
}
}
Intent emailintent = new Intent(Intent.ACTION_SEND);
emailintent.setType("text/plain");
emailintent.putExtra(Intent.EXTRA_EMAIL, new String[]{"xxxxxxxxx@xxxxxxxx.com"});
emailintent.putExtra(Intent.EXTRA_SUBJECT, "Data");
emailintent.putExtra(Intent.EXTRA_TEXT, "Hello World!");
File root = Environment.getExternalStorageDirectory();
String DataAttachment = "Android/LocationData/Locationer.txt";
File filer = new File(root, DataAttachment);
if (!filer.exists() || filer.canRead())
{
return;
}
Uri uri = Uri.fromFile(filer);
emailintent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailintent, "Choose an Email provider"));
}
XML
<Button
android:id="@+id/updatebtn"
android:onClick="clickedUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/contactbtn"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/buttonsettings"
android:paddingBottom="80dp"
android:text="@string/updatenow"
android:textColor="#ffffff"
android:textSize="18sp" />
答案 0 :(得分:2)
方法签名应该是这样的:
public void clickedUpdate(View view)
鉴于您已添加其他参数(Context
),Android无法通过反射找到正确的方法。删除它,你的代码应该工作。
答案 1 :(得分:0)
您的clickedUpdate
方法只能包含View
参数。如果您绝对需要使用传入的Context
变量变量,则可以将其作为活动中的实例变量。