我是 android 新手,所以我做了这个简单的应用程序,它接受一个字符串,这是你的名字,如果单击该按钮,它将显示“Hello World, I'm”
现在,问题是它不起作用,它一直给我“应用程序已停止工作。请重试”错误。
这是代码
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button = > (Button) findViewById(R.id.BT);
EditText textfield = (EditText) findViewById(R.id.ET);
TextView textview = (TextView) findViewById(R.id.TV);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (textfield.getText().toString().equalsIgnoreCase("Please type your name.."))
{
textfield.setText("");
}
else if (textfield.getText().toString().equals(""))
{
textfield.setText("");
}
else
{
String name = textfield.getText().toString();
textview.setText("Hello World!! I am " + name);
}
}
});
textfield.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus)
{
// TODO Auto-generated method stub
textfield.setText("");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是布局
<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:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/ET"
android:text="@string/textfield_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:inputType="textPersonName"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/BT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b_concat"
android:layout_centerHorizontal="true"
android:layout_below="@id/ET"
/>
<TextView
android:id="@+id/TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:text=""
android:layout_below="@id/BT"
/>
</RelativeLayout>
这是Android清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.helloworld.MainActivity"
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>
这是logcat
07-14 12:53:16.129: D/AndroidRuntime(615): Shutting down VM
07-14 12:53:16.249: W/dalvikvm(615): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-14 12:53:16.310: E/AndroidRuntime(615): FATAL EXCEPTION: main
07-14 12:53:16.310: E/AndroidRuntime(615): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.helloworld/com.example.helloworld.MainActivity}: java.lang.NullPointerException
07-14 12:53:16.310: E/AndroidRuntime(615): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
07-14 12:53:16.310: E/AndroidRuntime(615): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-14 12:53:16.310: E/AndroidRuntime(615): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-14 12:53:16.310: E/AndroidRuntime(615): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-14 12:53:16.310: E/AndroidRuntime(615): at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 12:53:16.310: E/AndroidRuntime(615): at android.os.Looper.loop(Looper.java:130)
07-14 12:53:16.310: E/AndroidRuntime(615): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-14 12:53:16.310: E/AndroidRuntime(615): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 12:53:16.310: E/AndroidRuntime(615): at java.lang.reflect.Method.invoke(Method.java:507)
07-14 12:53:16.310: E/AndroidRuntime(615): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-14 12:53:16.310: E/AndroidRuntime(615): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-14 12:53:16.310: E/AndroidRuntime(615): at dalvik.system.NativeStart.main(Native Method)
07-14 12:53:16.310: E/AndroidRuntime(615): Caused by: java.lang.NullPointerException
07-14 12:53:16.310: E/AndroidRuntime(615): at android.app.Activity.findViewById(Activity.java:1647)
07-14 12:53:16.310: E/AndroidRuntime(615): at com.example.helloworld.MainActivity.<init>(MainActivity.java:13)
07-14 12:53:16.310: E/AndroidRuntime(615): at java.lang.Class.newInstanceImpl(Native Method)
07-14 12:53:16.310: E/AndroidRuntime(615): at java.lang.Class.newInstance(Class.java:1409)
07-14 12:53:16.310: E/AndroidRuntime(615): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-14 12:53:16.310: E/AndroidRuntime(615): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
07-14 12:53:16.310: E/AndroidRuntime(615): ... 11 more