Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.09 KB | None | 0 0
  1. E/AndroidRuntime: FATAL EXCEPTION: main
  2. Process: com.example.user.planner, PID: 21140
  3. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.planner/com.example.user.planner.Customer}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
  4. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
  5. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
  6. at android.app.ActivityThread.-wrap14(ActivityThread.java)
  7. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
  8. at android.os.Handler.dispatchMessage(Handler.java:102)
  9. at android.os.Looper.loop(Looper.java:154)
  10. at android.app.ActivityThread.main(ActivityThread.java:6688)
  11. at java.lang.reflect.Method.invoke(Native Method)
  12. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
  13. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
  14. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
  15. at com.example.user.planner.Customer.AddData(Customer.java:68)
  16. at com.example.user.planner.Customer.onCreate(Customer.java:44)
  17. at android.app.Activity.performCreate(Activity.java:6912)
  18. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
  19. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
  20. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008) 
  21. at android.app.ActivityThread.-wrap14(ActivityThread.java) 
  22. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) 
  23. at android.os.Handler.dispatchMessage(Handler.java:102) 
  24. at android.os.Looper.loop(Looper.java:154) 
  25. at android.app.ActivityThread.main(ActivityThread.java:6688) 
  26. at java.lang.reflect.Method.invoke(Native Method) 
  27. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
  28. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
  29.  
  30. package com.example.user.planner;
  31.  
  32. import android.support.design.widget.TabLayout;
  33. import android.support.design.widget.FloatingActionButton;
  34. import android.support.design.widget.Snackbar;
  35. import android.support.v7.app.AppCompatActivity;
  36. import android.support.v7.widget.Toolbar;
  37.  
  38. import android.support.v4.app.Fragment;
  39. import android.support.v4.app.FragmentManager;
  40. import android.support.v4.app.FragmentPagerAdapter;
  41. import android.support.v4.view.ViewPager;
  42. import android.os.Bundle;
  43. import android.view.LayoutInflater;
  44. import android.view.Menu;
  45. import android.view.MenuItem;
  46. import android.view.View;
  47. import android.view.ViewGroup;
  48.  
  49. import android.widget.Button;
  50. import android.widget.EditText;
  51. import android.widget.TextView;
  52. import android.widget.Toast;
  53.  
  54. public class Customer extends AppCompatActivity {
  55. DatabaseHelper myDb;
  56. EditText editName, editSurname, editAddress, editContact;
  57. Button addDataBtn;
  58.  
  59. private SectionsPagerAdapter mSectionsPagerAdapter;
  60. private ViewPager mViewPager;
  61.  
  62. @Override
  63. protected void onCreate(Bundle savedInstanceState) {
  64. super.onCreate(savedInstanceState);
  65. setContentView(R.layout.activity_customer);
  66. myDb = new DatabaseHelper(this);
  67.  
  68. editName = (EditText)findViewById(R.id.firstEntry);
  69. editSurname = (EditText)findViewById(R.id.surnameEntry);
  70. editAddress = (EditText)findViewById(R.id.addressEntry);
  71. editContact = (EditText)findViewById(R.id.contactEntry);
  72. addDataBtn = (Button)findViewById(R.id.addBtn);
  73. AddData();
  74.  
  75. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  76. setSupportActionBar(toolbar);
  77. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
  78.  
  79. mViewPager = (ViewPager) findViewById(R.id.container);
  80. mViewPager.setAdapter(mSectionsPagerAdapter);
  81.  
  82. TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
  83. tabLayout.setupWithViewPager(mViewPager);
  84.  
  85. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  86. fab.setOnClickListener(new View.OnClickListener() {
  87. @Override
  88. public void onClick(View view) {
  89. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  90. .setAction("Action", null).show();
  91. }
  92. });
  93.  
  94. }
  95.  
  96. public void AddData() {
  97. addDataBtn.setOnClickListener(
  98. new View.OnClickListener(){
  99. public void onClick(View v) {
  100. boolean isInserted = myDb.insertData(
  101. editName.getText().toString(),
  102. editSurname.getText().toString(),
  103. editAddress.getText().toString(),
  104. editContact.getText().toString());
  105. if(isInserted =true)
  106. Toast.makeText(Customer.this, "Data Inserted", Toast.LENGTH_LONG).show();
  107. else
  108. Toast.makeText(Customer.this, "Data not Inserted", Toast.LENGTH_LONG).show();
  109. }
  110. }
  111. );
  112. }
  113.  
  114. @Override
  115. public boolean onCreateOptionsMenu(Menu menu) {
  116. // Inflate the menu; this adds items to the action bar if it is present.
  117. getMenuInflater().inflate(R.menu.menu_customer, menu);
  118. return true;
  119. }
  120.  
  121. @Override
  122. public boolean onOptionsItemSelected(MenuItem item) {
  123. // Handle action bar item clicks here. The action bar will
  124. // automatically handle clicks on the Home/Up button, so long
  125. // as you specify a parent activity in AndroidManifest.xml.
  126. int id = item.getItemId();
  127.  
  128. //noinspection SimplifiableIfStatement
  129. if (id == R.id.action_settings) {
  130. return true;
  131. }
  132.  
  133. return super.onOptionsItemSelected(item);
  134. }
  135.  
  136. /**
  137. * A placeholder fragment containing a simple view.
  138. */
  139. public static class PlaceholderFragment extends Fragment {
  140. /**
  141. * The fragment argument representing the section number for this
  142. * fragment.
  143. */
  144. private static final String ARG_SECTION_NUMBER = "section_number";
  145.  
  146. public PlaceholderFragment() {
  147. }
  148.  
  149. /**
  150. * Returns a new instance of this fragment for the given section
  151. * number.
  152. */
  153. public static PlaceholderFragment newInstance(int sectionNumber) {
  154. PlaceholderFragment fragment = new PlaceholderFragment();
  155. Bundle args = new Bundle();
  156. args.putInt(ARG_SECTION_NUMBER, sectionNumber);
  157. fragment.setArguments(args);
  158. return fragment;
  159. }
  160.  
  161. @Override
  162. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  163. Bundle savedInstanceState) {
  164.  
  165. if(getArguments().getInt(ARG_SECTION_NUMBER)==1)
  166. {
  167. View rootView = inflater.inflate(R.layout.fragment_sub_page01, container, false);
  168. return rootView;
  169. }
  170. else if(getArguments().getInt(ARG_SECTION_NUMBER)==2)
  171. {
  172. View rootView = inflater.inflate(R.layout.fragment_sub_page02, container, false);
  173. return rootView;
  174. }
  175. else
  176. {
  177. View rootView = inflater.inflate(R.layout.fragment_customer, container, false);
  178. TextView textView = (TextView) rootView.findViewById(R.id.section_label);
  179. textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
  180. return rootView;
  181. }
  182.  
  183. }
  184. }
  185.  
  186. /**
  187. * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
  188. * one of the sections/tabs/pages.
  189. */
  190. public class SectionsPagerAdapter extends FragmentPagerAdapter {
  191.  
  192. public SectionsPagerAdapter(FragmentManager fm) {
  193. super(fm);
  194. }
  195.  
  196. @Override
  197. public Fragment getItem(int position) {
  198. // getItem is called to instantiate the fragment for the given page.
  199. // Return a PlaceholderFragment (defined as a static inner class below).
  200. return PlaceholderFragment.newInstance(position + 1);
  201. }
  202.  
  203. @Override
  204. public int getCount() {
  205. // Show 3 total pages.
  206. return 2;
  207. }
  208.  
  209. @Override
  210. public CharSequence getPageTitle(int position) {
  211. switch (position) {
  212. case 0:
  213. return "Add Customer";
  214. case 1:
  215. return "View Customer";
  216. }
  217. return null;
  218. }
  219. }
  220. }
  221.  
  222. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  223. xmlns:tools="http://schemas.android.com/tools"
  224. android:layout_width="match_parent"
  225. android:layout_height="match_parent"
  226. tools:context="layout.SubPage01"
  227. android:textAlignment="center">
  228.  
  229. <!-- TODO: Update blank fragment layout -->
  230.  
  231. <EditText
  232. android:layout_width="match_parent"
  233. android:layout_height="wrap_content"
  234. android:inputType="textPersonName"
  235. android:ems="10"
  236. android:id="@+id/firstEntry"
  237. android:hint="First Name"
  238. android:textAlignment="center" />
  239.  
  240. <EditText
  241. android:layout_width="match_parent"
  242. android:layout_height="wrap_content"
  243. android:inputType="textPersonName"
  244. android:ems="10"
  245. android:id="@+id/addressEntry"
  246. android:hint="Address"
  247. android:textAlignment="center"
  248. android:layout_below="@+id/surnameEntry"
  249. android:layout_alignParentStart="true" />
  250.  
  251. <EditText
  252. android:layout_width="match_parent"
  253. android:layout_height="wrap_content"
  254. android:inputType="textPersonName"
  255. android:ems="10"
  256. android:id="@+id/surnameEntry"
  257. android:hint="Surname"
  258. android:textAlignment="center"
  259. android:layout_below="@+id/firstEntry"
  260. android:layout_alignParentStart="true" />
  261.  
  262. <EditText
  263. android:layout_width="match_parent"
  264. android:layout_height="wrap_content"
  265. android:inputType="textPersonName"
  266. android:ems="10"
  267. android:id="@+id/contactEntry"
  268. android:hint="Contact"
  269. android:textAlignment="center"
  270. android:layout_below="@+id/addressEntry"
  271. android:layout_alignParentStart="true" />
  272.  
  273. <Button
  274. android:text="Add Customer"
  275. android:layout_width="match_parent"
  276. android:layout_height="wrap_content"
  277. android:id="@+id/addBtn"
  278. android:layout_marginBottom="81dp"
  279. android:layout_alignParentBottom="true"
  280. android:layout_alignParentStart="true" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement