Java Android活动变黑然后崩溃
我最近开始开发应用程序,我得到了第一份工作。这是一个简单的应用程序,带有一个徽标和一个链接到网站的按钮。每次我运行它时,它都会崩溃,我不知道为什么,因为它是一个如此简单的程序。我花了很多时间寻找类似的问题,但都没有用。我还经历了eclipse并消除了任何编译器警告。有人知道什么地方可能出了问题吗 错误消息: 我一直在一次又一次地删减应用程序最简单的功能。还是不行。这是我的.java和主要活动。(这是一个一活动一类的应用程序)Java Android活动变黑然后崩溃,java,android,Java,Android,我最近开始开发应用程序,我得到了第一份工作。这是一个简单的应用程序,带有一个徽标和一个链接到网站的按钮。每次我运行它时,它都会崩溃,我不知道为什么,因为它是一个如此简单的程序。我花了很多时间寻找类似的问题,但都没有用。我还经历了eclipse并消除了任何编译器警告。有人知道什么地方可能出了问题吗 错误消息: 我一直在一次又一次地删减应用程序最简单的功能。还是不行。这是我的.java和主要活动。(这是一个一活动一类的应用程序) 此行按钮=(按钮)findViewById(R.id.button1
此行
按钮=(按钮)findViewById(R.id.button1)代码>应创建一个错误,这将导致应用程序崩溃。您设置了一个变量,该变量包含一个应该在onCreate
method中的方法
请尝试以下方法:
// init your variable only
Button button;
// Then in onCreate, as you already did:
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
// set findViewById method only here
button = (Button) findViewById(R.id.button1);
// ...
}
让我知道这是否有帮助。“和崩溃”logcat中有关于崩溃类型的任何信息吗?@StevenV我在使用模拟器时遇到问题,所以我只是构建了apk并将其安装在我的手机上。您应该仍然能够使用adb使用apk连接到设备,并查看logcat@史蒂文:你说得对。请原谅一个愚蠢的高中生!我认为@Fllo在他的回答中找到了一些线索。堆栈跟踪显示findViewById()
正在抛出NullReferenceException
。
<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: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=".FullscreenActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true"
android:src="@drawable/switchuplogo" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="94dp"
android:text="View Website" />
</RelativeLayout>
// init your variable only
Button button;
// Then in onCreate, as you already did:
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
// set findViewById method only here
button = (Button) findViewById(R.id.button1);
// ...
}