首页 文章

添加时App崩溃

提问于
浏览
-1

我有以下问题:我有一个活动,其中我想添加一个按钮,所以我可以在点击时向我的树莓派发送信息 .

现在,只要我包含Button,程序就会在尝试进入Activity时崩溃 . 清理项目没有帮助 .

在我为Button包含所有内容之前,它工作正常 .

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>

编辑:

Logcat在崩溃时说:

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

03-16 17:00:04.884 2942-2942 /? E / AndroidRuntime:FATAL EXCEPTION:main进程:com.example.paddy.cocktailapp20,PID:2942 java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.paddy.cocktailapp20 / com.example.paddy.cocktailapp20.CocktailActivity }:java.lang.NullPointerException:尝试在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)上的空对象引用上调用虚拟方法'android.view.View android.view.Window.findViewById(int)'在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)在android.app.ActivityThread.access $ 800(ActivityThread.java:151)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)在android位于android.app.Looper.loop(Looper.java:135)的.os.Handler.dispatchMessage(Handler.java:102)位于java.lang.reflect的android.app.ActivityThread.main(ActivityThread.java:5254) . 在com.android.internal.os.ZygoteInit $ MethodAnd的java.lang.reflect.Method.invoke(Method.java:372)上的Method.invoke(Native Method) ArgsCaller.run(ZygoteInit.java:903)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)引起:java.lang.NullPointerException:尝试调用虚方法'android.view.View android .view.Window.findViewById(int)'在android.app.Activity.findViewById(Activity.java:2072)上的空对象引用,位于java的com.example.paddy.cocktailapp20.CocktailActivity . (CocktailActivity.java:22)位于android.app.ActivityThread.performLaunchActivity的android.app.Instrumentation.newActivity(Instrumentation.java:1066)java.lang.Class.newInstance(Class.java:1606)的.lang.reflect.Constructor.newInstance(Native Method) (ActivityThread.java:2226)在Android.app.ActivityThread.access $ 800(ActivityThread.java:151)的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)android.app.ActivityThread $ H.handleMessage(ActivityThread) .java:1303)在android.app.A.Tharead.mai的android.os.Handler.dispatchMessage(Handler.java:102)android.os.Looper.loop(Looper.java:135) n(ActivityThread.java:5254)位于java.lang.reflect.Method.invoke(Native Method)的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:强制完成活动1 com.example.paddy.cocktailapp20 / .CocktailActivity 03-16 17:00:04.887 1487-1922 / system_process W / ActivityManager:强制完成活动2 com.example.paddy.cocktailapp20 / .MainActivity

1 回答

  • 0

    在've set the activity'内容视图后,您需要 make = (Button) findViewById(R.id.make); .

    所以你应该做一些像:

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

相关问题