2017-12-31 39 views
2

Ich kann nicht herausfinden, warum onClick-Methode nicht aufgerufen wird, wenn ich auf die Schaltfläche klicken. Dasselbe Code-Snippet funktioniert jedoch auch in anderen Aktivitäten einwandfrei. Irgendeine Idee, was vermisse ich?OnClick-Methode wird nicht aufgerufen Schaltfläche Klicken Sie auf

Hier ist die Java-Datei

public class PicMem extends BaseActivity { 

    private String TAG = PicMem.class.getSimpleName(); 
    private static final String endpoint = "https://api.androidhive.info/json/glide.json"; 
    private ArrayList<Image> images; 
    private ProgressBar progressBar; 
    private GalleryAdapter mAdapter; 
    private EmptyRecyclerView recyclerView; 
    private View emptyView; 
    private Button btnRetry; 
    private SwipeRefreshLayout mSwipeRefreshLayout; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(com.dailyzaib.siddhashram.R.layout.activity_pic_mem); 

     setupToolbar(); 
     checkInternetConnection(); 

     recyclerView = (EmptyRecyclerView) findViewById(com.dailyzaib.siddhashram.R.id.recycler_view); 

     progressBar = (ProgressBar) findViewById(R.id.progressBar); 

     images = new ArrayList<>(); 
     mAdapter = new GalleryAdapter(getApplicationContext(), images); 

     RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 2); 
     recyclerView.setLayoutManager(mLayoutManager); 

     emptyView = findViewById(R.id.recycler_view_empty); 
     recyclerView.setEmptyView(emptyView); 

     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     recyclerView.setAdapter(mAdapter); 

     recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(getApplicationContext(), recyclerView, new GalleryAdapter.ClickListener() { 
      @Override 
      public void onClick(View view, int position) { 
       Bundle bundle = new Bundle(); 
       bundle.putSerializable("images", images); 
       bundle.putInt("position", position); 

       FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
       SlideshowDialogFragment newFragment = SlideshowDialogFragment.newInstance(); 
       newFragment.setArguments(bundle); 
       newFragment.show(transaction, "slideshow"); 
      } 

      @Override 
      public void onLongClick(View view, int position) { 

      } 
     })); 

     btnRetry = (Button) findViewById(R.id.btnRetry); 
     btnRetry.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.e(TAG, "Calling onClick"); 
       checkInternetConnection(); 
      } 
     }); 

     mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeContainer); 
     mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
      @Override 
      public void onRefresh() { 
       new Handler().postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         checkInternetConnection(); 
         mSwipeRefreshLayout.setRefreshing(false); 
        } 
       }, 500); 
      } 
     }); 
    } 

    private void setupToolbar() { 

     final ActionBar ab = getActionBarToolbar(); 
     ab.setHomeAsUpIndicator(com.dailyzaib.siddhashram.R.drawable.ic_menu); 
     ab.setDisplayHomeAsUpEnabled(true); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.refresh_images, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       openDrawer(); 
       return true; 

      case R.id.refresh_pid: 
       checkInternetConnection(); 
       break; 

      case R.id.menu_prefs: 
       startActivity(new Intent(this, SettingsPrefActivity.class)); 
       break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    protected int getSelfNavDrawerItem() { 
     return com.dailyzaib.siddhashram.R.id.nav_memories; 
    } 

    @Override 
    public boolean providesActivityToolbar() { 
     return true; 
    } 

    private void fetchImages() { 
     if (progressBar != null) { 
      progressBar.setVisibility(View.VISIBLE); 
     } 

     JsonArrayRequest req = new JsonArrayRequest(endpoint, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         Log.d(TAG, response.toString()); 
         if (progressBar != null) { 
          progressBar.setVisibility(View.GONE); 
         } 

         images.clear(); 
         for (int i = 0; i < response.length(); i++) { 
          try { 
           JSONObject object = response.getJSONObject(i); 
           Image image = new Image(); 
           image.setName(object.getString("name")); 

           JSONObject url = object.getJSONObject("url"); 
           image.setSmall(url.getString("small")); 
           image.setMedium(url.getString("medium")); 
           image.setLarge(url.getString("large")); 
           image.setTimestamp(object.getString("timestamp")); 

           images.add(image); 

          } catch (JSONException e) { 
           Log.e(TAG, "Json parsing error: " + e.getMessage()); 
          } 
         } 

         mAdapter.notifyDataSetChanged(); 

        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e(TAG, "Error: " + error.getMessage()); 
       if (progressBar != null) { 
        progressBar.setVisibility(View.GONE); 
       } 
      } 
     }); 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(req); 
    } 

    //////// Check Internet Connection //////////////////// 

    private void checkInternetConnection(){ 

     new InternetCheckAsyncTask(this, new InternetCheckAsyncTask.InternetConsumer() { 

      @Override 
      public void internetStatusResult(Boolean internet) { 
       showSnack(internet); 
      } 
     }); 
    } 

    private void showSnack(boolean isConnected) { 

     if (!isConnected) { 

      emptyView.setVisibility(View.VISIBLE); 

      Snackbar snackbar = Snackbar.make(findViewById(R.id.drawer_layout), "No Active Internet Connection", Snackbar.LENGTH_LONG) 
        .setAction("RETRY", new View.OnClickListener() { 
         @Override 
         public void onClick(View view) { 
          checkInternetConnection(); 
         } 
        }); 

      View snackbarLayout = snackbar.getView(); 

      // SnackBar Message Text color 
      TextView textView = (TextView) snackbarLayout.findViewById(android.support.design.R.id.snackbar_text); 
      textView.setTextColor(Color.YELLOW); 

      // Changing Action Button Text Color 
      snackbar.setActionTextColor(Color.WHITE); 

      snackbar.show(); 
     }else{ 

      emptyView.setVisibility(View.INVISIBLE); 
      fetchImages(); 
     } 
    } 

    //////// End Of Check Internet Connection //////////////////// 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     checkInternetConnection(); 
    } 
} 

Hier ist das XML-Layout für die obige Aktivität

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/theme_primary_dark" 
     android:orientation="vertical"> 

     <include layout="@layout/include_toolbar" /> 

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

      <include layout="@layout/include_nointernet" /> 

      <include layout="@layout/include_progress_layout" /> 

      <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/swipeContainer" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <com.dailyzaib.siddhashram.extra.EmptyRecyclerView 
        android:id="@+id/recycler_view" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:scrollbars="vertical" /> 

      </android.support.v4.widget.SwipeRefreshLayout> 
     </RelativeLayout> 

    </LinearLayout> 

    <include layout="@layout/include_navigation" /> 
</android.support.v4.widget.DrawerLayout> 

schließlich das Layout für Taste (btnRetry), die nicht immer genannt wird:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/recycler_view_empty" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:visibility="visible"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     app:srcCompat="@drawable/ic_depress" /> 

    <TextView 
     android:id="@+id/tvNoConnection" 
     style="@style/TextAppearance.AppCompat.Title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/imageView" 
     android:layout_centerHorizontal="true" 
     android:layout_margin="10dp" 
     android:text="@string/no_internet_connection" 
     android:textColor="@color/cardview_light_background" /> 

    <TextView 
     android:id="@+id/tvNoConnectionDesc" 
     style="@style/TextAppearance.AppCompat.Small" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tvNoConnection" 
     android:layout_centerHorizontal="true" 
     android:layout_margin="10dp" 
     android:text="@string/no_internet_description" 
     android:textColor="@color/cardview_light_background" /> 

    <Button 
     android:id="@+id/btnRetry" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tvNoConnectionDesc" 
     android:layout_centerHorizontal="true" 
     android:layout_margin="10dp" 
     android:background="@drawable/button_background" 
     android:paddingLeft="30dp" 
     android:paddingRight="30dp" 
     android:text="@string/no_internet_try_again" 
     android:textColor="@color/cardview_light_background" /> 

</RelativeLayout> 

+0

posten Sie Ihre Log hier –

+0

@AGMTazim es kein Protokoll ist. Es heißt nicht – Zoe

+0

Was ist die 'checkInternetConnection()' Methode? Was beinhaltet? –

Antwort

1

Versuchen Sie, die Position von

<include layout="@layout/include_nointernet" /> 

innerhalb SwipeRefreshLayout zu ändern, wie unten

gezeigt
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/swipeContainer" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <include layout="@layout/include_nointernet" /> 

       <com.dailyzaib.siddhashram.extra.EmptyRecyclerView 
        android:id="@+id/recycler_view" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:scrollbars="vertical" /> 

      </android.support.v4.widget.SwipeRefreshLayout> 
+0

Warten. Lass mich das überprüfen. – Rauhayl

+0

Whoa .. Funktioniert wie ein Charme. Vielen Dank @Mariam – Rauhayl

+0

@Rauhayl Mein Vergnügen. –

0

Ändern Sie Ihre layout_visibility="visible" oder entfernen Sie dieses Attribut. Folge ihm -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/recycler_view_empty" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:visibility="visible"> 
</RelativeLayout> 

Lass mich wissen, was passiert.

+0

Toast wird auch nicht gefeuert. – Rauhayl

+1

Können Sie Ihren XML-Code hier teilen? –

+0

Ändern Sie Ihre 'layout_visibility =" visible "' oder entfernen Sie dieses Attribut in 'no_internet.xml' Layoutdatei. Überprüfen Sie meine aktualisierte Antwort. –

Verwandte Themen