-1

I have following issue: I got an Activity in which I wanted to add a Button so I can send Information to my raspberry Pi when clicked.

Now as soon as I included the Button the program crashes when it tries to enter the Activity. Clearing the Project didn't help.

Before I included everything for the Button it worked fine.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class CocktailActivity extends Activity {

    Button make = (Button) findViewById(R.id.make);
    TextView cocktail_name;
    TextView cocktail_zutaten;
    ImageView cocktail_image_big;
    Intent intent;


    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.cocktail_layout);


        cocktail_name = (TextView) findViewById(R.id.cocktail_name_detail);
        cocktail_zutaten = (TextView) findViewById(R.id.cocktail_zutaten_detail);
        cocktail_image_big = (ImageView) findViewById(R.id.cocktail_image_detail);

        intent = getIntent();
        Cocktail cocktail = AppData.cocktailList.get(intent.getIntExtra("cocktailId", 0));

        cocktail_name.setText(cocktail.getName());
        cocktail_zutaten.setText(cocktail.getZubereitung());
        cocktail_image_big.setImageDrawable(getResources().getDrawable(R.drawable.ic_cocktail_big_platzhalter));


        make.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                new Background_get().execute("led1=1");


            }
        });
    }

    private class Background_get extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            try {

                URL url = new URL("http://192.168.178.34/?" + params[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuilder result = new StringBuilder();
                String inputLine;
                while ((inputLine = in.readLine()) != null)
                    result.append(inputLine).append("\n");

                in.close();
                connection.disconnect();
                return result.toString();

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

    }

}


<?xml version="1.0" encoding="UTF-8"?>

    <LinearLayout
    android:paddingRight="20dp"
    android:paddingLeft="20dp"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:textSize="22sp"
        android:id="@+id/cocktail_name_detail"/>


        <TextView
            android:id="@+id/cocktail_zutaten_detail"
            android:layout_width="match_parent"
            android:layout_height="185dp"
            android:gravity="center" />

    <Button
        android:id="@+id/make"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Make" />

    <ImageView
        android:id="@+id/cocktail_image_detail"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" />



</LinearLayout>

Edit:

The Logcat says at the crash:

                                             --------- beginning of crash

03-16 17:00:04.884 2942-2942/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.paddy.cocktailapp20, PID: 2942 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.paddy.cocktailapp20/com.example.paddy.cocktailapp20.CocktailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference at android.app.Activity.findViewById(Activity.java:2072) at com.example.paddy.cocktailapp20.CocktailActivity.(CocktailActivity.java:22) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.Class.newInstance(Class.java:1606) at android.app.Instrumentation.newActivity(Instrumentation.java:1066) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)  at android.app.ActivityThread.access$800(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5254)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)  03-16 17:00:04.886 1487-1922/system_process W/ActivityManager: Force finishing activity 1 com.example.paddy.cocktailapp20/.CocktailActivity 03-16 17:00:04.887 1487-1922/system_process W/ActivityManager: Force finishing activity 2 com.example.paddy.cocktailapp20/.MainActivity

Patrick
  • 125
  • 1
  • 7
  • 1
    What does the logcat tell you? – Ken White Mar 16 '18 at 16:30
  • 1
    Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Zoe Mar 16 '18 at 16:47
  • Please learn how to read properly stacktraces before start posting questions here.... – hardartcore Mar 16 '18 at 16:56

1 Answers1

0

You need to make = (Button) findViewById(R.id.make); after you've set the activity's content view.

So you should instead do something like:

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.cocktail_layout);
    make = (Button) findViewById(R.id.make);

    ...
}
takecare
  • 1,684
  • 3
  • 21
  • 32