gpt4 book ai didi

java - 似乎无法解决此 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 03:27:02 24 4
gpt4 key购买 nike

我得到的错误是:

java.lang.NullPointerException at com.me.app.MainActivity.onCreate(MainActivity.java:34)

MainActivity.java中第34-39行如下:

light_switch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
turn_torch_on();
}
});

这让我相信 light_switch 正在返回 null,尽管在​​第 31 行:

light_switch = (ImageButton)findViewById(R.id.light_switch);

我仔细检查了 ID 是否正确。有什么我想念的吗?我不太确定目前我能做些什么来解决这个问题。

异常日志可在此处获得:http://pastebin.com/raw.php?i=yDPRjvry

主 Activity .java

package com.me.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

public class MainActivity extends Activity {

ImageButton light_switch;

private boolean has_flash;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}

// get the light switch
light_switch = (ImageButton)findViewById(R.id.light_switch);

// make the light switch work
light_switch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
turn_torch_on();
}
});

// check if device has flash capability
has_flash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

// alert user that their device is not supported
if ( !has_flash ) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Sorry :(");
alert.setMessage("Your device does not have a flash");
alert.show();
return;
}

}

/**
* turn torch on
*/
private void turn_torch_on() {
// change the layout to off
toggle_layout();
}

/**
* toggle layout for on/off
*/
private void toggle_layout() {
light_switch.setBackgroundResource(R.drawable.button_on);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}

}

fragment _main.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/bg_off"
tools:context=".MainActivity$PlaceholderFragment">

<ImageButton
android:id="@+id/light_switch"
android:layout_height="100dp"
android:layout_width="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/button_off"
android:contentDescription="On/Off switch" />

</RelativeLayout>

最佳答案

您正在 PlaceholderFragment 的 onCreateView 上膨胀 fragment_main.xml(其中包含您的 ImageButton)。试试这个:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// get the light switch
light_switch = (ImageButton)rootView.findViewById(R.id.light_switch);

// make the light switch work
light_switch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
turn_torch_on();
}
});
return rootView;
}

关于java - 似乎无法解决此 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20509355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com