[英]My Android-Emulator displays emulator error
I made an Android Application. 我做了一个Android应用程序。 Eclipse isn't reporting me what errors are there in my code.
Eclipse没有报告我的代码中有哪些错误。
But if I run my project then emulator displays: 但如果我运行我的项目然后模拟器显示:
emulator error http://s2.ipicture.ru/uploads/20121106/466GCZH9.png 模拟器错误http://s2.ipicture.ru/uploads/20121106/466GCZH9.png
My Java code is ( MainActivity.java
): 我的Java代码是(
MainActivity.java
):
package ru.startandroid.develop.AppLog;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
TextView myText = (TextView) findViewById(R.id.myText);
Button myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
Button myBtnOK = (Button) findViewById(R.id.myBtnOK);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.myBtnCancel:
myText.setText("Нажата кнопка Cancel");
break;
case R.id.myBtnOK:
myText.setText("Нажата кнопка ОК");
break;
}
}
}
You can only use findViewById()
after calling setContentView()
: 您只能在调用
setContentView()
后使用findViewById()
setContentView()
:
public class MainActivity extends Activity implements OnClickListener{
TextView myText;
Button myBtnCancel;
Button myBtnOK;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView) findViewById(R.id.myText);
myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
myBtnOK = (Button) findViewById(R.id.myBtnOK);
}
You need to bind the Widgets (TextView and Buttons) calling findById
after the setContentView
method, because depends of it: 您需要在
setContentView
方法之后绑定调用findById
的Widgets(TextView和Buttons),因为它取决于它:
TextView myText;
Button myBtnCancel;
Button myBtnOK;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView) findViewById(R.id.myText);
myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
myBtnOK = (Button) findViewById(R.id.myBtnOK);
}
Change your code like below: 更改您的代码如下:
Declare variables at instance level and assign values inside onCreate()
在实例级别声明变量并在
onCreate()
指定值
public class MainActivity extends Activity implements OnClickListener{
TextView myText = null;
Button myBtnCancel = null;
Button myBtnOK = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView) findViewById(R.id.myText);
myBtnCancel = (Button) findViewById(R.id.myBtnCancel);
myBtnOK = (Button) findViewById(R.id.myBtnOK);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.