0

我是 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
4

2 回答 2

3

简单你必须使用这种方式

public class MainActivity extends Activity
 {
Button button ; 
EditText textfield; 
TextView textview ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     button =  (Button) findViewById(R.id.BT);
     textfield = (EditText) findViewById(R.id.ET);
     textview = (TextView) findViewById(R.id.TV);
     .......
}

编辑:您在创建活动之前初始化 ui 元素(未设置视图)并且必须记住在;onCreate(..)行之后的方法 中初始化 ui 元素setContentView(Your layout filename)

于 2013-07-14T05:16:57.903 回答
0

将您的初始化移到下方setContentView

Button button;
EditText textField;
TextView textview;   
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main); // set the content to the activity
 button =  (Button) findViewById(R.id.BT); //initialize after setContentView
 textfield = (EditText) findViewById(R.id.ET);
 textview = (TextView) findViewById(R.id.TV);
  ... // rest of the code
 } 

您可以findViewById将当前视图层次结构设置为活动。你必须在之后初始化setContentView。您已在 onCreate 外部初始化。初始化失败。因此NullPointerException

于 2013-07-14T05:14:28.020 回答