2014-12-21 127 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-stopped-how-can-i-solve-this – laalto

回答

2

嘗試移動瀏覽的初始化裏面的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); 
} 

編輯:

,如果你需要的對象可訪問類的範圍,申報出來的方法 的,並在方法初始化它們:

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

It Worked。但如果我將它們添加到onCreate它不會是本地的,對吧? –

+0

@MohamadNabeel我已經更新了答案,如果你需要它們的類範圍,你可以聲明它們爲成員變量,並在'onCreate() – Yazan

相關問題