在片段中单击按钮实现后应用程序崩溃
App crashes after button click implementation in fragments
我的应用包含导航抽屉。其中有一个项目片段。在该片段中,我尝试实现三个按钮单击事件以打开 3 个活动。但是,每当我单击导航抽屉中的片段时,应用程序就会因 nullPointerAcception 而崩溃。调试后,它显示以下包含 onClickListener 的代码存在问题:
itemButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
startActivity(itemIntent);
}
});
这是我的片段代码。
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* A simple {@link Fragment} subclass.
*/
public class ItemsFragment extends Fragment {
Button itemButton;
Button categoryButton;
Button discountButton;
public ItemsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
itemButton=(Button) container.findViewById(R.id.items_button);
categoryButton=(Button) container.findViewById(R.id.category_button);
discountButton=(Button) container.findViewById(R.id.discount_button);
itemButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
startActivity(itemIntent);
}
});
setHasOptionsMenu(false);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_items, container, false);
}
}
// Logcat 错误
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.soumya.possystem.ItemsFragment.onCreateView(ItemsFragment.java:36)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:536)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
在扩充视图和创建视图之前,您试图在 onCreateView
中查找 controls.and return 扩充视图的实例
另一种方法是在onViewCreated
或onActivityCreated
中获取实例(findviewById
)
第二个问题是您正在尝试从 container
获取控件的对象,而不是您需要从膨胀视图中获取的对象。
View v=inflater.inflate(R.layout.fragment_items, container, false);
下面我保留了更改的代码。
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* A simple {@link Fragment} subclass.
*/
public class ItemsFragment extends Fragment {
Button itemButton;
Button categoryButton;
Button discountButton;
public ItemsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_items, container, false);
//check in place of container here you need to use inflated view (`v`) instance
itemButton=(Button) v.findViewById(R.id.items_button);
categoryButton=(Button) v.findViewById(R.id.category_button);
discountButton=(Button) v.findViewById(R.id.discount_button);
itemButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
startActivity(itemIntent);
}
});
setHasOptionsMenu(false);
// Inflate the layout for this fragment
return v;
}
}
您必须根据要求初始化视图:
Create a Fragment Class
所以你必须做
View view = inflater.inflate(R.layout.YOUR_LAYOUT, container, false);
然后
itemButton=(Button) view.findViewById(R.id.items_button);
试试这个
View rootView = inflater.inflate(R.layout.fragment_id, container, false);
itemButton=(Button) container.findViewById(R.id.items_button);
categoryButton=(Button) container.findViewById(R.id.category_button);
discountButton=(Button) container.findViewById(R.id.discount_button);
.
.
.
return rootView;
试试这个:
public class ItemsFragment extends Fragment {
Button itemButton;
Button categoryButton;
Button discountButton;
public ItemsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_items, container, false);
setHasOptionsMenu(false);
return v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
itemButton=(Button) view.findViewById(R.id.items_button);
categoryButton=(Button) view.findViewById(R.id.category_button);
discountButton=(Button) view.findViewById(R.id.discount_button);
itemButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
startActivity(itemIntent);
}
});
}
}
public class ItemsFragment extends Fragment {
Button itemButton;
Button categoryButton;
Button discountButton;
public ItemsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Inflate rootView of fragment **/
View rootView = inflater.inflate(R.layout.fragment_items, container, false);
//replace "container" with "rootView" **/
itemButton=(Button) rootView.findViewById(R.id.items_button);
categoryButton=(Button) rootView.findViewById(R.id.category_button);
discountButton=(Button) rootView.findViewById(R.id.discount_button);
itemButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
startActivity(itemIntent);
}
});
setHasOptionsMenu(false);
// Inflate the layout for this fragment
return rootView; // Return the view object you inflated
}
}
我的应用包含导航抽屉。其中有一个项目片段。在该片段中,我尝试实现三个按钮单击事件以打开 3 个活动。但是,每当我单击导航抽屉中的片段时,应用程序就会因 nullPointerAcception 而崩溃。调试后,它显示以下包含 onClickListener 的代码存在问题:
itemButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
startActivity(itemIntent);
}
});
这是我的片段代码。
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* A simple {@link Fragment} subclass.
*/
public class ItemsFragment extends Fragment {
Button itemButton;
Button categoryButton;
Button discountButton;
public ItemsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
itemButton=(Button) container.findViewById(R.id.items_button);
categoryButton=(Button) container.findViewById(R.id.category_button);
discountButton=(Button) container.findViewById(R.id.discount_button);
itemButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
startActivity(itemIntent);
}
});
setHasOptionsMenu(false);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_items, container, false);
}
}
// Logcat 错误
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.soumya.possystem.ItemsFragment.onCreateView(ItemsFragment.java:36)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:536)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
在扩充视图和创建视图之前,您试图在 onCreateView
另一种方法是在onViewCreated
或onActivityCreated
findviewById
)
第二个问题是您正在尝试从 container
获取控件的对象,而不是您需要从膨胀视图中获取的对象。
View v=inflater.inflate(R.layout.fragment_items, container, false);
下面我保留了更改的代码。
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* A simple {@link Fragment} subclass.
*/
public class ItemsFragment extends Fragment {
Button itemButton;
Button categoryButton;
Button discountButton;
public ItemsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_items, container, false);
//check in place of container here you need to use inflated view (`v`) instance
itemButton=(Button) v.findViewById(R.id.items_button);
categoryButton=(Button) v.findViewById(R.id.category_button);
discountButton=(Button) v.findViewById(R.id.discount_button);
itemButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
startActivity(itemIntent);
}
});
setHasOptionsMenu(false);
// Inflate the layout for this fragment
return v;
}
}
您必须根据要求初始化视图:
Create a Fragment Class
所以你必须做
View view = inflater.inflate(R.layout.YOUR_LAYOUT, container, false);
然后
itemButton=(Button) view.findViewById(R.id.items_button);
试试这个
View rootView = inflater.inflate(R.layout.fragment_id, container, false);
itemButton=(Button) container.findViewById(R.id.items_button);
categoryButton=(Button) container.findViewById(R.id.category_button);
discountButton=(Button) container.findViewById(R.id.discount_button);
.
.
.
return rootView;
试试这个:
public class ItemsFragment extends Fragment {
Button itemButton;
Button categoryButton;
Button discountButton;
public ItemsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_items, container, false);
setHasOptionsMenu(false);
return v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
itemButton=(Button) view.findViewById(R.id.items_button);
categoryButton=(Button) view.findViewById(R.id.category_button);
discountButton=(Button) view.findViewById(R.id.discount_button);
itemButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
startActivity(itemIntent);
}
});
}
}
public class ItemsFragment extends Fragment {
Button itemButton;
Button categoryButton;
Button discountButton;
public ItemsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Inflate rootView of fragment **/
View rootView = inflater.inflate(R.layout.fragment_items, container, false);
//replace "container" with "rootView" **/
itemButton=(Button) rootView.findViewById(R.id.items_button);
categoryButton=(Button) rootView.findViewById(R.id.category_button);
discountButton=(Button) rootView.findViewById(R.id.discount_button);
itemButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
startActivity(itemIntent);
}
});
setHasOptionsMenu(false);
// Inflate the layout for this fragment
return rootView; // Return the view object you inflated
}
}