从活动的呼吁片段抛出空指针的Andr​​oid与异常 [英] calling fragment from activity throws null pointer exception- Android

查看:163
本文介绍了从活动的呼吁片段抛出空指针的Andr​​oid与异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打电话从片段活动列表片段,我m到处空指针exception.Here是我的code,

 公共类SampleEvents扩展FragmentActivity {    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_sample_events);
        FragmentManager FM = getFragmentManager();
        片段片段= fm.findFragmentById(R.id.fragment_content);
        如果(片段== NULL){
            FragmentTransaction英尺= fm.beginTransaction();
            ft.add(R.id.fragment_content,新EventsList());
            ft.commit();
        }
    }

片段活动的布局

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>
  <的FrameLayout
        机器人:ID =@ + ID / fragment_content
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT/>
< / LinearLayout中>

片段EVENT_LIST的布局

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=@绘制/背景
    机器人:方向=垂直>
     <的TextView
        机器人:ID =@ + ID / empty_TV
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_centerVertical =真
        机器人:文字颜色=#0c2c0c
        机器人:文字=没有公告找到
        机器人:文字样式=大胆
        机器人:TEXTSIZE =20SP/>
     < ListView控件
         机器人:ID =@ + ID / events_list
         机器人:填充=10dip
         机器人:layout_width =match_parent
         机器人:layout_height =WRAP_CONTENT
         >
     < /&的ListView GT;
< / RelativeLayout的>

片段类

 公共类EventsList扩展ListFragment工具
android.widget.AdapterView.OnItemClickListener {    查看rootView;
    静态共享preferences US preFS;
    私人NetWorkConnection NC;
    私人BaseAlerts警报;
    私人的ListView events_listview;
    私人TextView的emptyTV;
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){
        rootView = inflater.inflate(R.layout.events_list,集装箱,FALSE);
        initUI();
        uiListener();
        返回rootView;
}

请告诉我错了我的code?有人能帮助?

日志猫

  06-10 19:22:08.248:E / AndroidRuntime(1009):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.ie.minoritybussiness / com.ie。 minoritybussiness.dashboard.SampleEvents}了java.lang.RuntimeException:内容视图与id属性'android.R.id.list'不是一个ListView类
06-10 19:22:08.248:E / AndroidRuntime(1009):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-10 19:22:08.248:E / AndroidRuntime(1009):在android.app.ActivityThread.access $ 600(ActivityThread.java:130)
06-10 19:22:08.248:E / AndroidRuntime(1009):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195)
06-10 19:22:08.248:E / AndroidRuntime(1009):在android.os.Handler.dispatchMessage(Handler.java:99)
06-10 19:22:08.248:E / AndroidRuntime(1009):在android.os.Looper.loop(Looper.java:137)
06-10 19:22:08.248:E / AndroidRuntime(1009):在android.app.ActivityThread.main(ActivityThread.java:4745)
06-10 19:22:08.248:E / AndroidRuntime(1009):在java.lang.reflect.Method.invokeNative(本机方法)
06-10 19:22:08.248:E / AndroidRuntime(1009):在java.lang.reflect.Method.invoke(Method.java:511)
06-10 19:22:08.248:E / AndroidRuntime(1009):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
06-10 19:22:08.248:E / AndroidRuntime(1009):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-10 19:22:08.248:E / AndroidRuntime(1009):在dalvik.system.NativeStart.main(本机方法)
06-10 19:22:08.248:E / AndroidRuntime(1009):了java.lang.RuntimeException:通过引起内容与观点id属性'android.R.id.list'不是一个ListView类
06-10 19:22:08.248:E / AndroidRuntime(1009):在android.app.ListFragment.ensureList(ListFragment.java:402)
06-10 19:22:08.248:E / AndroidRuntime(1009):在android.app.ListFragment.onViewCreated(ListFragment.java:203)
06-10 19:22:08.248:E / AndroidRuntime(1009):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:843)
06-10 19:22:08.248:E / AndroidRuntime(1009):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)


解决方案

  

内容视图与id属性'android.R.id.list'不是一个ListView类


这看起来像你的问题。有2个原因,我能想到的这是发生的历史。


  1. 您是给Android的ID到非列表视图。检查你的XML文件的任何地方使用的是 @android:ID /列表作为视图的ID。它重命名为别的东西。


  2. 您使用的是 ListFragment / ListActivity 不会确定与视图@android:ID /列表作为ID。这些课程都要求标识符。见<一href=\"http://stackoverflow.com/questions/17899086/fatal-exception-main-java-lang-runtimeexception-content-has-view-with-id-attri\">this堆栈溢出后了解一些更多的细节。


I am trying to call a List fragment from fragment activity, I m getting null pointer exception.Here is my code,

public class SampleEvents extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample_events);
        FragmentManager fm       = getFragmentManager();
        Fragment        fragment = fm.findFragmentById(R.id.fragment_content); 
        if (fragment == null) {
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.fragment_content, new EventsList());
            ft.commit(); 
        }


    }

Layout of fragment activity

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  <FrameLayout
        android:id="@+id/fragment_content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

Layout of the fragment event_list

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >
     <TextView
        android:id="@+id/empty_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="#0c2c0c"
        android:text="No Announcements Found"
        android:textStyle="bold"
        android:textSize="20sp" />
     <ListView
         android:id="@+id/events_list"
         android:padding="10dip"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         >
     </ListView>
</RelativeLayout>

Fragment class

public class EventsList extends ListFragment  implements
android.widget.AdapterView.OnItemClickListener{

    View rootView;
    static SharedPreferences sPrefs;
    private NetWorkConnection nc;
    private BaseAlerts alert;
    private ListView events_listview;
    private TextView emptyTV;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.events_list, container, false);
        initUI();
        uiListener();
        return rootView;
}

Whats wrong with my code?Can someone help?

The log cat

06-10 19:22:08.248: E/AndroidRuntime(1009): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ie.minoritybussiness/com.ie.minoritybussiness.dashboard.SampleEvents}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
06-10 19:22:08.248: E/AndroidRuntime(1009):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-10 19:22:08.248: E/AndroidRuntime(1009):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-10 19:22:08.248: E/AndroidRuntime(1009):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-10 19:22:08.248: E/AndroidRuntime(1009):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 19:22:08.248: E/AndroidRuntime(1009):     at android.os.Looper.loop(Looper.java:137)
06-10 19:22:08.248: E/AndroidRuntime(1009):     at android.app.ActivityThread.main(ActivityThread.java:4745)
06-10 19:22:08.248: E/AndroidRuntime(1009):     at java.lang.reflect.Method.invokeNative(Native Method)
06-10 19:22:08.248: E/AndroidRuntime(1009):     at java.lang.reflect.Method.invoke(Method.java:511)
06-10 19:22:08.248: E/AndroidRuntime(1009):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-10 19:22:08.248: E/AndroidRuntime(1009):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-10 19:22:08.248: E/AndroidRuntime(1009):     at dalvik.system.NativeStart.main(Native Method)
06-10 19:22:08.248: E/AndroidRuntime(1009): Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
06-10 19:22:08.248: E/AndroidRuntime(1009):     at android.app.ListFragment.ensureList(ListFragment.java:402)
06-10 19:22:08.248: E/AndroidRuntime(1009):     at android.app.ListFragment.onViewCreated(ListFragment.java:203)
06-10 19:22:08.248: E/AndroidRuntime(1009):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:843)
06-10 19:22:08.248: E/AndroidRuntime(1009):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)

解决方案

Content has view with id attribute 'android.R.id.list' that is not a ListView class

That looks like your issue. There are 2 reasons I can think of for this to be occuring.

  1. You are giving the android id to a non-list view. Check out your XML files for anywhere you are using @android:id/list as an id for a view. Rename it to something else.

  2. You are using a ListFragment/ListActivity and are not defining a view with @android:id/list as the id. These classes require that identifier. See this Stack Overflow post for some more details.

这篇关于从活动的呼吁片段抛出空指针的Andr​​oid与异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆