-1

我开始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

建议?

4

4 回答 4

0

在类的第 14 行有一个 NullPointerException SecondActivity

请给我们这个类的源代码。

于 2012-08-01T09:55:06.180 回答
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");
}
于 2012-08-01T09:56:00.857 回答
0

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

公共类 SecondActivity 扩展 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"));
}

}

于 2012-08-01T10:00:09.963 回答
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);

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

像这样使用它。-->

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);
于 2012-08-01T10:07:15.380 回答