相关疑难解决方法(0)

片段中的findViewById

我试图在片段中创建一个ImageView,它将引用我在片段的XML中创建的ImageView元素.但是,该findViewById方法仅在扩展Activity类时才有效.无论如何,我还可以在片段中使用它吗?

public class TestClass extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ImageView imageView = (ImageView)findViewById(R.id.my_image);
        return inflater.inflate(R.layout.testclassfragment, container, false);
    }
}
Run Code Online (Sandbox Code Playgroud)

findViewById方法有一个错误,表明该方法是未定义的.

android android-imageview android-fragments findviewbyid

975
推荐指数
23
解决办法
56万
查看次数

使用Kotlin自定义Android视图

我正在尝试在我的Android项目中使用Kotlin.我需要创建自定义视图类.每个自定义视图都有两个重要的构造函数

public class MyView extends View {
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}
Run Code Online (Sandbox Code Playgroud)

MyView(Context)用于在代码中实例化视图,并MyView(Context, AttributeSet)在从XML 扩展布局时由布局inflater调用.

这个问题的回答表明我使用默认值或工厂方法的构造函数.但这就是我们所拥有的:

工厂方法:

fun MyView(c: Context) = MyView(c, attrs) //attrs is nowhere to get
class MyView(c: Context, attrs: AttributeSet) : View(c, attrs) { ... }
Run Code Online (Sandbox Code Playgroud)

要么

fun MyView(c: Context, attrs: AttributeSet) = MyView(c) //no way to pass attrs.
                                                        //layout inflater can't use 
                                                        //factory methods
class MyView(c: Context) : View(c) { …
Run Code Online (Sandbox Code Playgroud)

android constructor kotlin

41
推荐指数
5
解决办法
2万
查看次数

开发Gallery视图时setAdapter出现错误

基本上,我正在尝试创建一个具有图片库视图的应用程序。我使用网格视图进行了画廊视图。使用片段,而不是活动。但是,打开图库会话时,应用程序崩溃。这是代码:

XML文件(fragment_gallery.xml):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    tools:context="com.example.tonymathew.eliteenglishclub.gallery">

    <!-- TODO: Update blank fragment layout -->

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gridView"
        android:columnWidth="100dp"
        android:stretchMode="columnWidth"

        android:numColumns="auto_fit"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="5dp"
        android:layout_gravity="center" />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

片段文件(gallery.java):

public class gallery extends Fragment {
    GridView gridView;

    public gallery() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_gallery, container, false);

        gridView = (GridView) container.findViewById(R.id.gridView);

        gridView.setAdapter(new ImageAdapter(this));

        return view;
    }

}
Run Code Online (Sandbox Code Playgroud)

适配器类(Image Adapter.java):

public class ImageAdapter extends BaseAdapter{
    private Context context;

    public Integer[] images …
Run Code Online (Sandbox Code Playgroud)

java android nullpointerexception android-fragments

5
推荐指数
1
解决办法
320
查看次数

findViewByID(R.layout.skeleton_activity)返回null

我想在骨架应用程序的OnCreate()中注册一个上下文菜单:

/** Called with the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Inflate our UI from its XML layout description.
    setContentView(R.layout.skeleton_activity);

    View v = findViewById(R.layout.skeleton_activity);
    registerForContextMenu(v);      

    // Find the text editor view inside the layout, because we
    // want to do various programmatic things with it.
    mEditor = (EditText) findViewById(R.id.editor);

    // Hook up button presses to the appropriate event handler.
    ((Button) findViewById(R.id.back)).setOnClickListener(mBackListener);
    ((Button) findViewById(R.id.clear)).setOnClickListener(mClearListener);

    mEditor.setText(getText(R.string.main_label));
}
Run Code Online (Sandbox Code Playgroud)

调试器告诉我findViewById(R.layout.skeleton_activity)返回null.

@CommonsWare对类似帖子的解决方案是等到onFinishInflate().但是,在他提供的示例项目中,他似乎并不等到onFinishInflate.

我的问题:

  1. registerForContextMenu()可以等到onFinishInflate()吗? …

android

2
推荐指数
1
解决办法
7491
查看次数

findViewById 返回 null。但为什么?

我有一个 Adroid Activity,我想引入一个自定义列表视图。这是我的 onCreate() 活动方法。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LoadListView();

    ListView lvw = (ListView)findViewById(android.R.id.list);

    lvw.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

            // selected item
            String element = ((TextView) view).getText().toString();

            // Launching new Activity on selecting single List Item
            Intent i = new Intent(getApplicationContext(), AddEntryActivity.class);
            // sending data to new activity
            i.putExtra("entryToEdit", element);
            long idToEdit = Long.parseLong(hEntries.get(position).toString());
            i.putExtra("idToEdit", idToEdit);
            startActivity(i);

        }
      });   

    DbAccess oDb = new DbAccess(this);
}
Run Code Online (Sandbox Code Playgroud)

如您所见,调用 LoadListView() 方法来加载列表视图项: …

eclipse android findviewbyid

2
推荐指数
1
解决办法
4138
查看次数