Java 在android ToggleButton中放置onclicklistener时获取nullpointerexception
当我尝试在android e中的toggleButton中的侦听器中放置祝酒词时,get null pointer异常有人能帮我吗 代码如下:Java 在android ToggleButton中放置onclicklistener时获取nullpointerexception,java,android,nullpointerexception,togglebutton,Java,Android,Nullpointerexception,Togglebutton,当我尝试在android e中的toggleButton中的侦听器中放置祝酒词时,get null pointer异常有人能帮我吗 代码如下: private ToggleButton locationToggle; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceStat
private ToggleButton locationToggle;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.settings);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.title_bar);
context = getApplicationContext();
sensorAdapter = new SensorAdapter();
ListView sensorListview = (ListView) findViewById(R.id.listViewSettings);
sensorListview.setAdapter(sensorAdapter);
locationToggle = (ToggleButton) findViewById(R.id.toggleButton1);
// attach an OnClickListener
locationToggle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "CARREGOU", Toast.LENGTH_LONG).show();
// your click actions go here
}
});
public class SensorAdapter extends BaseAdapter {
List<sensorItem> sensorList = getDataForListView();
private ToggleButton locationToggle, photosToggle, soundToggle,
orientationToggle, messagesToggle, batteryToggle, calendarToggle,
accelerometerToggle, lightToggle, contactsToggle;
@Override
public int getCount() {
return sensorList.size();
}
@Override
public sensorItem getItem(int arg0) {
return sensorList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
if (arg1 == null) {
LayoutInflater inflater = (LayoutInflater) Settings.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.listitem, arg2, false);
}
// TextView sensorName = (TextView)
// arg1.findViewById(R.id.textView1);
TextView sensorDescription = (TextView) arg1
.findViewById(R.id.textView2);
ImageView sensorImage = (ImageView) arg1
.findViewById(R.id.imageView1);
locationToggle = (ToggleButton) findViewById(R.id.toggleButton1);
photosToggle = (ToggleButton) findViewById(R.id.toggleButton1);
soundToggle = (ToggleButton) findViewById(R.id.toggleButton1);
orientationToggle = (ToggleButton) findViewById(R.id.toggleButton1);
messagesToggle = (ToggleButton) findViewById(R.id.toggleButton1);
batteryToggle = (ToggleButton) findViewById(R.id.toggleButton1);
calendarToggle = (ToggleButton) findViewById(R.id.toggleButton1);
accelerometerToggle = (ToggleButton) findViewById(R.id.toggleButton1);
lightToggle = (ToggleButton) findViewById(R.id.toggleButton1);
contactsToggle = (ToggleButton) findViewById(R.id.toggleButton1);
// attach an OnClickListener
locationToggle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "CARREGOU", Toast.LENGTH_LONG).show();
// your click actions go here
}
});
sensorItem sensor = sensorList.get(arg0);
// sensorName.setText(sensor.sensorName);
sensorDescription.setText(sensor.sensorDescription);
sensorImage.setBackground(sensor.sensorImage);
return arg1;
}
public sensorItem getCodeLearnChapter(int position) {
return sensorList.get(position);
}
}
这是错误日志:
04-03 16:57:29.511: E/AndroidRuntime(10552): FATAL EXCEPTION: main
04-03 16:57:29.511: E/AndroidRuntime(10552): java.lang.RuntimeException: Unable to start activity ComponentInfo{main.inSituApp/main.inSituApp.Settings}: java.lang.NullPointerException
04-03 16:57:29.511: E/AndroidRuntime(10552): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-03 16:57:29.511: E/AndroidRuntime(10552): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-03 16:57:29.511: E/AndroidRuntime(10552): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-03 16:57:29.511: E/AndroidRuntime(10552): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-03 16:57:29.511: E/AndroidRuntime(10552): at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 16:57:29.511: E/AndroidRuntime(10552): at android.os.Looper.loop(Looper.java:137)
04-03 16:57:29.511: E/AndroidRuntime(10552): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-03 16:57:29.511: E/AndroidRuntime(10552): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 16:57:29.511: E/AndroidRuntime(10552): at java.lang.reflect.Method.invoke(Method.java:511)
04-03 16:57:29.511: E/AndroidRuntime(10552): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-03 16:57:29.511: E/AndroidRuntime(10552): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-03 16:57:29.511: E/AndroidRuntime(10552): at dalvik.system.NativeStart.main(Native Method)
04-03 16:57:29.511: E/AndroidRuntime(10552): Caused by: java.lang.NullPointerException
04-03 16:57:29.511: E/AndroidRuntime(10552): at main.inSituApp.Settings.onCreate(Settings.java:64)
04-03 16:57:29.511: E/AndroidRuntime(10552): at android.app.Activity.performCreate(Activity.java:5008)
04-03 16:57:29.511: E/AndroidRuntime(10552): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-03 16:57:29.511: E/AndroidRuntime(10552): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-03 16:57:29.511: E/AndroidRuntime(10552): ... 11 more
我想不出这是怎么回事。始终为空指针异常
切换按钮位于listitem.xml文件中
编辑:
我更改了代码以初始化适配器内的切换按钮。代码如下:
private ToggleButton locationToggle;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.settings);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.title_bar);
context = getApplicationContext();
sensorAdapter = new SensorAdapter();
ListView sensorListview = (ListView) findViewById(R.id.listViewSettings);
sensorListview.setAdapter(sensorAdapter);
locationToggle = (ToggleButton) findViewById(R.id.toggleButton1);
// attach an OnClickListener
locationToggle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "CARREGOU", Toast.LENGTH_LONG).show();
// your click actions go here
}
});
public class SensorAdapter extends BaseAdapter {
List<sensorItem> sensorList = getDataForListView();
private ToggleButton locationToggle, photosToggle, soundToggle,
orientationToggle, messagesToggle, batteryToggle, calendarToggle,
accelerometerToggle, lightToggle, contactsToggle;
@Override
public int getCount() {
return sensorList.size();
}
@Override
public sensorItem getItem(int arg0) {
return sensorList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
if (arg1 == null) {
LayoutInflater inflater = (LayoutInflater) Settings.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.listitem, arg2, false);
}
// TextView sensorName = (TextView)
// arg1.findViewById(R.id.textView1);
TextView sensorDescription = (TextView) arg1
.findViewById(R.id.textView2);
ImageView sensorImage = (ImageView) arg1
.findViewById(R.id.imageView1);
locationToggle = (ToggleButton) findViewById(R.id.toggleButton1);
photosToggle = (ToggleButton) findViewById(R.id.toggleButton1);
soundToggle = (ToggleButton) findViewById(R.id.toggleButton1);
orientationToggle = (ToggleButton) findViewById(R.id.toggleButton1);
messagesToggle = (ToggleButton) findViewById(R.id.toggleButton1);
batteryToggle = (ToggleButton) findViewById(R.id.toggleButton1);
calendarToggle = (ToggleButton) findViewById(R.id.toggleButton1);
accelerometerToggle = (ToggleButton) findViewById(R.id.toggleButton1);
lightToggle = (ToggleButton) findViewById(R.id.toggleButton1);
contactsToggle = (ToggleButton) findViewById(R.id.toggleButton1);
// attach an OnClickListener
locationToggle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "CARREGOU", Toast.LENGTH_LONG).show();
// your click actions go here
}
});
sensorItem sensor = sensorList.get(arg0);
// sensorName.setText(sensor.sensorName);
sensorDescription.setText(sensor.sensorDescription);
sensorImage.setBackground(sensor.sensorImage);
return arg1;
}
public sensorItem getCodeLearnChapter(int position) {
return sensorList.get(position);
}
}
你有
setContentView(R.layout.settings);
你说
切换按钮位于列表项.xml文件中
那么这个
locationToggle = (ToggleButton) findViewById(R.id.toggleButton1);
失败导致
NullPointerException
ToogleButton
不属于settings.xml
,您可以在当前视图层次结构中找到视图。但listitem是设置中listview的一部分。我可以通过R.id.toggleButton1找到它。这没有错@JoaoFilipeClementeMartins为listview和为activity展开的布局不同,您可以在当前视图层次结构下找到视图。因此,如果settings.xml
没有切换按钮,您会得到任何关于如何在settings.xml中工作的建议?因为这就是我需要的。也许我只是做得不对。@Joaofilpeclementematins你所做的是不可能的。在适配器本身中初始化并写入侦听器there@JoaoFilipeClementeMartins阅读