- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
当我尝试设置/获取我的 EditText 时,我的应用程序抛出一个 NullPointerException
。
看来问题与我的 setContentView()
方法和我的 xml 布局有关,它们没有正确膨胀。我相信我必须更改我的 EditTexts 的 findViewByID
,例如更改为 view.findViewByID
。我已经尝试了 this.findViewByID
和 mViewPager.findViewByID
,但是我找不到错误..
我的部分代码:
private static ArrayList<String> roomList = new ArrayList<String>();
private static ArrayList<String> deviceList = new ArrayList<String>();
private static String project_name;
private static String router_ip;
private static String port;
private static String device_name;
private static String room_name;
private static String datatype;
private static String grpaddr;
private static boolean status;
private EditText et_project_name;
private EditText et_router_ip;
private EditText et_port;
private Spinner spinner_dpt;
private CheckBox cb_checkStatus;
private EditText et_device_name;
private EditText et_groupaddress;
private View textEntryView;
private static final String[] items={"1.001 (Lichtschalter)", "5.001 (Dimmer/Jalousien)",
"9.001 (Temperaturanzeige)", "1.008 (Jalousien)"};
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private static ManualConfigDBAdapter dbHelper;
/**
* OnCreate
* */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_project_manually);
setup();
}
/**
* Setup
* */
public void setup(){
//DATABASE
// Add project to Database
dbHelper = new ManualConfigDBAdapter(this);
dbHelper.open();
// Create the adapter that will return a fragment for each of the three primary sections
// of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the action bar.
final ActionBar actionBar = getActionBar();
// set the app icon as an action to go home
actionBar.setDisplayHomeAsUpEnabled(true);
//enable tabs in actionbar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding tab.
// We can also use ActionBar.Tab#select() to do this if we have a reference to the
// Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by the adapter.
// Also specify this Activity object, which implements the TabListener interface, as the
// listener for when this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
//get the controls from the layout
et_project_name = (EditText) findViewById(R.id.project_name);
et_router_ip = (EditText) findViewById(R.id.router_ip);
et_port = (EditText) findViewById(R.id.port);
//Use a input filter for the input of the IP adress
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (end > start) {
String destTxt = dest.toString();
String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend);
if (!resultingTxt.matches ("^\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) {
return "";
} else {
String[] splits = resultingTxt.split("\\.");
for (int i=0; i<splits.length; i++) {
if (Integer.valueOf(splits[i]) > 255) {
return "";
}
}
}
}
return null;
}
};
et_router_ip.setFilters(filters);
}
和 LogCat:
02-16 20:09:25.755: E/AndroidRuntime(26658): FATAL EXCEPTION: main
02-16 20:09:25.755: E/AndroidRuntime(26658): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.bertrandt.bertrandtknx/de.bertrandt.bertrandtknx.CreateProjectManually}: java.lang.NullPointerException
02-16 20:09:25.755: E/AndroidRuntime(26658): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
02-16 20:09:25.755: E/AndroidRuntime(26658): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
02-16 20:09:25.755: E/AndroidRuntime(26658): at android.app.ActivityThread.access$600(ActivityThread.java:128)
02-16 20:09:25.755: E/AndroidRuntime(26658): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
02-16 20:09:25.755: E/AndroidRuntime(26658): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 20:09:25.755: E/AndroidRuntime(26658): at android.os.Looper.loop(Looper.java:137)
02-16 20:09:25.755: E/AndroidRuntime(26658): at android.app.ActivityThread.main(ActivityThread.java:4514)
02-16 20:09:25.755: E/AndroidRuntime(26658): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 20:09:25.755: E/AndroidRuntime(26658): at java.lang.reflect.Method.invoke(Method.java:511)
02-16 20:09:25.755: E/AndroidRuntime(26658): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
02-16 20:09:25.755: E/AndroidRuntime(26658): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
02-16 20:09:25.755: E/AndroidRuntime(26658): at dalvik.system.NativeStart.main(Native Method)
02-16 20:09:25.755: E/AndroidRuntime(26658): Caused by: java.lang.NullPointerException
02-16 20:09:25.755: E/AndroidRuntime(26658): at de.bertrandt.bertrandtknx.CreateProjectManually.setup(CreateProjectManually.java:152)
02-16 20:09:25.755: E/AndroidRuntime(26658): at de.bertrandt.bertrandtknx.CreateProjectManually.onCreate(CreateProjectManually.java:75)
02-16 20:09:25.755: E/AndroidRuntime(26658): at android.app.Activity.performCreate(Activity.java:4562)
02-16 20:09:25.755: E/AndroidRuntime(26658): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
02-16 20:09:25.755: E/AndroidRuntime(26658): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
02-16 20:09:25.755: E/AndroidRuntime(26658): ... 11 more
02-16 20:09:25.770: D/dalvikvm(26658): GC_CONCURRENT freed 396K, 4% free 14704K/15303K, paused 2ms+3ms
XML 文件 activity_create_project_manually:
<RelativeLayout 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:background="@color/black" >
<RelativeLayout
android:id="@+id/project_save_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/black"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:text="@string/project_name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/project_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:ems="10"
android:hint="@string/project_name_hint" >
</EditText>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/project_name"
android:layout_marginTop="10dp"
android:text="@string/router_ip"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/router_ip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView4"
android:digits="0123456789."
android:ems="10"
android:hint="@string/router_adress_info"
android:inputType="numberDecimal|phone" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/placeholder"
android:src="@drawable/bertrandtlogoinv" />
<TextView
android:id="@+id/info_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageView1"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="@string/info_settings"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16dp" />
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ImageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/info_text"
android:layout_alignParentLeft="true"
android:contentDescription="@string/placeholder"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:scaleType="fitXY"
android:src="@color/ics_blue" />
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/imageView1"
android:contentDescription="@string/placeholder"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:scaleType="fitXY"
android:src="@color/ics_blue" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/router_ip"
android:layout_marginTop="10dp"
android:text="@string/Port"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/port"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView3"
android:digits="0123456789"
android:ems="10"
android:hint="@string/port_hint"
android:inputType="number"
android:maxLength="5" />
</RelativeLayout>
</RelativeLayout>
XML 文件设置_手动:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CreateProjektManually" />
以下行引发错误:
et_router_ip.setFilters(filters);
如果我在 Debug模式下检查它们,所有三个 EditText 字段的值都是 null
。
最佳答案
要使用 View 寻呼机创建 3 个不同的 fragment ,您应该:
1)使用 View 分页器和 View 分页器适配器创建 FragmentActivity。
public class FragmentActivity extends FragmentActivity
{
private FragmentAdapter adapter;
private ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
adapter = new FragmentAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
}
2)用静态实例创建3个 fragment
public final class Fragment1 extends Fragment
{
public static Fragment1 newInstance() {
return new Fragment1();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout layout = new LinearLayout(getActivity());
// your fragment xml view
return view;
}
}
3)用它填充 View 寻呼机适配器;
public class FragmentAdapter extends FragmentPagerAdapter
{
public InstallFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return Fragment1.newInstance();
case 1:
return Fragment2.newInstance();
case 2:
return Fragment3.newInstance();
}
return null;
}
}
关于android - 在 Android 中使用 ViewPager 和 fragment 的 EditText 出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12935705/
我正在将我的模板代码移植到 XTend。在某些时候,我在测试用例中有这种类型的条件处理: @Test def xtendIfTest() { val obj = new FD if (
我是新来的 kotlin , 当我开始 Null Safety 时,我对下面的情况感到困惑. There's some data inconsistency with regard to initia
我的应用程序一直在各种Android版本中保持良好状态。我有用户在Android 4.3、5.0、5.1和6.0上正常运行。但是,具有S7 Edge的用户刚刚更新了Android 7.0,将文本粘贴到
我使用的是最新版本的 LWUIT (1.5)。我在资源编辑器中设计了我的表单,然后将代码生成到 netbeans。问题是如果我想访问除表单之外的任何对象,我会收到此错误: java.lang.Null
更新: 我在 Fedora 21 上运行它。 SonarQube - 5.0。 SonarQube Runner - 2.4 更新 2:Findbugs v3.1,Java 插件 v2.8 更新3:
RecupData 我的类仅在 web 中返回 NullPointerException。我连接到 pgsql db 8.3.7 - 该脚本在“控制台”syso 中运行良好 - 但引发了测试 Web
我在 mac 上使用 Processing 2.08。我正在尝试使用文档中给出的 createShape 函数创建 PShape。 PShape s; void setup(){ size(500
我在 mac 上使用 Processing 2.08。我正在尝试使用文档中给出的 createShape 函数创建 PShape。 PShape s; void setup(){ size(500
每次运行此 jsp 时,都会收到以下错误异常: org.apache.jasper.JasperException: java.lang.NullPointerException root cause
Kotlin 在编译时有一个出色的 null 检查,使用分离到“可空?”和“不可为空”的对象。它有一个 KAnnotator 来帮助确定来自 Java 的对象是否可以为空。但是,如果 not-null
我有一个布局将显示一个TextView,用于显示一个滴答时间。我遵循了此链接中的代码 How to Display current time that changes dynamically for
Elasticsearch 1.4.1版(“lucene_version”:“4.10.2”) 我有一个像这样的文件: $ curl 'http://localhost:9200/blog/artic
这是我从另一个类调用函数的方法Selenium 设置已定义。 public void Transfer() throws Exception { System.out.println("\nTrans
我试图在主类中使用我在此类中创建的函数,但它崩溃并显示“警告:无法在根 0 处打开/创建首选项根节点 Software\JavaSoft\Prefsx80000002。 Windows RegCrea
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 3 年前。 我有一个 Java 代码,它将
我声明了两张牌: Card card1 = new Card('3', Card.Suit.clubs); Card card2 = new Card('T', Card.Suit.diamonds)
我编写了一段代码来解码 Base64 图像并在 javafx 中表示该图像。在我的 url base64 代码中不断变化。这就是我在 javafx 代码中使用任务的原因。但我收到错误:java.lan
我正在尝试使用 arrayList 的 arrayList 在 Java 中实现图形。 每当调用 addEdge 函数时,我都会收到 NullPointerException 。我似乎无法弄清楚为什么
我是 Java/android 的新手,所以很多这些术语都是外国的,但我愿意学习。我不打算详细介绍该应用程序,因为我认为它不相关。我目前的问题是,我使用了博客中的教程和代码 fragment ,并使我
我正在开发一个 Android 应用程序来在 Android developer guide 的帮助下录制视频.我程序上的所有代码都与此页面相同。 我在 之外定义了权限标签。 当应
我是一名优秀的程序员,十分优秀!