java - 不幸的是,应用程序已停止,错误为零

标签 java android

即使我 100% 确定没有错误,但这些应用程序都无法运行。 当我启动该应用程序时,它只是告诉我它已停止。这是我的代码

这是 Activity

TextPlay.java

package com.example.txt;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class TextPlay extends Activity{

Button chkCmd = (Button) findViewById(R.id.bResults);
ToggleButton passTog = (ToggleButton) findViewById(R.id.tbPassword);
EditText input = (EditText) findViewById(R.id.etCommands);
TextView display = (TextView) findViewById(R.id.tvResults);

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text);
}

布局

text.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:padding="25dp"
tools:context=".TextPlay" >

<EditText
    android:id="@+id/etCommands"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/edittext1"
    android:inputType="textPassword" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100" >

    <Button
        android:id="@+id/bResults"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="20"
        android:text="@string/button1" />

    <ToggleButton
        android:id="@+id/tbPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="80"
        android:checked="true"
        android:paddingBottom="10dp"
        android:text="@string/toggle1" />
</LinearLayout>

<TextView
    android:id="@+id/tvResults"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/tv1" />

 </LinearLayout>

和 list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.txt"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".TextPlay"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

最佳答案

尝试在 onCreate 中移动 View 的初始化

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text);

    Button chkCmd = (Button) findViewById(R.id.bResults);
    ToggleButton passTog = (ToggleButton) findViewById(R.id.tbPassword);
    EditText input = (EditText) findViewById(R.id.etCommands);
    TextView display = (TextView) findViewById(R.id.tvResults);
}

编辑:

如果您需要对象在类范围内可访问,请在方法之外声明它们 并在方法中初始化它们:

Button chkCmd;
ToggleButton passTog;
EditText input;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text);

    chkCmd = (Button) findViewById(R.id.bResults);
    passTog = (ToggleButton) findViewById(R.id.tbPassword);
    input = (EditText) findViewById(R.id.etCommands);
    display = (TextView) findViewById(R.id.tvResults);
}

关于java - 不幸的是,应用程序已停止,错误为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27589149/

相关文章:

java - 为什么从 java 9 PhantomReference java doc 开始,它专门用于 POST-mortem 清理操作,尽管它之前是 PRE-mortem

java - Spring OAuth2.0 : Where does spring store the access token?

java - API文档的使用方法

android - 在 onActivityResult 中收到 "content://"Uri 后从 MediaStore 获取 PDF?

android - 获取图像的 "best guess"信息,如谷歌反向图像搜索

android - react 原生 : How to open OS settings in Android and iOS?

java - java中的抽象类不具有任何值

java - 使用 Camel 和 Bindy 检查编码的 Pojo

javascript - Phonegap android ajax 调用在浏览器中工作,而不是在移动设备上工作

android - "ISO-8859-1"字符串的 SharedPreferences 有问题吗?