从 onItemSelected() 获取的 Spinner 的字符串变量未被意图识别
String variable taken from onItemSelected() for Spinner not recognized by intent
我有一个简单的微调器小部件,它有星期几(周一至周日)。
我希望微调器选择的日期为:
在 activity 中显示所选日期(我已经这样做了)
在单击按钮后使用 intents 在新的 activity 中显示选定的日期(我卡在这里了)。
我得到的错误如下
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() 按钮函数中被读取?
谢谢
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);
}
}
第二个activity:
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首activity:
<?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 第二个 activity 收到意图:
<?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>
第二行中的这一行 activity 让您的应用程序崩溃
TextView textView = (TextView) findViewById(R.id.result);
因为activity_output.xml
中没有id为result
的TextView
。
解决方案:将第二个activity中的代码更改为
TextView textView = (TextView) findViewById(R.id.textView3);
海在先activity变
public void sendThis(View view) {
Intent intent = new Intent(this, output.class);
intent.putExtra("message", send);
startActivity(intent);
}
在第二个 activity 你没有名称结果的 id 所以像这样改变
TextView textView = (TextView) findViewById(R.id.textView3);
if (getIntent().getStringExtra("message")!=null){
textView.setText(getIntent().getStringExtra("message"));
}
我有一个简单的微调器小部件,它有星期几(周一至周日)。 我希望微调器选择的日期为:
在 activity 中显示所选日期(我已经这样做了)
在单击按钮后使用 intents 在新的 activity 中显示选定的日期(我卡在这里了)。
我得到的错误如下
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() 按钮函数中被读取?
谢谢
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);
}
}
第二个activity:
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首activity:
<?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 第二个 activity 收到意图:
<?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>
第二行中的这一行 activity 让您的应用程序崩溃
TextView textView = (TextView) findViewById(R.id.result);
因为activity_output.xml
中没有id为result
的TextView
。
解决方案:将第二个activity中的代码更改为
TextView textView = (TextView) findViewById(R.id.textView3);
海在先activity变
public void sendThis(View view) {
Intent intent = new Intent(this, output.class);
intent.putExtra("message", send);
startActivity(intent);
}
在第二个 activity 你没有名称结果的 id 所以像这样改变
TextView textView = (TextView) findViewById(R.id.textView3);
if (getIntent().getStringExtra("message")!=null){
textView.setText(getIntent().getStringExtra("message"));
}