【问题标题】:why findViewById return null in android-section-list为什么 findViewById 在 android-section-list 中返回 null
【发布时间】:2013-09-27 10:27:13
【问题描述】:

Here is the original code

我在 src 根目录下创建了一个 testing 包,以 SectionListActivityExtended 结尾

Is 只是一个普通的别名,但在不同的包中:

package testing;

import pl.polidea.sectionedlist.SectionListActivity;

public class SectionListActivityExtended extends SectionListActivity{

}

修改了AndroidManifest.xml部分:

   <activity
        android:name="testing.SectionListActivityExtended"
        android:label="@string/app_name"
        android:theme="@style/SectionListViewTheme" >

并修改了SectionListActivity:

@Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        arrayAdapter = new StandardArrayAdapter(this, R.id.example_text_view, exampleArray);
        sectionAdapter = new SectionListAdapter(getLayoutInflater(), arrayAdapter);
        String className = SectionListActivity.class.getPackage().getName();//className: pl.polidea.sectionedlist
        System.out.println("className: "+className);//className: pl.polidea.sectionedlist
        listView = (SectionListView) findViewById(getResources().getIdentifier("section_list_view", "id", className));
        listView.setAdapter(sectionAdapter);
    }

经过测试,正在运行,它写出了带有类名标签的包名,现在不重要了。

我已经创建了我的工作应用程序(它在 NDA 下有很多样式和定义)我已经将带有所需文件的源代码复制到我的项目中,希望我能开始工作。修改了SectionListActivity,从扩展类调用,类似上面:不同的根包。

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        arrayAdapter = new StandardArrayAdapter(this, R.id.example_text_view, exampleArray);
        sectionAdapter = new SectionListAdapter(getLayoutInflater(), arrayAdapter);

//      FrameLayout frameLayout = (FrameLayout) findViewById(R.id.listView);
//      if (frameLayout == null) {
//          Log.e("tag", "got frameLayout");
//      } else {
//
//          Log.e("tag", "frameLayout is NULL");
//      }

        String packageName = SectionListActivity.class.getPackage().getName();//pl.polidea.sectionedlist
        System.out.println("packageName: " + packageName);
        listView = (SectionListView) findViewById(getResources().getIdentifier("section_list_view", "id", packageName));
        if (listView != null) {
            listView.setAdapter(sectionAdapter);
        } else {
            Log.e(packageName, "Can't load section_list_view ");
        }
    }

frameLayoutlistView 均为空。

从其他帖子来看,主要原因是他们没有将 attrs 传递给 super,但是这部分代码没有受到影响:

public SectionListView(final Context context, final AttributeSet attrs) {
    super(context, attrs);
    commonInitialisation();
}

也许在造型上它没有合并一些东西,但不知道如何追踪,或者我错过了一些东西并且还不知道。

有什么想法吗?

【问题讨论】:

    标签: android inheritance layout android-activity classloader


    【解决方案1】:

    这样做

    listView = (SectionListView) findViewById(getResources().getIdentifier("section_list_view", "id", getPackageName()));
    

    【讨论】:

    • 找不到更改的原因,但我已经尝试过了。现在包名称位于调用包名称处。即使在原始代码上也无法正常工作,这就是为什么需要更改该部分
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    相关资源
    最近更新 更多