我在 2 个地方遇到错误,但我不知道为什么,因为我是 Android 新手...
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
Context context=getApplication();
CharSequence text = "This is for ontouch event";
int duration = Toast.LENGTH_SHORT;
Toast Msg = Toast.makeText(context,text,duration);
int x=(int)event.getX();
int y=(int)event.getY();
Msg.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
Msg.show();
return true;
} //Error type Syntax error on token "}", delete this token
TextView t1=(TextView)findViewById(R.id.text);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
} // Error Syntax error, insert "}" to complete ClassBody
最佳答案
在 onCreate()
方法中编写 TextView 代码,您不能按照您编写的方式在任何函数之外编写此代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1=(TextView)findViewById(R.id.text);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
}
关于java - token "}"上的类型语法错误,删除此 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17334860/