为什么我的Android应用程序意外停止?
我是Android编程新手,下面的程序是我的第一个Android应用程序为什么我的Android应用程序意外停止?,android,Android,我是Android编程新手,下面的程序是我的第一个Android应用程序 我的.java文件 public class MainActivity extends Activity { Button btn = (Button) findViewById(R.id.btn); private OnClickListener btn_click = new OnClickListener() { public void onClick(View v) {
我的
.java
文件
public class MainActivity extends Activity
{
Button btn = (Button) findViewById(R.id.btn);
private OnClickListener btn_click = new OnClickListener() {
public void onClick(View v) {
btn.setText("Hi there!");
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
btn.setOnClickListener(btn_click);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Button" />
</RelativeLayout>
我的.XML
文件
public class MainActivity extends Activity
{
Button btn = (Button) findViewById(R.id.btn);
private OnClickListener btn_click = new OnClickListener() {
public void onClick(View v) {
btn.setText("Hi there!");
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
btn.setOnClickListener(btn_click);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Button" />
</RelativeLayout>
我想一旦我点击按钮我的按钮
它的文本就会变成“你好!”。但当我的应用程序在emulator中运行时,它会中断并弹出一条消息:应用程序testapp_1(process com.example.testapp_1)意外停止
我的错误是什么?我怎样才能修复它们 在
setContentView
之后移动初始化
Button btn;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn); // initalize button after setContentView
btn.setOnClickListener(btn_click);
}
您需要先将内容设置为活动,然后初始化视图。您在创建时在外部初始化了
onCreate
。您将获得NullPointerException
。您可以findViewById
将当前视图层次结构设置为活动。在setContentView
之后移动初始化
Button btn;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn); // initalize button after setContentView
btn.setOnClickListener(btn_click);
}
您需要先将内容设置为活动,然后初始化视图。您在创建时在外部初始化了
onCreate
。您将获得NullPointerException
。您可以查找设置为活动的当前视图层次结构的视图ID
。@ABFORCE很高兴它有所帮助。如果有助于在网站允许的情况下接受答案。记住在崩溃发生时发布堆栈跟踪。将记录有用的信息。此堆栈跟踪在哪里?请注意,我在Eclipse中使用emulator。您可以从Eclipse转到windows-->开放透视-->ddms-->logcat中的logcat
@ABFORCE获取堆栈跟踪。@ABFORCE很高兴它有帮助。如果有助于在网站允许的情况下接受答案。记住在崩溃发生时发布堆栈跟踪。将记录有用的信息。此堆栈跟踪在哪里?请注意,我在Eclipse中使用emulator您可以从Eclipse转到windows-->打开透视-->ddms-->logcat中的logcat
@ABFORCE获取堆栈跟踪。Button btn=(Button)findViewById(R.id.btn)代码>您还需要将其移动到方法中。所有Java可执行代码都必须存在于方法中。根据您想要的可见性,可能会将其放入onCreate
。Button btn=(Button)findviewbyd(R.id.btn)代码>您还需要将其移动到方法中。所有Java可执行代码都必须存在于方法中。根据您想要的可见性,可能会将其放入onCreate
。