- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的文件代码,旨在使用布局动态添加 View
我指的是 http://maxmakedesign.co.uk/how-to/80/android-add-views-in-a-loop
package com.example.database;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.util.Log;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class Dynamic extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic);
String[] info = new String[2];
info[0] = "info 1";
info[1] = "info 2";
LayoutInflater layoutInflator = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout insertPoint = (LinearLayout) findViewById(R.id.insert_point);
List views = new ArrayList();
for(int i1=0; i1 < info.length; i1++)
{
View view = layoutInflator.inflate(R.layout.activity_dynamic, null);
TextView textView = (TextView) view.findViewById(R.id.a_text_view);
textView.setText(info[0]); /* if I comment this out , no force close */
//views.add(view);
}
/*
for(int i = 0; i< views.size(); i++)
{
insertPoint.addView((View) views.get(i));
}*/
}
}
如果我注释掉“textView.setText(info[0]);”我没有用力接近。但是,有了它,我就接近了。我想知道为什么?
这是我的 Logcat 文本:
> 03-07 21:26:04.880: E/Trace(20025): error opening trace file: No such file or directory (2)
03-07 21:26:05.681: D/libEGL(20025): loaded /system/lib/egl/libEGL_adreno200.so
03-07 21:26:05.691: D/libEGL(20025): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
03-07 21:26:05.701: D/libEGL(20025): loaded /system/lib/egl/libGLESv2_adreno200.so
03-07 21:26:05.711: I/Adreno200-EGL(20025): <qeglDrvAPI_eglInitialize:294>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB.04.01.01.00.036_msm8960_JB_CL2644550_release_AU (CL2644550)
03-07 21:26:05.711: I/Adreno200-EGL(20025): Build Date: 07/31/12 Tue
03-07 21:26:05.711: I/Adreno200-EGL(20025): Local Branch:
03-07 21:26:05.711: I/Adreno200-EGL(20025): Remote Branch: quic/master
03-07 21:26:05.711: I/Adreno200-EGL(20025): Local Patches: NONE
03-07 21:26:05.711: I/Adreno200-EGL(20025): Reconstruct Branch: AU_LINUX_ANDROID_JB.04.01.01.00.036 + NOTHING
03-07 21:26:05.771: D/OpenGLRenderer(20025): Enabling debug mode 0
03-07 21:27:38.542: E/Trace(20165): error opening trace file: No such file or directory (2)
03-07 21:27:38.952: D/AndroidRuntime(20165): Shutting down VM
03-07 21:27:38.952: W/dalvikvm(20165): threadid=1: thread exiting with uncaught exception (group=0x41bf3300)
03-07 21:27:38.952: E/AndroidRuntime(20165): FATAL EXCEPTION: main
03-07 21:27:38.952: E/AndroidRuntime(20165): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.Dynamic}: java.lang.NullPointerException
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2215)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.ActivityThread.access$600(ActivityThread.java:145)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.os.Looper.loop(Looper.java:137)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.ActivityThread.main(ActivityThread.java:4978)
03-07 21:27:38.952: E/AndroidRuntime(20165): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 21:27:38.952: E/AndroidRuntime(20165): at java.lang.reflect.Method.invoke(Method.java:511)
03-07 21:27:38.952: E/AndroidRuntime(20165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-07 21:27:38.952: E/AndroidRuntime(20165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
03-07 21:27:38.952: E/AndroidRuntime(20165): at dalvik.system.NativeStart.main(Native Method)
03-07 21:27:38.952: E/AndroidRuntime(20165): Caused by: java.lang.NullPointerException
03-07 21:27:38.952: E/AndroidRuntime(20165): at com.example.database.Dynamic.onCreate(Dynamic.java:48)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.Activity.performCreate(Activity.java:5008)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-07 21:27:38.952: E/AndroidRuntime(20165): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-07 21:27:38.952: E/AndroidRuntime(20165): ... 11 more
03-07 21:27:40.253: I/Process(20165): Sending signal. PID: 20165 SIG: 9
03-07 21:32:47.353: E/Trace(20649): error opening trace file: No such file or directory (2)
03-07 21:32:47.613: D/AndroidRuntime(20649): Shutting down VM
03-07 21:32:47.613: W/dalvikvm(20649): threadid=1: thread exiting with uncaught exception (group=0x41bf3300)
03-07 21:32:47.613: E/AndroidRuntime(20649): FATAL EXCEPTION: main
03-07 21:32:47.613: E/AndroidRuntime(20649): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.Dynamic}: java.lang.NullPointerException
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2215)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.ActivityThread.access$600(ActivityThread.java:145)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.os.Looper.loop(Looper.java:137)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.ActivityThread.main(ActivityThread.java:4978)
03-07 21:32:47.613: E/AndroidRuntime(20649): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 21:32:47.613: E/AndroidRuntime(20649): at java.lang.reflect.Method.invoke(Method.java:511)
03-07 21:32:47.613: E/AndroidRuntime(20649): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-07 21:32:47.613: E/AndroidRuntime(20649): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
03-07 21:32:47.613: E/AndroidRuntime(20649): at dalvik.system.NativeStart.main(Native Method)
03-07 21:32:47.613: E/AndroidRuntime(20649): Caused by: java.lang.NullPointerException
03-07 21:32:47.613: E/AndroidRuntime(20649): at com.example.database.Dynamic.onCreate(Dynamic.java:48)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.Activity.performCreate(Activity.java:5008)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-07 21:32:47.613: E/AndroidRuntime(20649): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-07 21:32:47.613: E/AndroidRuntime(20649): ... 11 more
03-07 21:32:49.075: I/Process(20649): Sending signal. PID: 20649 SIG: 9
activity_dynamic.xml 的内容是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/insert_point"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginLeft="22dp"
android:text="Add View" />
</LinearLayout>
感谢任何帮助!
干杯
最佳答案
因为您需要在 activity_dynamic
布局中添加一个 TextView
,id 为 a_text_view
。在 activity_dynamic
布局中添加一个 TextView 为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/insert_point"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginLeft="22dp"
android:text="Add View" />
<TextView
android:id="@+id/a_text_view" <!-- TextView id here -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- other TextView Attributes... -->
/>
</LinearLayout>
关于android - 强制关闭 - LayoutInflater,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15276003/
好吧,这很奇怪。当我在模拟器中运行应用程序时,我的 logcat 突然发生崩溃,之前我从未遇到过它,并且自上次在模拟器中运行应用程序以来我没有对我的应用程序进行任何更改几周前,它正在运行。 这是完整的
我使用了构造函数,但它不起作用。 LayoutInflater layoutInflater = new LayoutInflater(getContext()); Android Studio 用红
至少我认为这是问题所在。来自 API 文档: inflate(int resource, ViewGroup root) Inflate a new view hierarchy from the s
我有一个用于 listView 的自定义适配器。当我尝试膨胀某些 View 时,它会抛出异常。这是我的 getView() 代码: public View getView(int position,
我是 Android 开发的新手,我对 Android 开发社区中流传的“Inflate a View”这个词很好奇。 我知道它与调用 LayoutInflater 类的 Inflate 方法有关,但
这是我的文件代码,旨在使用布局动态添加 View 我指的是 http://maxmakedesign.co.uk/how-to/80/android-add-views-in-a-loop packa
我想在 OnCreateView 中设置 wordtoSpan 字符串。 我的代码: package com.tachles; import android.os.Bundle; import and
我有以下表示 ImageButton 的 xml 对象 我尝试使用以下代码将其膨胀并添加到 TableRow 中: LayoutInflater inflater= this.getLayout
事情是这样的,我有一个 RecycleView 适配器,它可以在 onCreateViewHolder 上扩展布局。当我尝试执行以下操作以发生这种情况时: 应用创建没有任何问题 进入多任务窗口 回到应
我只是偶尔在一台设备上遇到这个错误: 04-09 16:05:22.016: E/AndroidRuntime(31843): FATAL EXCEPTION: main 04-09 16
这是我的类 DownloadableProjectsFromWebAdapter 的 getView 方法: @Override public View getView(int position, V
我的问题是创建 LayoutInflater 实例的最佳方法是什么?有什么区别吗 LayoutInflater inflater = LayoutInflater.from(context); 和 L
我正在考虑编写一个自定义适配器来填充一个 ListView ,每行 3 个 TextView 。我找到了相当多的示例代码来执行此操作,但看起来最好的代码位于:http://www.anddev.org
我在 LayoutInflater 代码中看到实际 View 类是 instantiated via reflection (LayoutInflater - createView() 方法),但是,
我有一个 Fragment,它在 onCreateView 方法中有很多代码,所以我认为将这些代码放在外部线程中是个好主意。现在的问题是我必须在此线程中加载布局,但是如果我在 Activity 的 o
我们目前正在大学里使用 ListView 。我的讲师给了我们一个简单的应用程序,它在列表中显示邮件消息,当用户选择一个时,它会在新 Activity 中显示消息的内容。我几乎了解所有发生的事情,但我想
我对这段代码感到困惑 在这里,他们使用了抽象类并继承了它,最重要的是,他们在不创建对象的情况下调用了方法 附注完整代码:- https://github.com/udacity/ud839_Custo
我正在开发一个具有抽屉导航并且需要用户登录的应用程序。 到目前为止,我已经完成了整个用户登录/注册工作,并且我已经构建了抽屉导航。 我现在想做的是让抽屉导航上的 TextView 显示用户的全名和他/
我正在寻找一种方法来“扩充”Android XML 布局,这在编译时是未知的。我已经搜索了几个小时,总能找到答案,这是不可能的,因为 LayoutInflater 不能使用简单的 XML 文件。好的,
我试图在回收站 View 适配器中创建一个弹出窗口,以便在用户单击回收站 View 的某个项目时显示一个小菜单。这是弹出功能: public void Popup(final int id) {
我是一名优秀的程序员,十分优秀!