Android应用程序使用setOnClickListener崩溃

时间:2012-08-01 09:53:13

标签: android android-widget

我启动了programmig Android App。但是我正在努力解决这个问题,尽管我在这个论坛上找到了类似的东西(here -yet checked-)。当我按下按钮时,我的应用程序崩溃了。 这里是代码:

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_file:

<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

建议?

4 个答案:

答案 0 :(得分:0)

SecondActivity类的第14行有一个NullPointerException。

请提供此课程的源代码。

答案 1 :(得分:0)

试试这个......

Intent Intent2 = new Intent(MainActivity.this, SecondActivity.class);
                            ^^^^^^^^^^^^^^^^^
startActivity(Intent2);

在你的第二次活动中检查这个

if(getIntent().getExtras().getString("result") != null)
{
   textView.setText(getIntent().getExtras().getString("result"))
}
else
{
   textView.setText("null");
}

答案 2 :(得分:0)

您收到NullPointerException,因为您没有使用 setContentView 方法。

public class SecondActivity extends Activity {     TextView text_result; ??声明

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.second_activity_xml);  //  CREATE XML ADD  THIS

    Bundle bundle = this.getIntent().getExtras();

    text_result = (TextView) findViewViewById(R.id.text_resultID);  //  INITIALIZE
    text_result.setText(bundle.getString("result"));
}

}

答案 3 :(得分:0)

在您的代码中使用

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);

您正在使用Button创建View和类型转换对象,而不是使用对象按钮定义Button。

像这样使用它.--&gt;

public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Button button = (Button) findViewById(R.id.button_print_string);
          button.setOnClickListener(this);