刚刚开始Android开发,我无法弄清楚为什么它不会起作用。这是我得到的错误(粗体块引用前的最后一行):
package com.ample.ballv2;
import java.util.Random;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Button answer = (Button) findViewById(R.id.answerbutton);
answer.setOnClickListener(answerL);
}
private OnClickListener answerL = new OnclickListener(){
public void onClick(view v) {
Random token = random();
int tokenno = token.nextInt(10);
CharSequence ans = "";
switch(tokenno)
{case 0:ans=getString(R.string.msg0);break;
case 1: ans=getString(R.string.msg1);break;
case 2: ans=getString(R.string.msg2);break;
case 3: ans=getString(R.string.msg3);break;
case 4: ans=getString(R.string.msg4);break;
case 5: ans=getString(R.string.msg5);break;
case 6: ans=getString(R.string.msg6);break;
case 7: ans=getString(R.string.msg7);break;
case 8: ans=getString(R.string.msg8);break;
case 9: ans=getString(R.string.msg9);break;
case 10:ans=getString(R.string.msg10);break;
}
Context context = getApplicationContext();
Toast msg = Toast.makeText(context,ans,Toast.LENGTH_LONG);
msg.show();
}
}//error's popping up here
>描述资源路径位置类型 语法错误,插入";"完成FieldDeclaration MainActivity.java / 8ballv2 / src / com / ample / ballv2第56行Java问题
@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 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
答案 0 :(得分:1)
好吧,我在IDE中尝试了你的代码,问题 - 在提供的示例中 - 是在变量声明中:
private OnClickListener answerL = new OnClickListener(){
public void onClick(view v) {
Random token = random();
int tokenno = token.nextInt(10);
CharSequence ans = "";
switch(tokenno) {
case 0:ans=getString(R.string.msg0);break;
case 1: ans=getString(R.string.msg1);break;
case 2: ans=getString(R.string.msg2);break;
case 3: ans=getString(R.string.msg3);break;
case 4: ans=getString(R.string.msg4);break;
case 5: ans=getString(R.string.msg5);break;
case 6: ans=getString(R.string.msg6);break;
case 7: ans=getString(R.string.msg7);break;
case 8: ans=getString(R.string.msg8);break;
case 9: ans=getString(R.string.msg9);break;
case 10:ans=getString(R.string.msg10);break;
}
Context context = getApplicationContext();
Toast msg = Toast.makeText(context,ans,Toast.LENGTH_LONG);
msg.show();
}
}//here should be ';'
发生这种情况是因为View.OnClickListener具有带方法的主体。如果没有方法(例如,您创建了
私有类MyOnClickListener实现OnClickListener
(http://developer.android.com/reference/android/view/View.OnClickListener.html) 那会是这样的:
private MyOnClickListener answerL = new MyOnClickListener(); //<- semicolon here.
您可以在将来使用autoformating工具(在Android / ADT中默认使用ctrl + shift + f和在AndroidStudio中使用ctrl + alt + l(或类似))或在方法之前/之后严格声明类字段。
希望有所帮助)
答案 1 :(得分:0)
正如它说你需要在那里插入一个分号,因为你在这里声明了一个字段。所以这就应该是
private OnClickListener answerL = new OnclickListener(){
public void onClick(view v) {
...............
}//onClick ends here
}; //OnClickListener ends here
答案 2 :(得分:0)
OnclickListener
是interface
,你应该像这样创建它的对象:
private OnClickListener answerL = new OnclickListener(){
public void onClick(view v) {
//do stuffs here
}
};
请注意最后的semi-colon
。