У меня есть три счетчика, так что мне нужно перейти к соответствующей активности в зависимости от входных данных в счетчиках. При запуске приложения происходит сбой приложения. Я понятия не имею об ошибке. Любая помощь будет оценена.
Я дважды проверил свой код и не смог найти никаких ошибок. Приложение аварийно завершает работу, когда я пытаюсь запустить его на своем устройстве.
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Button btn;
Spinner spin1;
Spinner spin2;
Spinner spin3;
String[] years = {"1", "2", "3", "4"};
String[] branches = {"CSE", "ECE", "EEE", "CIVIL", "IT", "MECH"};
String[] sections = {"A", "B", "C"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin1 = (Spinner) findViewById(R.id.spinner1);
spin1.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, years);
//Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//attaching dataAdapter to spinner
spin1.setAdapter(dataAdapter);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
spin2.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, branches);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(dataAdapter2);
Spinner spin3 = (Spinner) findViewById(R.id.spinner3);
spin3.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, sections);
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin3.setAdapter(dataAdapter3);
Button btn = (Button) findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displaycheck();
}
});
}
public void displaycheck() {
String sp1 = spin1.getSelectedItem().toString();
String sp2 = spin2.getSelectedItem().toString();
String sp3 = spin3.getSelectedItem().toString();
if (sp1.equals("3") && sp2.equals("CSE") && sp3.equals("B")) {
Intent it = new Intent(this, M2.class);
startActivity(it);
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//on selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}``````
i have expected succesfull opening of other activity, but it`s crashing
**manifest.xml** `<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.meratimetable">
<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">
<activity android:name=".M2"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest> `
**stack trace**
`Process: com.example.meratimetable, PID: 2470
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.widget.Spinner.getSelectedItem()' on a null object reference
at com.example.meratimetable.MainActivity.displaycheck(MainActivity.java:59)
at com.example.meratimetable.MainActivity$1.onClick(MainActivity.java:53)
at android.view.View.performClick(View.java:7339)
at android.widget.TextView.performClick(TextView.java:14226)
at android.view.View.performClickInternal(View.java:7305)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27787)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7081)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
2019-10-18 22:33:14.517 4053-4841/? E/rs$DefaultThreadFactory: ===== Detect pool-thread leak =====
2019-10-18 22:33:14.517 4053-4841/? E/rs$DefaultThreadFactory: dalvik.system.VMStack.getThreadStackTrace(Native Method)`