我正在制作一个应用程序,每次用户执行特定操作时都会创建一个 ImageButton。所以我的按钮是动态创建的。
我在 FRAGMENT 类中有一个方法 (createButton),它传递图像(用作 ImageButton 的背景)。
我的问题是,我似乎需要使用“LayoutInflater inflate”和“ViewGroup container”来打开我的相对布局 View 并向其添加动态按钮,但我还通过此方法传递图像,所以我的方法迫使我初始化 inflate 和容器。
我无法将它们初始化为 null,因为当我运行应用程序时,我会收到 NullPointerException。我还尝试从 onCreateView 传递它们(如下面的代码),但我仍然收到 NullPointerException。 如何使用 LayoutInflater inflate 和 ViewGroup 容器(不将它们初始化为 null),同时仍将其他变量传递给我的方法。在打印“tag-name: rLayout”之前我抛出异常。
public class HomeFragment extends Fragment {
ViewGroup rootContainer;
LayoutInflater rootInflater;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment, container, false);
rootContainer = container;
rootInflater = inflater;
return view;
}
// Create button from icon and image name
public void createButton (BitmapDrawable bitmapdrawable, String applicationName){
Log.d("tag_name", "createButton");
//LayoutInflater inflater = (LayoutInflater)getContext().getSystemService
//(Context.LAYOUT_INFLATER_SERVICE);
try {
Log.d("tag_name", "Try_2");
// Get Layout home_fragment
View rootView = rootInflater.inflate(R.layout.home_fragment, rootContainer, false);
RelativeLayout rLayout = (RelativeLayout) rootView.findViewById(R.id.home_fragment);
Log.d("tag_name", "rLayout");
// create image button with background as bitmapdrawable
ImageButton btn = new ImageButton(getActivity());
btn.setImageDrawable(bitmapdrawable);
int id = 1;
btn.setId(id);
Log.d("tag_name", "set the ID of the btn");
// create textview with applicationName
//TextView appName = new TextView(getActivity());
//appName.setText(applicationName);
// place new button next to existing button
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, id);
btn.setLayoutParams(params);
// place text under newly created button
// Put button and text in layout
rLayout.addView(btn);
//rLayout.addView(appName);
}
catch (Exception e){
System.out.println("Exception is "+e.toString());
}
}
}
这是我的堆栈跟踪:
05-03 14:33:42.468 7197-7223/it.anddev.bradipao.janus D/tag_name: createButton
05-03 14:33:42.468 7197-7223/it.anddev.bradipao.janus D/tag_name: Try_2
05-03 14:33:42.468 7197-7223/it.anddev.bradipao.janus I/System.out: Exception is java.lang.NullPointerException
05-03 14:33:42.468 7197-7223/it.anddev.bradipao.janus W/System.err: java.lang.NullPointerException
05-03 14:33:42.468 7197-7223/it.anddev.bradipao.janus W/System.err: at it.anddev.bradipao.janus.HomeFragment.createButton(HomeFragment.java:118)
05-03 14:33:42.468 7197-7223/it.anddev.bradipao.janus W/System.err: at it.anddev.bradipao.janus.MainActivity$1.run(MainActivity.java:155)
HomeFragment 中的第 118 行是这一行:
View rootView = rootInflater.inflate(R.layout.home_fragment, rootContainer, false);
mainActivity 中的第 155 行是第二行,我将变量传递给 HomeFragment:
HomeFragment createbutton = new HomeFragment();
createbutton.createButton(bitmapdrawable, applicationName);
最佳答案
问题在于创建 fragment 并调用 createButton 方法的方式
HomeFragment createbutton = new HomeFragment();
createbutton.createButton(bitmapdrawable, applicationName);
你不能这样做。 Android 中的所有 Activity 、 fragment 和服务都必须经历各自的生命周期,以便它们附加有效的上下文。
通过将它们视为普通的 Java 类,您最终会得到一个空上下文。由于 Activity、 fragment 或服务中的大多数方法都是在其上下文上调用的,因此您将收到空指针异常,这就是您的应用程序崩溃的原因。
你需要什么!!
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
Fragment profileFragment = new HomeFragment();//the fragment you want to show
fragmentTransaction
.add(R.id.parent, profileFragment);//R.id.content_frame is the layout you want to replace
fragmentTransaction.commit();
这就是创建 fragment 的方式。
为了调用其 createButton 方法,您需要一个接口(interface)。
您需要在 fragment 中创建一个这样的界面
public interface ButtonCreator{
void buttonCreator(BitmapDrawable bitmapDrawable, String string);
}
并且在您的 Activity 中您将需要实现此方法。像这样:
@Override
public void buttonCreator(BitmapDrawable bitmap,String a) {
HomeFragment homeFragment = (HomeFragment)getSupportFragmentManager().getFragments().get(1);
homeFragment.createButton(bitmap,a);
}
不要忘记实现这样的接口(interface)
public class MainActivity extends FragmentActivity implements MenuFragment.OnMenufragListener, HomeFragment.ButtonCreator {
此外,在您的 fragment 中,您必须在 onAttach 方法中获取此内容
private ButtonCreator bc;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
bc = (ButtonCreator) activity;
}
你会打电话
buttonCreator(bitmapdrawable, applicationName);
代替
createbutton.createButton(bitmapdrawable, applicationName);
这会起作用。不过,我在您的按钮实现中也发现了一个错误。
你必须调查一下。
btn.setId(id);
上面的行需要像 R.id.something
这样的 id,而不是整数。我相信你会解决这个问题。
关于java - LayoutInflater 充气器和 ViewGroup 容器的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36993849/