2016-06-14 8 views
0

フラグメントとプログラムクリックイベントにあるボタンにアクセスするにはどうすればよいですか。私はこれに似た質問があることを知っていますが、私は彼らが与えた解決策を試してみましたが、彼らは私のために働いていませんでした。私はいつも到達不能なエラーを受け取ります。フラグメント上のボタンにはどのようにアクセスできますか?

要件。基本的に私はXMLに2つのボタンを持っています。表示されているボタンをクリックして見えなくしてから、目に見えないボタンを表示させようとしています。

フラグメントは、Androidスタジオのデフォルトのナベチングドロワーアクティビティを編集することで作成されています。 ユニバーサルナビゲーションドロワーを用意しようとしています。デフォルトのクラスを簡単に編集できます。ここ

アクティビティコード

package co.timetrax.drawer; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 

import butterknife.Bind; 
import butterknife.ButterKnife; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    NavigationView navigationView = null; 
    Toolbar toolbar = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //setting fragments 
     MainFragment fragment = new MainFragment(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 

     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 

    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
      MainFragment fragment = new MainFragment(); 
      android.support.v4.app.FragmentTransaction fragmentTransaction = 
        getSupportFragmentManager().beginTransaction(); 
      fragmentTransaction.replace(R.id.fragment_container, fragment); 
      fragmentTransaction.commit(); 
     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

フラグメントコード

package co.timetrax.drawer; 


import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 

import butterknife.Bind; 
import butterknife.ButterKnife; 



/** 
* A simple {@link Fragment} subclass. 
*/ 
public class MainFragment extends Fragment implements View.OnClickListener { 

    public MainFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_main, container, false); 

    } 

    @Override 
    public void onClick(View v) { 

    } 

} 

は、フラグメントのXMLです。

<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" 
    tools:context="co.timetrax.drawer.MainFragment"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="MAIN" 
     android:padding="8dp" 
     android:textColor="#fff" 
     android:background="@color/colorPrimary" 
     android:textSize="28sp" 
     android:id="@+id/main_button" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="second" 
     android:visibility="gone" 
     android:padding="8dp" 
     android:textColor="#fff" 
     android:background="@color/colorPrimary" 
     android:textSize="28sp" 
     android:id="@+id/second_button" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 


</RelativeLayout> 
+0

ボタンにリスナーを設定しておらず、 'onClick'メソッドにコードがないこともあります。 – Rohit5k2

+0

それは私の質問の全体のポイントです。私はリスナーを設定しましたが、彼らはアンドロイドスタジオで赤い鳴っていました。 – mpanga

答えて

0

変更し、これに、この方法:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_main, container, false); 
    Button mainButton = (Button) view.findViewById(R.id.main_button); 
    //proceed to add the listener in a regular way 
    //I will not write that code here; mainButton.setOnClickListener ...... 
    return view; 

} 

、すべてButtonまたはあなたが必要とする任意の他のUI要素のためにこれを行います。

+0

私はすでにこれを試しました。私がmainButton.setOnclickListener(this)と書くときに。 (これ)には赤色の下線が引かれており、そのエラーはその到達不能である。 – mpanga

+0

そして?これはうまくいかなければならない。 – Vucko

+0

それは動作していない、もう一度試してみてください。私はどこかで何か悪いことをしたに違いない。しかし、私はそれが上記の答えとまったく同じように書かれています。 – mpanga

-1

フラグメント内のどこにでもgetView()を呼び出して、ルートビューを取得できます。

View root = getView(); 
Button myButton = (Button) root.findViewById(R.id./*id*/); 
myButton.setVisibility(View.INVISIBLE); 
+0

'getView()'がnullを返す可能性があるので、 'onCreateView'でビューを膨らませるときには、これを行う方がいいです。 – Vucko

+0

確かに、それについて詳しく述べるべきです。さらに、 'findViewById'を呼び出すほうがいいです。しかし、それはできることです。 – Sunshinator

+0

あなたの答えをありがとう。私は実際にこれを試して、Vuckoが言及したエラーを得ました。 – mpanga

関連する問題