无法通过意图识别从Spinner的onItemSelected()中获取的字符串变量

时间:2019-03-06 01:57:11

标签: android android-intent spinner

我有一个简单的微调器小部件,其中有星期几(周一至周日)。 我希望从微调框选择的日期为:

  1. 显示活动中的选定日期(我已经这样做了)

  2. 单击按钮(我被困在这里)后,使用意图在新活动中显示选定的日期。

我得到的错误如下

03-05 20:47:03.840 4490-4490/com.abc.lab2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.abc.lab2, PID: 4490
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.lab2/com.abc.lab2.output}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)

为什么没有在有意图的sendThis()按钮函数内部读取我的String发送变量?

谢谢

package com.abc.lab2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    public Button button;
    public String send;
    public String result;
    public Intent intent;


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

        Spinner myspinner = findViewById(R.id.spinner);

        ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.list, android.R.layout.simple_spinner_item);
        myspinner.setAdapter(adapter);
        myspinner.setOnItemSelectedListener(this);

        myspinner.setSelection(0, false);
        myspinner.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

        if(adapterView.getId()==R.id.spinner){
            Log.d("LOG", "Item " + i + " was selected");
            String what = adapterView.getItemAtPosition(i).toString();
            send=what;
            Log.d("Log", "Choice is" + what );
            TextView result = (TextView) findViewById(R.id.result);
            result.setText(send);




        }

    }



    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    public void sendThis(View view) {
        Intent intent  = new Intent(this, output.class);
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra("message", send); // the ERROR is here... send is a string found in the onItemSelected().


        startActivity(intent);


    }
}

第二项活动:

package com.abc.lab2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class output extends AppCompatActivity {



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

        Intent intent = getIntent();
        TextView textView = (TextView) findViewById(R.id.result);
        String message = intent.getStringExtra("message");

        textView.setText(message);

    }
}

XML的第一个活动:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <include
        layout="@layout/toolbar"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LabTwo!"
        android:id="@+id/textView"
        android:layout_below="@+id/toolbar"/>

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="53dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="208dp"
        android:layout_marginEnd="379dp"
        android:layout_marginRight="379dp"
        android:text="Selection is:"
        android:textSize="44dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:id="@+id/result"
        android:layout_below="@+id/textView2"
        android:textSize="100dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="249dp"
        android:layout_height="142dp"
        android:layout_below="@+id/text"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="72dp"
        android:layout_marginLeft="72dp"
        android:layout_marginTop="31dp"
        android:layout_marginEnd="74dp"
        android:layout_marginRight="74dp"
        android:layout_marginBottom="3dp"
        android:onClick="sendThis"
        android:text="@string/button" />


</RelativeLayout>

获得意图的XML第二项活动:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".output">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="55dp"
        tools:layout_editor_absoluteX="52dp"
        tools:layout_editor_absoluteY="356dp" />
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

第二个活动中的这一行使您的应用程序崩溃

TextView textView = (TextView) findViewById(R.id.result);

因为在activity_output.xml中,没有TextView的ID为result

解决方案::将第二个活动中的代码更改为

TextView textView = (TextView) findViewById(R.id.textView3);

答案 1 :(得分:0)

第一次活动改变中的快乐

 public void sendThis(View view) {
    Intent intent  = new Intent(this, output.class);
    intent.putExtra("message", send);
    startActivity(intent);
}

在第二次活动中,您没有名称结果的ID,因此请更改

TextView textView = (TextView) findViewById(R.id.textView3);
if (getIntent().getStringExtra("message")!=null){
        textView.setText(getIntent().getStringExtra("message"));
    }