Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.22 KB | None | 0 0
  1. recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
  2. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  3.  
  4. 06-22 19:52:25.801 19743-19743/com.transvision.bertho.transvisiondashboardapp E/AndroidRuntime: FATAL EXCEPTION: main
  5. Process: com.transvision.bertho.transvisiondashboardapp, PID: 19743
  6. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.transvision.bertho.transvisiondashboardapp/com.transvision.bertho.transvisiondashboardapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
  7. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
  8. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
  9. at android.app.ActivityThread.-wrap11(ActivityThread.java)
  10. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
  11. at android.os.Handler.dispatchMessage(Handler.java:102)
  12. at android.os.Looper.loop(Looper.java:148)
  13. at android.app.ActivityThread.main(ActivityThread.java:5417)
  14. at java.lang.reflect.Method.invoke(Native Method)
  15. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  16. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  17. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
  18. at com.transvision.bertho.transvisiondashboardapp.MainActivity.onCreate(MainActivity.java:53)
  19. at android.app.Activity.performCreate(Activity.java:6237)
  20. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
  21. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
  22. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
  23. at android.app.ActivityThread.-wrap11(ActivityThread.java) 
  24. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
  25. at android.os.Handler.dispatchMessage(Handler.java:102) 
  26. at android.os.Looper.loop(Looper.java:148) 
  27. at android.app.ActivityThread.main(ActivityThread.java:5417) 
  28. at java.lang.reflect.Method.invoke(Native Method) 
  29. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
  30. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  31.  
  32. package adapter;
  33.  
  34. import android.content.Context;
  35. import android.support.v7.widget.RecyclerView;
  36. import android.view.LayoutInflater;
  37. import android.view.View;
  38. import android.view.ViewGroup;
  39. import android.widget.ImageView;
  40. import android.widget.LinearLayout;
  41. import android.widget.TextView;
  42.  
  43. import com.transvision.bertho.transvisiondashboardapp.R;
  44.  
  45. import java.util.List;
  46.  
  47. import model.Channel;
  48.  
  49.  
  50. public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.ChannelViewHolder> {
  51.  
  52. private List<Channel> channels;
  53. private int rowLayout;
  54. private Context context;
  55.  
  56. public static class ChannelViewHolder extends RecyclerView.ViewHolder {
  57.  
  58. LinearLayout moviesLayout;
  59. TextView movieTitle;
  60. TextView data;
  61. TextView movieDescription;
  62. TextView rating;
  63.  
  64. TextView name;
  65. TextView code;
  66. TextView description;
  67. TextView number;
  68. TextView definition;
  69. TextView paket;
  70. ImageView logo;
  71.  
  72. public ChannelViewHolder(View v) {
  73. super(v);
  74. moviesLayout = (LinearLayout) v.findViewById(R.id.movies_layout);
  75. name = (TextView) v.findViewById(R.id.title);
  76. definition = (TextView) v.findViewById(R.id.subtitle);
  77. description = (TextView) v.findViewById(R.id.description);
  78. // rating = (TextView) v.findViewById(R.id.rating);
  79. }
  80. }
  81.  
  82. public ChannelAdapter(List<Channel> channels, int rowLayout, Context context) {
  83. this.channels = channels;
  84. this.rowLayout = rowLayout;
  85. this.context = context;
  86. }
  87.  
  88. @Override
  89. public ChannelAdapter.ChannelViewHolder onCreateViewHolder(ViewGroup parent,
  90. int viewType) {
  91. View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
  92. return new ChannelViewHolder(view);
  93. }
  94.  
  95. @Override
  96. public void onBindViewHolder(ChannelViewHolder holder, final int position) {
  97. holder.name.setText(channels.get(position).getName());
  98. holder.definition.setText(channels.get(position).getDefinition());
  99. holder.description.setText(channels.get(position).getDescription());
  100. // holder.rating.setText(channels.get(position).getVoteAverage().toString());
  101. }
  102.  
  103. @Override
  104. public int getItemCount() {
  105. return channels.size();
  106. }
  107.  
  108. }
  109.  
  110. <?xml version="1.0" encoding="utf-8"?>
  111. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  112. xmlns:app="http://schemas.android.com/apk/res-auto"
  113. xmlns:tools="http://schemas.android.com/tools"
  114. android:id="@+id/drawer_layout"
  115. android:layout_width="match_parent"
  116. android:layout_height="match_parent"
  117. android:fitsSystemWindows="true"
  118. tools:openDrawer="start">
  119.  
  120. <include
  121. layout="@layout/app_bar_main"
  122. android:layout_width="match_parent"
  123. android:layout_height="match_parent" />
  124.  
  125. <android.support.design.widget.NavigationView
  126. android:id="@+id/nav_view"
  127. android:layout_width="wrap_content"
  128. android:layout_height="match_parent"
  129. android:layout_gravity="start"
  130. android:fitsSystemWindows="true"
  131. app:headerLayout="@layout/nav_header_main"
  132. app:menu="@menu/activity_main_drawer" />
  133.  
  134. </android.support.v4.widget.DrawerLayout>
  135.  
  136. <?xml version="1.0" encoding="utf-8"?>
  137. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  138. xmlns:app="http://schemas.android.com/apk/res-auto"
  139. xmlns:tools="http://schemas.android.com/tools"
  140. android:layout_width="match_parent"
  141. android:layout_height="match_parent"
  142. android:fitsSystemWindows="true"
  143. tools:context="com.transvision.bertho.transvisiondashboardapp.MainActivity">
  144.  
  145. <android.support.design.widget.AppBarLayout
  146. android:layout_width="match_parent"
  147. android:layout_height="wrap_content"
  148. android:theme="@style/AppTheme.AppBarOverlay">
  149.  
  150. <android.support.v7.widget.Toolbar
  151. android:id="@+id/toolbar"
  152. android:layout_width="match_parent"
  153. android:layout_height="?attr/actionBarSize"
  154. android:background="?attr/colorPrimary"
  155. app:popupTheme="@style/AppTheme.PopupOverlay" />
  156.  
  157. </android.support.design.widget.AppBarLayout>
  158.  
  159. <include layout="@layout/main" />
  160.  
  161. <android.support.design.widget.FloatingActionButton
  162. android:id="@+id/fab"
  163. android:layout_width="wrap_content"
  164. android:layout_height="wrap_content"
  165. android:layout_gravity="bottom|end"
  166. android:layout_margin="@dimen/fab_margin"
  167. android:src="@android:drawable/ic_dialog_email" />
  168.  
  169. </android.support.design.widget.CoordinatorLayout>
  170.  
  171. <?xml version="1.0" encoding="utf-8"?>
  172. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  173. android:orientation="vertical"
  174. android:layout_width="match_parent"
  175. android:layout_height="match_parent">
  176.  
  177. <FrameLayout
  178. android:id="@+id/root"
  179. android:layout_width="match_parent"
  180. android:layout_height="match_parent" />
  181.  
  182. </LinearLayout>
  183.  
  184. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  185. xmlns:tools="http://schemas.android.com/tools"
  186. android:layout_width="match_parent"
  187. android:layout_height="match_parent"
  188. android:orientation="vertical"
  189. tools:context="com.transvision.bertho.transvisiondashboardapp.MainActivity">
  190.  
  191. <android.support.v7.widget.RecyclerView
  192. android:id="@+id/movies_recycler_view"
  193. android:layout_width="match_parent"
  194. android:layout_height="match_parent"
  195. android:scrollbars="vertical" />
  196.  
  197. </RelativeLayout>
  198.  
  199. package com.transvision.bertho.transvisiondashboardapp;
  200.  
  201. import android.os.Bundle;
  202. import android.support.design.widget.FloatingActionButton;
  203. import android.support.design.widget.NavigationView;
  204. import android.support.design.widget.Snackbar;
  205. import android.support.v4.app.Fragment;
  206. import android.support.v4.app.FragmentManager;
  207. import android.support.v4.app.FragmentTransaction;
  208. import android.support.v4.view.GravityCompat;
  209. import android.support.v4.widget.DrawerLayout;
  210. import android.support.v7.app.ActionBarDrawerToggle;
  211. import android.support.v7.app.AppCompatActivity;
  212. import android.support.v7.widget.LinearLayoutManager;
  213. import android.support.v7.widget.RecyclerView;
  214. import android.support.v7.widget.Toolbar;
  215. import android.util.Log;
  216. import android.view.Menu;
  217. import android.view.MenuItem;
  218. import android.view.View;
  219. import android.widget.Toast;
  220.  
  221. import java.util.List;
  222.  
  223. import adapter.ChannelAdapter;
  224. import model.Channel;
  225. import model.ChannelResponse;
  226. import model.Movie;
  227. import model.MoviesResponse;
  228. import page.DefaultFragment;
  229. import page.HomeFragment;
  230. import page.ProfileFragment;
  231. import rest.ApiClient;
  232. import rest.ApiInterface;
  233. import retrofit2.Call;
  234. import retrofit2.Callback;
  235. import retrofit2.Response;
  236.  
  237. public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
  238.  
  239. private static final String TAG = MainActivity.class.getSimpleName();
  240.  
  241. private final static String API_KEY = "7e8f60e325cd06e164799af1e317d7a7";
  242.  
  243. private RecyclerView recyclerView;
  244.  
  245. @Override
  246. protected void onCreate(Bundle savedInstanceState) {
  247. super.onCreate(savedInstanceState);
  248. setContentView(R.layout.activity_main);
  249.  
  250. recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
  251. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  252.  
  253. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  254. setSupportActionBar(toolbar);
  255.  
  256. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  257. fab.setOnClickListener(new View.OnClickListener() {
  258. @Override
  259. public void onClick(View view) {
  260. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  261. .setAction("Action", null).show();
  262. }
  263. });
  264.  
  265. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  266. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  267. this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  268. drawer.setDrawerListener(toggle);
  269. toggle.syncState();
  270.  
  271. NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  272. navigationView.setNavigationItemSelectedListener(this);
  273.  
  274. Fragment fragment = null;
  275. fragment = new DefaultFragment();
  276. FragmentManager fragmentManager = getSupportFragmentManager();
  277. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  278. fragmentTransaction.replace(R.id.root, fragment);
  279. fragmentTransaction.commit();
  280.  
  281. }
  282.  
  283. @Override
  284. public void onBackPressed() {
  285. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  286. if (drawer.isDrawerOpen(GravityCompat.START)) {
  287. drawer.closeDrawer(GravityCompat.START);
  288. } else {
  289. super.onBackPressed();
  290. }
  291. }
  292.  
  293. @Override
  294. public boolean onCreateOptionsMenu(Menu menu) {
  295. // Inflate the menu; this adds items to the action bar if it is present.
  296. getMenuInflater().inflate(R.menu.main, menu);
  297. return true;
  298. }
  299.  
  300. @Override
  301. public boolean onOptionsItemSelected(MenuItem item) {
  302. // Handle action bar item clicks here. The action bar will
  303. // automatically handle clicks on the Home/Up button, so long
  304. // as you specify a parent activity in AndroidManifest.xml.
  305. int id = item.getItemId();
  306.  
  307. //noinspection SimplifiableIfStatement
  308. if (id == R.id.action_settings) {
  309. return true;
  310. }
  311.  
  312. return super.onOptionsItemSelected(item);
  313. }
  314.  
  315. @SuppressWarnings("StatementWithEmptyBody")
  316. @Override
  317. public boolean onNavigationItemSelected(MenuItem item) {
  318. // Handle navigation view item clicks here.
  319. int id = item.getItemId();
  320.  
  321. Fragment fragment = null;
  322. String title = getString(R.string.app_name);
  323.  
  324. if (id == R.id.nav_camera) {
  325. fragment = new HomeFragment();
  326. title = "Home";
  327. getChannelData();
  328. } else if (id == R.id.nav_gallery) {
  329. fragment = new ProfileFragment();
  330. title = "Profile";
  331. } else if (id == R.id.nav_slideshow) {
  332.  
  333. } else if (id == R.id.nav_manage) {
  334.  
  335. } else if (id == R.id.nav_share) {
  336.  
  337. } else if (id == R.id.nav_send) {
  338.  
  339. }
  340.  
  341. if (fragment != null) {
  342. FragmentManager fragmentManager = getSupportFragmentManager();
  343. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  344. fragmentTransaction.replace(R.id.root, fragment);
  345. fragmentTransaction.commit();
  346.  
  347. // set the toolbar title
  348. getSupportActionBar().setTitle(title);
  349. }
  350.  
  351. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  352. drawer.closeDrawer(GravityCompat.START);
  353. return true;
  354. }
  355.  
  356. public void getChannelData() {
  357. ApiInterface apiService = ApiClient.getChannel().create(ApiInterface.class);
  358. Call<ChannelResponse> call = apiService.getItems();
  359.  
  360. call.enqueue(new Callback<ChannelResponse>() {
  361. @Override
  362. public void onResponse(Call<ChannelResponse> call, Response<ChannelResponse> response) {
  363. int statusCode = response.code();
  364. List<Channel> channel = response.body().getItems();
  365. Log.d(TAG, "NUMBER OF MOVIES RECEIVED : " + channel.size());
  366. recyclerView.setAdapter(new ChannelAdapter(channel, R.layout.list_channel, getApplicationContext()));
  367. }
  368.  
  369. @Override
  370. public void onFailure(Call<ChannelResponse> call, Throwable t) {
  371. // Log error here since request failed
  372. Log.e(TAG, t.toString());
  373. }
  374. });
  375. }
  376.  
  377.  
  378. public void showToast(String output){
  379. Toast.makeText(this.getBaseContext(), output, Toast.LENGTH_SHORT).show();
  380. }
  381. }
  382.  
  383. Fragment fragment = null;
  384. fragment = new DefaultFragment();
  385. FragmentManager fragmentManager = getSupportFragmentManager();
  386. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  387. fragmentTransaction.replace(R.id.root, fragment);
  388. fragmentTransaction.commit();
  389.  
  390. recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
  391. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  392.  
  393. recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
  394. // The above line doesnot throw null pointer exception, since you can cast null to any object. Refer this post - http://stackoverflow.com/questions/18723596/no-exception-while-type-casting-with-a-null-in-java
  395. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  396. // The above line will throw null pointer exception, since here we are accessing the null object and invoking a method on that null object.
  397.  
  398. recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
  399. if(recyclerView != null) {
  400. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  401. .....
  402. .....
  403. .....
  404. }
  405.  
  406. <?xml version="1.0" encoding="utf-8"?>
  407. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  408. android:orientation="vertical"
  409. android:layout_width="match_parent"
  410. android:layout_height="match_parent">
  411.  
  412. <FrameLayout
  413. android:id="@+id/root"
  414. android:layout_width="match_parent"
  415. android:layout_height="match_parent" >
  416.  
  417. <android.support.v7.widget.RecyclerView
  418. android:id="@+id/movies_recycler_view"
  419. android:layout_width="match_parent"
  420. android:layout_height="match_parent"
  421. android:scrollbars="vertical" />
  422.  
  423. </FrameLayout>
  424.  
  425. </LinearLayout>
  426.  
  427. recyclerView = (RecyclerView) rootview.findViewById(R.id.movies_recycler_view);
  428. recyclerView.setLayoutManager(new LinearLayoutManager(this));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement