2014-12-21 2 views
3

Ни одно из этих приложений не срабатывало, хотя я на 100% уверен, что ошибок нет. Когда я запускаю приложение, он просто говорит мне, что он остановился. Вот мой кодК сожалению, приложение остановилось с нулевыми ошибками

Это деятельность

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> 

и манифест

<?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> 
+1

http://stackoverflow.com/ вопросы/23353173/К сожалению, myapp-has-stop-how-can-i-solve-this – laalto

ответ

2

попробуйте подвигать Init просмотров в OnCreate

@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); 
} 

EDIT:

если вам нужны предметы, чтобы быть доступной классовой сфера, объявить их из метода и инициализировать их в методе:

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); 
} 
+0

Работает. Но если бы я добавил их в onCreate, он не будет локальным, не так ли? –

+0

@MohamadNabeel Я обновил ответ, если вам понадобится класс-scope, вы можете объявить их как переменные-члены и инициализировать их внутри 'onCreate()' – Yazan

Смежные вопросы