我开始programmig Android App。但是我正在为此苦苦挣扎,尽管我在这个论坛上发现了类似的东西(这里-尚未检查-)。当我按下按钮时,我的应用程序崩溃了。这里的代码:
package mobileapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View button = (Button) findViewById(R.id.button_print_string);
button.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_print_string:
EditText cerca = (EditText) findViewById(R.id.cerca);
Bundle bundle = new Bundle();
bundle.putString("result", cerca.getText().toString());
Intent Intent2 = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(Intent2);
}
}
}
这是我的清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.stasbranger.clinigomobile"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity" >
<intent-filter ></intent-filter>
</activity>
</application>
</manifest>
和我的activity_main:
<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" >
<Button
android:id="@+id/button_print_string"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="@string/str_button" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/cerca"
android:inputType="textEmailAddress"
android:text="@string/cerca">
<requestFocus></requestFocus>
</EditText>
</RelativeLayout>
我得到:
08-01 09:40:59.335: E/AndroidRuntime(560): FATAL EXCEPTION: main
08-01 09:40:59.335: E/AndroidRuntime(560): java.lang.RuntimeException: Unable to start activity ComponentInfo{it.stasbranger.clinigomobile/it.stasbranger.clinigomobile.SecondActivity}: java.lang.NullPointerException
08-01 09:40:59.335: E/AndroidRuntime(560): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-01 09:40:59.335: E/AndroidRuntime(560): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-01 09:40:59.335: E/AndroidRuntime(560): at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-01 09:40:59.335: E/AndroidRuntime(560): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-01 09:40:59.335: E/AndroidRuntime(560): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 09:40:59.335: E/AndroidRuntime(560): at android.os.Looper.loop(Looper.java:137)
08-01 09:40:59.335: E/AndroidRuntime(560): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-01 09:40:59.335: E/AndroidRuntime(560): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 09:40:59.335: E/AndroidRuntime(560): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 09:40:59.335: E/AndroidRuntime(560): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-01 09:40:59.335: E/AndroidRuntime(560): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-01 09:40:59.335: E/AndroidRuntime(560): at dalvik.system.NativeStart.main(Native Method)
08-01 09:40:59.335: E/AndroidRuntime(560): Caused by: java.lang.NullPointerException
08-01 09:40:59.335: E/AndroidRuntime(560): at it.stasbranger.clinigomobile.SecondActivity.onCreate(SecondActivity.java:14)
08-01 09:40:59.335: E/AndroidRuntime(560): at android.app.Activity.performCreate(Activity.java:4465)
08-01 09:40:59.335: E/AndroidRuntime(560): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-01 09:40:59.335: E/AndroidRuntime(560): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-01 09:40:59.335: E/AndroidRuntime(560): ... 11 more
建议?