即使我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>
答案 0 :(得分:2)
尝试在onCreate
中移动视图的init@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);
}