У меня есть простой виджет прядильщика, который имеет дни недели (понедельник-воскресенье).Я хочу, чтобы выбранный день на счетчике:
Отображение выбранного дня внутри упражнения (я уже это сделал)
Отображение выбранногодень внутри нового действия с использованием намерений после нажатия кнопки (я застрял здесь).
Я получаю следующую ошибку
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>