【问题标题】:I don't receive the String I passed with an intent on my second activity我没有收到我传递的第二个活动意图的字符串
【发布时间】:2018-09-28 12:34:03
【问题描述】:

有我的在我需要快点的时候花点时间的问题..

我只是打算将信息从我的主要活动转移到其他活动。 但数据不会转到其他活动。

请稍等片刻。

MainActivity.class

public class MainActivity extends AppCompatActivity {

public static final String KEY = "KEY";
Button button;

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

    button = findViewById(R.id.button);

    button.setOnClickListener(v -> {

        Intent intent = new Intent(MainActivity.this, OtherActivity.class);

        intent.putExtra(KEY, "FONCTIONNE");

        startActivity(intent);

        });
    }
}

OtherActivity.class

import static com.example.marguerite.experiences.MainActivity.KEY;

public class OtherActivity extends AppCompatActivity {

Button button;
TextView textView;
String text;

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

        button = findViewById(R.id.button);
        textView = findViewById(R.id.textview);

        text = getIntent().getStringExtra(KEY);

        textView.setText(text);

        button.setOnClickListener(v ->finish());
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.marguerite.experiences">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:fullBackupContent="@xml/backup_descriptor">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".OtherActivity" />
</application>

</manifest>

错误事件

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.marguerite.experiences, PID: 8496
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.marguerite.experiences/com.example.marguerite.experiences.OtherActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                  at com.example.marguerite.experiences.OtherActivity.onCreate(OtherActivity.java:26)
                  at android.app.Activity.performCreate(Activity.java:7136)

就是这样。我当然忘记了一些东西,但我看过的所有教程都使用相同的简单技术。

【问题讨论】:

  • 请发帖activity_other.xml。看起来TextView 在那里不存在。
  • 将 textview id 更改为实际的。我认为复制粘贴错误

标签: java android android-intent error-handling


【解决方案1】:

下面一行...

textView.setText(text);

...引发以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

这意味着textView 为空。 activity_other.xml 中没有 ID 为 textview 的视图。确保视图存在并排除任何拼写错误。

另见:What is a NullPointerException, and how do I fix it?

【讨论】:

  • 是的,谢谢你的帮助。我需要这个来确保我自己这是一个好的技术,我在另一个应用程序上工作并使用相同的但它不起作用。一旦代码被更正,我就复制/过去了这个,它工作了,所以谢谢你!对方的问题不是缺少身份证,我永远不会知道...
【解决方案2】:

以上所有答案都应该有效,但如果仍然无效,请尝试更改

 button = findViewById(R.id.button);
    textView = findViewById(R.id.textview);  

 button = (Button)findViewById(R.id.button);
    textView = (TextView)findViewById(R.id.textview);  

在这两个活动中

当你在 OtherActivity 中得到意图时,像这样调用它

text = getIntent.getStringExtra("KEY");

【讨论】:

    猜你喜欢
    • 2014-12-28
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多