我想知道是否有人可以帮助我。 我有一个带有9个按钮的活动,当点击它们改变另一个按钮的颜色时,9个按钮中的每个按钮都会使用以下颜色将按钮的颜色改为不同的颜色:
currentcolour.setBackgroundColor(Color.rgb(0, 255, 0));
用于自定义颜色,或者:
currentcolour.setBackgroundColor(Color.RED);
表示默认颜色
当我在平板电脑上运行应用程序并单击该按钮时,它会抛出空指针异常:
07-09 10:26:21.492: D/AndroidRuntime(8157): Shutting down VM
07-09 10:26:21.492: W/dalvikvm(8157): threadid=1: thread exiting with uncaught exception (group=0x41986930)
07-09 10:26:21.507: E/AndroidRuntime(8157): FATAL EXCEPTION: main
07-09 10:26:21.507: E/AndroidRuntime(8157): java.lang.NullPointerException
07-09 10:26:21.507: E/AndroidRuntime(8157): at sjtech.rompa.wifi.Wifi$1.onClick(Wifi.java:54)
07-09 10:26:21.507: E/AndroidRuntime(8157): at android.view.View.performClick(View.java:4452)
07-09 10:26:21.507: E/AndroidRuntime(8157): at android.widget.Button.performClick (Button.java:148)
07-09 10:26:21.507: E/AndroidRuntime(8157): at android.view.View$PerformClick.run (View.java:18428)
07-09 10:26:21.507: E/AndroidRuntime(8157): at android.os.Handler.handleCallback(Handler.java:725)
07-09 10:26:21.507: E/AndroidRuntime(8157): at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 10:26:21.507: E/AndroidRuntime(8157): at android.os.Looper.loop(Looper.java:176)
07-09 10:26:21.507: E/AndroidRuntime(8157): at android.app.ActivityThread.main(ActivityThread.java:5365)
07-09 10:26:21.507: E/AndroidRuntime(8157): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 10:26:21.507: E/AndroidRuntime(8157): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 10:26:21.507: E/AndroidRuntime(8157): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-09 10:26:21.507: E/AndroidRuntime(8157): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-09 10:26:21.507: E/AndroidRuntime(8157): at dalvik.system.NativeStart.main(Native Method)
07-09 10:26:21.539: D/dalvikvm(8157): GC_CONCURRENT freed 668K, 31% free 8844K/12804K, paused 1ms+4ms, total 33ms
这是.java
package sjtech.rompa.wifi;
import com.javacodegeeks.android.bluetoothtest.R;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.util.Log;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
//called when the activity is first created
public class Wifi extends Activity {
private Button currentcolour;
private Button redbtn;
private Button grnbtn;
private Button bluebtn;
private Button yellowbtn;
private Button orangebtn;
private Button purplebtn;
private Button pinkbtn;
private Button whitebtn;
private Button resetbtn;
private Button blueleftarrow;
//Button wifi_screen changes between screens.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wifi);
//****RED BUTTON****
// Get a reference to the button
redbtn = (Button) findViewById(R.id.redbtn);
// Set the click listener to run the code.
redbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// red button's click event
Toast.makeText(getBaseContext(), "RED " , Toast.LENGTH_SHORT ).show();
currentcolour.setBackgroundColor(Color.RED);
}
});
//****GREEN BUTTON****
// Get a reference to the button
grnbtn = (Button)findViewById(R.id.grnbtn);
// Set the click listener to run the code.
grnbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// green button's click event
Toast.makeText(getBaseContext(), "GREEN " , Toast.LENGTH_SHORT ).show();
currentcolour.setBackgroundColor(Color.rgb(0, 255, 0));
}
});
//****BLUE BUTTON****
// Get a reference to the button
bluebtn = (Button)findViewById(R.id.bluebtn);
// Set the click listener to run the code.
bluebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// blue button's click event
Toast.makeText(getBaseContext(), "BLUE " , Toast.LENGTH_SHORT ).show();
currentcolour.setBackgroundColor(Color.BLUE);
}
});
//****YELLOW BUTTON****
// Get a reference to the button
yellowbtn = (Button)findViewById(R.id.yellowbtn);
// Set the click listener to run the code.
yellowbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// yellow button's click event
Toast.makeText(getBaseContext(), "YELLOW" , Toast.LENGTH_SHORT ).show();
currentcolour.setBackgroundColor(Color.YELLOW);
}
});
//****ORANGE BUTTON****
// Get a reference to the button
orangebtn = (Button)findViewById(R.id.orangebtn);
// Set the click listener to run the code.
orangebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// orange button's click event
Toast.makeText(getBaseContext(), "ORANGE" , Toast.LENGTH_SHORT ).show();
currentcolour.setBackgroundColor(Color.rgb(255, 100, 0));
}
});
//****PURPLE BUTTON****
// Get a reference to the button
purplebtn = (Button)findViewById(R.id.purplebtn);
// Set the click listener to run the code.
purplebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// purple button's click event
Toast.makeText(getBaseContext(), "PURPLE" , Toast.LENGTH_SHORT ).show();
currentcolour.setBackgroundColor(Color.rgb(128, 0, 128));
}
});
//****PINK BUTTON****
// Get a reference to the button
pinkbtn = (Button)findViewById(R.id.pinkbtn);
// Set the click listener to run the code.
pinkbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// pink button's click event
Toast.makeText(getBaseContext(), "PINK" , Toast.LENGTH_SHORT ).show();
currentcolour.setBackgroundColor(Color.rgb(255, 20, 147));
}
});
//****WHITE BUTTON****
// Get a reference to the button
whitebtn = (Button)findViewById(R.id.whitebtn);
//Set the click listener to run the code.
whitebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//white button's click event
Toast.makeText(getBaseContext(), "WHITE" , Toast.LENGTH_SHORT ).show();
currentcolour.setBackgroundColor(Color.WHITE);
}
});
//****GRAY BUTTON****
// Get a reference to the button
resetbtn = (Button)findViewById(R.id.resetbtn);
// Set the click listener to run the code.
resetbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// gray button's click event
Toast.makeText(getBaseContext(), "RESET" , Toast.LENGTH_SHORT ).show();
currentcolour.setBackgroundColor(Color.TRANSPARENT);
}
});
//****blueleftarrow BUTTON****
//changes from wifi to bluetooth page.
// Get a reference to the button
blueleftarrow = (Button)findViewById(R.id.blueleftarrow);
// Set the click listener to run the code.
blueleftarrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//blueleftarrow's click event
Intent intent = new Intent(v.getContext(), Bluetoothsetup.class);
startActivity(intent);
}
});
}
}
和.xml
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/clouds"
android:focusable="false"
android:focusableInTouchMode="false"
android:layoutMode="clipBounds"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:visibility="visible"
tools:context=".Wifi" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="193dp"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="0dp"
android:src="@drawable/rompalogo" />
<Button
android:id="@+id/orangebtn"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_x="223dp"
android:layout_y="750dp"
android:background="@drawable/orangecircle2" />
<Button
android:id="@+id/whitebtn"
style="@android:style/Animation"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_x="63dp"
android:layout_y="673dp"
android:background="@drawable/whitecircle2" />
<Button
android:id="@+id/pinkbtn"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_x="395dp"
android:layout_y="685dp"
android:background="@drawable/pinkcircle2" />
<Button
android:id="@+id/resetbtn"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_x="223dp"
android:layout_y="497dp"
android:background="@drawable/greycircle2" />
<Button
android:id="@+id/purplebtn"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_x="432dp"
android:layout_y="493dp"
android:background="@drawable/purplecircle2" />
<Button
android:id="@+id/yellowbtn"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_x="25dp"
android:layout_y="499dp"
android:background="@drawable/yellowcircle2" />
<Button
android:id="@+id/bluebtn"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_x="387dp"
android:layout_y="332dp"
android:background="@drawable/bluecircle2" />
<Button
android:id="@+id/redbtn"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_x="223dp"
android:layout_y="259dp"
android:background="@drawable/redcircle2" />
<Button
android:id="@+id/grnbtn"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_x="62dp"
android:layout_y="340dp"
android:background="@drawable/greencircle2" />
<Button
android:id="@+id/blueleftarrow"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_x="6dp"
android:layout_y="53dp"
android:background="@drawable/blueleftarrow" />
<Button
android:id="@+id/currentcolour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="438dp"
android:layout_y="66dp"
android:text="" />
</AbsoluteLayout>
提前致谢!
答案 0 :(得分:4)
您尚未初始化Button currentcolour;
并且您在点击监听器中使用它作为(即) currentcolour.setBackgroundColor(Color.RED);
导致NullPointerException
因为currentcolour
1}}是null
。
currentcolour= (Button) findViewById(R.id.currentcolour);//Initialize first
答案 1 :(得分:0)
在您的代码中,您使用了currentcolor
,它尚未初始化并且您正在尝试使用它。这就是你得到空指针异常的原因。
答案 2 :(得分:0)
这样做
//Initialize
currentcolour= (Button) findViewById(R.id.currentcolour);
//Set Colour
currentcolour.setBackgroundColor(Color.RED);
答案 3 :(得分:0)
currentcolour.setBackgroundColor(Color.RED);
没有初始化currentcolor
button
对象首先初始化它使用它。
如果您使用curentcolor而不启动它,您将获得NullPointerException