The following code throws a NullPointerException and I do not know why. The problem seems that I am not referencing the EditText objects correctly. But I don’t know how to make it correct.
Here the interesting part of my code:
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) mViewPager.findViewById(R.id.project_name);
et_router_ip = (EditText) mViewPager.findViewById(R.id.router_ip);
et_port = (EditText) mViewPager.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);
}
And 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
The used xml-file:
<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>
The following line throws the error:
et_router_ip.setFilters(filters);
SOLUTION
To create a 3 different fragments with view pager you should:
1)Create FragmentActivity with view pager and view pager adapter.
2)Create 3 Fragments with static instance
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)Fill view pager adapter with it;
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;
}
}
R.id.router_ipexists but it does not exist within a ViewPager element with the id of pager according to all the code you have posted. By calling findViewById() on the reference to your ViewPager you are only looking within that ViewPager object forR.id.router_ipand not globally within all inflated XML elements.If the above XML file called is not called
activity_create_project_manually.xml, then your problem extends from not having inflated the required XML into memory before using elements within it.setContentView()inflates an XML file into memory. You can also do it with a LayoutInflater but I don’t think this solution is for you.