此代码无法在Android Studio上运行。我只是希望它在单击按钮时显示吐司。请帮忙。代码如下:
我是Android开发的新手,并且很难搞清楚这一点。我认为问题是一个愚蠢的语法错误或未定义的变量。另外,我如何定义" v" ?
package com.shutter2000.www.shutter_2000_oms;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class SiteVisitDetails extends Activity {
public EditText mCusName;
public EditText mOrderID;
public EditText mProjectID;
public EditText mHeightWidth;
public EditText mNote;
public Button mSubmitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_site_visit_details);
//initialize text fields
mCusName = (EditText) findViewById(R.id.txtCusName);
mOrderID = (EditText) findViewById(R.id.txtOrderID);
mProjectID = (EditText) findViewById(R.id.txtProjectID);
mHeightWidth = (EditText) findViewById(R.id.txtHeightWidth);
mNote = (EditText) findViewById(R.id.txtNote);
mSubmitButton = (Button) findViewById(R.id.btnSubmit);
//listen to submit button click event
mSubmitButton.OnClickListener(new View.OnClickListener())
{
@Override
public void onClick(View v){
if (v == mSubmitButton) {
Context context = getApplicationContext();
CharSequence text = "Successfully Sent!";
int duration = Toast.LENGTH_LONG;
final Toast tost = Toast.makeText(context, text, duration);
tost.show();
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.site_visit_details, menu);
return true;
}
public void addItemsToProductSpinner() {
Spinner spinner = (Spinner) findViewById(R.id.spnProducts);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.products, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
android.R.layout.simple_spinner_item
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
错误是:
&#34; mSubmitButton的错误......&#34;
Expected class or package
'OnClickListener' is abstract;cannot be instantiated
错误&#34; @override public void onClick(查看v){...&#34;
Annotations are not allowed here
Identifier or type expected
&#34; public void addItemsToProductSpinner()的错误...&#34;
";" expected
Cannot resolve symbol "v"
unexpected token
答案 0 :(得分:1)
这是错误的v == mSubmitButton
在java中,可以等于operator == only primitive类型。对于对象,您需要使用equals()方法。
替换:
mSubmitButton.onClickListener(new View.OnClickListener())
{
@Override
public void onClick(View v){
if (v.getId() == R.id.btnSubmit) {
Context context = getApplicationContext();
CharSequence text = "Successfully Sent!";
int duration = Toast.LENGTH_LONG;
final Toast tost = Toast.makeText(context, text, duration);
tost.show();
}
}
}
答案 1 :(得分:0)
尝试使用以下代码:
if(v.getId() == R.id.btnSubmit){
Toast.makeText(SiteVisitDetails.this,"Successfully Sent!",Toast.LENGTH_LONG).show();
}
答案 2 :(得分:0)
我认为你不需要检查
@Override
public void onClick(View v){
//Don't need to check condition control come here only v.getId() == R.id.btnSubmit
// if (v == mSubmitButton) {
Context context = getApplicationContext();
CharSequence text = "Successfully Sent!";
int duration = Toast.LENGTH_LONG;
final Toast tost = Toast.makeText(context, text, duration);
tost.show();
//}
}
答案 3 :(得分:0)
package com.shutter2000.www.shutter_2000_oms;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class SiteVisitDetails extends Activity implements OnClickListener{
public EditText mCusName;
public EditText mOrderID;
public EditText mProjectID;
public EditText mHeightWidth;
public EditText mNote;
public Button mSubmitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_site_visit_details);
//initialize text fields
mCusName = (EditText) findViewById(R.id.txtCusName);
mOrderID = (EditText) findViewById(R.id.txtOrderID);
mProjectID = (EditText) findViewById(R.id.txtProjectID);
mHeightWidth = (EditText) findViewById(R.id.txtHeightWidth);
mNote = (EditText) findViewById(R.id.txtNote);
mSubmitButton = (Button) findViewById(R.id.btnSubmit);
//listen to submit button click event
mSubmitButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.mSubmitButton) {
Toast.makeText(this,"Successfully sent!",Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.site_visit_details, menu);
return true;
}
public void addItemsToProductSpinner() {
Spinner spinner = (Spinner) findViewById(R.id.spnProducts);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.products, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
android.R.layout.simple_spinner_item
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 4 :(得分:0)
mSubmitButton.setOnClickListener(new View.OnClickListener())
{
@Override
public void onClick(View v){
CharSequence text = "Successfully Sent!";
int duration = Toast.LENGTH_LONG;
Toast tost = Toast.makeText(SiteVisitDetails.this, text, duration);
tost.show();
}
});
使用它。无需检查if(v.getId() == R.id.btnSubmit)
。因为您要对已经初始化的按钮设置单击操作。
答案 5 :(得分:0)
你应该使用setter来设置onclickListener。 当你使用一个新的onclicklistener而你只为你的按钮设置它时,你不需要检查v == mSubmitButton是否因为它是唯一的选择:)
mSubmitButton.setOnClickListener(new View.OnClickListener())
{
@Override
public void onClick(View v){
Context context = getApplicationContext();
CharSequence text = "Successfully Sent!";
int duration = Toast.LENGTH_LONG;
final Toast tost = Toast.makeText(context, text, duration);
tost.show();
}
答案 6 :(得分:0)
mSubmitButton.OnClickListener(
应该是
mSubmitButton.setOnClickListener(
编辑。方法setOnClickListener需要一个实现View.OnClickListener
接口的对象:
mSubmitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
Context context = getApplicationContext();
CharSequence text = "Successfully Sent!";
int duration = Toast.LENGTH_LONG;
final Toast tost = Toast.makeText(context, text, duration);
tost.show();
}
});
答案 7 :(得分:0)
用getApplicationContext()
替换'this'@Override
public void onClick(View v) {
if (v.getId() == R.id.mSubmitButton) {
Toast.makeText(getApplicationContext(),"Successfully sent!",Toast.Length_LONG).show();
}
}
答案 8 :(得分:0)
单击按钮时显示祝酒词。
mSubmitButton = (Button) findViewById(R.id.btnSubmit);
mSubmitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//SiteVisitDetails
Toast tea = Toast.makeText(getApplicationContext(), "Send", Toast.LENGTH_LONG);
tea.show();
}
});
答案 9 :(得分:0)
您的语法中有错误的括号,缺少;
和拼写错误的setOnClickListener
方法名称。
更改
mSubmitButton.OnClickListener(new View.OnClickListener())
{
// ...
}
到
mSubmitButton.setOnClickListener(new View.OnClickListener()
{
// ...
});