[英]App is Force Closing but I can't figure out why
I can't get an emulator working on my slow computer so I have to go through the process of loading my app into a physical device. 我无法在速度较慢的计算机上运行模拟器,因此必须完成将应用程序加载到物理设备的过程。 Well it compiles all fine and I can't notice any obvious error but it doesn't want to run.
好吧,它可以很好地编译,我看不到任何明显的错误,但它不想运行。 It started happening after I followed a tutorial on how to go from page to another.
在我遵循了如何从页面转到另一个教程之后,它开始发生。 Everything looks good in comparison to the Tutorial.
与本教程相比,一切看起来都不错。
Manifest File 清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mcesfireassist"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" android:hardwareAccelerated="false">
<activity
android:name="com.example.mcesfireassist.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".App2Activity" >
</activity>
</application>
</manifest>
Main XML 主要XML
<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"
android:background="@layout/activity_main"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/home_intro" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:ems="10"
android:inputType="number"
android:singleLine="true"
android:text="@string/Unit_num" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="textNoSuggestions|textShortMessage"
android:text="@string/asn" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="@string/start_button" />
</RelativeLayout>
Main Java File 主Java文件
package com.example.mcesfireassist;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button button;
@Override
protected 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 addListenerOnButton() {
final Context context = this;
button = (Button) findViewById (R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, App2Activity.class);
startActivity(intent);
}
});
}
}
If you would like to see the java and xml of the second page I can add it, I just didn't wanna make the post any longer then necessary. 如果您想查看第二页的Java和xml,可以添加它,那只是我不想再发布该帖子了。
You code is not functioning because you never call the addListenerOnButton()
method. 您的代码无法正常工作,因为您从未调用过
addListenerOnButton()
方法。 An OnClickListener
is never attached to the Button
. OnClickListener
永远不会附加到Button
。 In fact, a reference to the button doesn't even exist in your activity. 实际上,您的活动中甚至不存在对该按钮的引用。
You can either call addListenerOnButton()
after setContentView(R.layout.activity_main)
or try the following code that includes a few changes: 您可以在
setContentView(R.layout.activity_main)
addListenerOnButton()
之后调用addListenerOnButton()
,也可以尝试以下包含一些更改的代码:
package com.example.mcesfireassist;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById (R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, App2Activity.class);
startActivity(intent);
}
});
}
@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;
}
}
Edit 1: 编辑1:
Please change the following code: 请更改以下代码:
<activity
android:label="@string/app_name"
android:name=".App2Activity" >
</activity>
to: 至:
<activity
android:label="@string/app_name"
android:name="com.example.mcesfireassist.App2Activity" >
</activity>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.