Android开发中的一个简单的问题,求帮忙看下怎么解决?
要创建一个最简单的helloworld安卓应用,把MainActivity.java文件改为如下packagecom.example.helloworld;importa...
要创建一个最简单的helloworld安卓应用,把MainActivity.java文件改为如下
package com.example.helloworld;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
public 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;
}
public void clickHandler(View source) { //这一行提示错误View cannot be resolved to a type
// 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.
TextView tv=(TextView)findViewById(R.id.show); //提示错误show cannot be resolved or is not a field
tv.setText("Hello Android="+new java.util.Date());
}
}
怎么回事 展开
package com.example.helloworld;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
public 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;
}
public void clickHandler(View source) { //这一行提示错误View cannot be resolved to a type
// 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.
TextView tv=(TextView)findViewById(R.id.show); //提示错误show cannot be resolved or is not a field
tv.setText("Hello Android="+new java.util.Date());
}
}
怎么回事 展开
2个回答
展开全部
①如果只是简单的程序一般继承的是Activity,而不是ActionBarActivity
②如果你的这个clickHandler方法是继承父类的方法,而不是自己写的话,一般要在上面加上@Override。
③一般初始化控件,可以直接写在setContentView(R.layout.activity_main);语句后面;如果要单独写一个方法的话,一般不需要带参数。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText("Text");
}
②如果你的这个clickHandler方法是继承父类的方法,而不是自己写的话,一般要在上面加上@Override。
③一般初始化控件,可以直接写在setContentView(R.layout.activity_main);语句后面;如果要单独写一个方法的话,一般不需要带参数。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText("Text");
}
展开全部
package com.example.helloworld;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickHandler();
}
@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;
}
public void clickHandler() { //这一行提示错误View cannot be resolved to a type
// 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.
TextView tv=(TextView)findViewById(R.id.show); //提示错误show cannot be resolved or is not a field
tv.setText("Hello Android="+new java.util.Date());
}
}
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickHandler();
}
@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;
}
public void clickHandler() { //这一行提示错误View cannot be resolved to a type
// 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.
TextView tv=(TextView)findViewById(R.id.show); //提示错误show cannot be resolved or is not a field
tv.setText("Hello Android="+new java.util.Date());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询