Приложение состоит пока из 2 экранов:
1) MainActivity: с 3 кнопками для разной функциональности
public class MainActivity extends AppCompatActivity {
Button btn_getRandomCocktail, btn_searchByName, btn_searchByIngredient;
EditText et_datainput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// assign values to the each control on the layout
btn_getRandomCocktail = findViewById(R.id.btn_getRandomCocktail);
btn_searchByName = findViewById(R.id.btn_searchByName);
btn_searchByIngredient = findViewById(R.id.btn_searchByIngredient);
et_datainput = findViewById(R.id.et_dataInput);
// click listeners to the each button
btn_getRandomCocktail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, randomActivity.class);
startActivity(intent);
2) RandomActivity: открывается по нажатию на первую кнопку и должно выдавать случайной коктейль через Api
public class randomActivity extends Activity{
Button btn_new;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btn_new = findViewById(R.id.btn_new);
setContentView(R.layout.activity_random);
final randomCocktailService randomCocktailService = new randomCocktailService(randomActivity.this);
btn_new.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
randomCocktailService.getRandomCocktail(new randomCocktailService.RandomCocktailListener() {
@Override
public void onResponse(CocktailModel cocktailModel) {
Toast.makeText(randomActivity.this, cocktailModel.getStrDrink(), Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String message) {
Toast.makeText(randomActivity.this, "Something wrong", Toast.LENGTH_SHORT).show();
}
});
}
});
При нажатии на кнопку, которая связана со вторым активити вылетает такая ошибка:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cocktail/com.example.cocktail.randomActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3479)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3651)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2104)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:600)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.cocktail.randomActivity.onCreate(randomActivity.java:22)
at android.app.Activity.performCreate(Activity.java:8109)
at android.app.Activity.performCreate(Activity.java:8083)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3452)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3651)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2104)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:600)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
Как я понял, кнопка не связывается по id, а почему так я не нагуглил.