我收到错误 消息“无法解析符号“ OnClickListener” 和 “无法解析符号“ V””, 我开始使用Java,因此我不太擅长此语言,并且对语言不太熟悉
我的代码:
package com.emiliogaines.counter.counter; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button mButton = (Button) findViewById(R.id.More); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final TextView mTextView = (TextView) findViewById(R.id.Antal); mTextView.setText("Some Text"); } }); @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar // if it is present. getMenuInflater().inflate(R.menu.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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
我不确定您为什么要这样做,但请将初始化和侦听器移至onCreate():
onCreate()
只需剪切以下代码行:
Button mButton = (Button) findViewById(R.id.More); mButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { final TextView mTextView = (TextView) findViewById(R.id.Antal); mTextView.setText("Some Text"); } });
并将它们粘贴到onCreate():
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button mButton = (Button) findViewById(R.id.More); final TextView mTextView = (TextView)findViewById(R.id.Antal); mButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { mTextView.setText("Some Text"); } }); }
然后阅读编码的基础及其标准。