我在平板电脑上使用我的应用时得到NullPointerException
,而它在Emulater(平板电脑+手机)和手机上运行正常。
问题似乎是在与其他片段进行通信时。
我的平板电脑是1280x800,7“
我有五个XML文件:
activity_main.xml
activity_main.xml(land)
activity_main.xml(sw600dp)
iteminfo_fragment.xml
listview_fragment.xml
Activity_Main.java
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements ListViewFragment.OnitemClickedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(findViewById(R.id.layout_container) != null){
if (savedInstanceState != null){
return;
}
ListViewFragment listViewFragment = new ListViewFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.layout_container,listViewFragment);
fragmentTransaction.commit();
}
}
@Override
public void onItemClick(int position) {
ItemInfo_Fragment itemInfo_fragment = (ItemInfo_Fragment) getFragmentManager().
findFragmentById(R.id.ListView_ItemFragmentID);
if(itemInfo_fragment != null) {
itemInfo_fragment.updateInfo(position);
} else {
ItemInfo_Fragment itemInfo_fragment1 = new ItemInfo_Fragment();
Bundle args = new Bundle();
args.putInt("position", position);
itemInfo_fragment1.setArguments(args);
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
fragmentTransaction.replace(R.id.layout_container, itemInfo_fragment1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
}
ListViewFragment.java
import android.app.Fragment;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* Created by Sarbast on 30/06/2016.
*/
public class ListViewFragment extends ListFragment {
OnitemClickedListener mCallBack;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.listview_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] data = getResources().getStringArray(R.array.heros);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, data));
}
@Override
public void onStart() {
super.onStart();
//Highlight the item when using two-pane layout
Fragment fragmentManager = getFragmentManager()
.findFragmentById(R.id.ListView_ItemFragmentID);
ListView v = getListView();
if (fragmentManager != null && v != null){
v.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
mCallBack = (OnitemClickedListener) context;
}catch (ClassCastException e){
throw new ClassCastException(e.toString() + "Must implement OnItemClickedInstance");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
mCallBack.onItemClick(position);
}
public interface OnitemClickedListener {
public void onItemClick(int position);
}
}
itemInfo_Fragment.java
public class ItemInfo_Fragment extends Fragment {
TextView mTextView;
String[] mStrings;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.iteminfo_fragment, container, false);
mTextView = (TextView) v.findViewById(R.id.showInfo_ID);
mStrings = getResources().getStringArray(R.array.heros_info);
Bundle args = getArguments();
if (args != null) {
int index = args.getInt("position");
mTextView.setText(mStrings[index]);
}
return v;
}
public void updateInfo(int position){
mTextView.setText(mStrings[position]);
}
}
错误:
E / AndroidRuntime:致命异常:主要流程: com.sarbast.android.listfragmenttest,PID:26930 java.lang.NullPointerException:尝试调用接口方法 “无效 com.sarbast.android.listfragmenttest.ListViewFragment $ OnitemClickedListener.onItemClick(INT)” 在null对象引用 COM。******。android.listfragmenttest.ListViewFragment.onListItemClick(ListViewFragment.java:63) 在android.app.ListFragment $ 2.onItemClick(ListFragment.java:160)at android.widget.AdapterView.performItemClick(AdapterView.java:340)at android.widget.AbsListView.performItemClick(AbsListView.java:1532)
在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3700) 在android.widget.AbsListView $ 3.run(AbsListView.java:5686)at android.os.Handler.handleCallback(Handler.java:739)at android.os.Handler.dispatchMessage(Handler.java:95)at android.os.Looper.loop(Looper.java:145)at android.app.ActivityThread.main(ActivityThread.java:6939)at java.lang.reflect.Method.invoke(Native Method)at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
答案 0 :(得分:0)
你的回调界面永远不会被初始化,
所以调用方法
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
mCallBack.onItemClick(position);
}
抛出空指针Exception
你需要在触发回调之前检查null
if(mCallBack!=null){
mCallBack.onItemClick(position);
}
或定义一个setter方法,允许您正确初始化接口。
答案 1 :(得分:0)
谢天谢地,我找到了答案,因为我使用的API低于23,这是我平板电脑上的22并且不使用支持库,因此onAttach(Context context)
永远不会被调用,所以我不得不使用onAttach(Activity activity)
< / p>