我有以下问题:我有一个活动,其中我想添加一个Button,以便我可以在点击时向我的树莓派发送信息。
现在,只要我包含Button,程序在尝试进入活动时就会崩溃。清理项目没有帮助。
在我为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:致命异常:主要 处理:com.example.paddy.cocktailapp20,PID:2942 java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.paddy.cocktailapp20 / com.example.paddy.cocktailapp20.CocktailActivity}:java.lang.NullPointerException:尝试调用虚拟方法'android.view.View android。在null对象引用上查看.Window.findViewById(int)' 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 在android.app.ActivityThread.access $ 800(ActivityThread.java:151) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 在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) 在com.example.paddy.cocktailapp20.CocktailActivity。(CocktailActivity.java:22) at java.lang.reflect.Constructor.newInstance(Native Method) 在java.lang.Class.newInstance(Class.java:1606) 在android.app.Instrumentation.newActivity(Instrumentation.java:1066) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 在android.app.ActivityThread.access $ 800(ActivityThread.java:151) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 在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
答案 0 :(得分:0)
在您设置了活动的内容视图后,您需要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);
...
}