从onItemSelected()获取的字符串变量,用于Spinner无法被intent识别

问题描述 投票:0回答:2

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

  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)

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

谢谢

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>
android android-intent spinner
2个回答
0
投票

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

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

因为在activity_output.xml,没有TextView,其id为result

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

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

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"));
    }
© www.soinside.com 2019 - 2024. All rights reserved.