2016-09-11 4 views
0

android:onClick = "clickFun1"Button私のレイアウトxmlファイルでは、DipToVol extends FragmentクラスのclickFun1()メソッドが認識されませんでした。 Androidスタジオでは、その方法は使用されていないと言いますandroid:onClick = "functionName"は、サブクラス内の関数名を認識しませんでした

public class DipToVol extends Fragment { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.fragment_one, container, false); 
    } 

    public double calVol(short rad, short len, int height) { 
     double area; 
     area = (Math.PI * Math.pow(rad, 2)/2) - Math.pow(rad, 2) * Math.asin(1 - (height/(double)rad)) - (rad - height) * Math.sqrt(height * (2 * rad - height)); 
     return area*len*0.000001; 
    } 

    //here android studio says this method never used 
    public void clickFun1 (View view) 
    { 
     int dipHSD = 0, dipMS = 0; 
     final short radHSD = 1219; 
     final short lenHSD = 6810; 
     final short radMS = 999; 
     final short lenMS = 6804; 
     double volHSD, volMS; 

     EditText dipH = (EditText) view.findViewById(R.id.dipHSD1); 
     EditText dipM = (EditText) view.findViewById(R.id.dipMS1); 

     TextView volH = (TextView) view.findViewById(R.id.volHSD1); 
     TextView volM = (TextView) view.findViewById(R.id.volMS1); 
     try { 
      dipHSD = Integer.parseInt(dipH.getText().toString()); 
      volHSD = calVol(radHSD, lenHSD, dipHSD); 
      volHSD = Math.round(volHSD); 
      volH.setText(volHSD + ""); 
     } 
     catch (NumberFormatException e) { 
      volH.setText(""); 
     } 
     try{ dipMS = Integer.parseInt(dipM.getText().toString()); 
      volMS = calVol(radMS, lenMS, dipMS); 
      volMS = Math.round(volMS); 
      volM.setText(volMS + ""); 
     }catch(NumberFormatException e){volM.setText("");} 

    } 

    //here android studio says this method never used 
    public void clearFun1(View view){ 
     EditText dipH = (EditText) view.findViewById(R.id.dipHSD1); 
     EditText dipM = (EditText) view.findViewById(R.id.dipMS1); 

     TextView volH = (TextView) view.findViewById(R.id.volHSD1); 
     TextView volM = (TextView) view.findViewById(R.id.volMS1); 

     volH.setText(""); 
     volM.setText(""); 
     dipH.setText(""); 
     dipM.setText(""); 
    } 

} 

fragment_one.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/section_label1" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context=".MainActivity" 
    android:background="#4ab4b2"> 


     <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/contentView" 
     android:layout_weight ="1" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
      android:layout_alignParentBottom="false" 
      android:layout_alignParentRight="false" 
      android:layout_alignWithParentIfMissing="false"> 

     <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginTop="10dp"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="HSD Dip" 
       android:id="@+id/lableDipHSD" 
       android:layout_column="0" 
       android:textSize="30sp" 
       android:textStyle="bold" 
       android:textColor="#ffffff" 
       android:layout_marginLeft="5dp" /> 

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:inputType="number" 
       android:ems="10" 
       android:id="@+id/dipHSD1" 
       android:textSize="30sp" 
       android:textStyle="bold" 
       android:layout_column="1" 
       android:textColor="#ffffff" 
       /> 
     </TableRow> 

     <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginTop="10dp"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="MS Dip" 
       android:id="@+id/lableDipMS" 
       android:layout_column="0" 
       android:textSize="30sp" 
       android:textStyle="bold" 
       android:textColor="#ffffff" 
       android:layout_marginLeft="5dp" /> 

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:inputType="number" 
       android:id="@+id/dipMS1" 
       android:layout_column="1" 
       android:textStyle="bold" 
       android:textSize="30sp" 
       android:textColor="#ffffff" 
       /> 
     </TableRow> 

     <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginTop="10dp"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="HSD Volume" 
       android:id="@+id/lableVolHSD" 
       android:layout_column="0" 
       android:textStyle="bold" 
       android:textSize="30sp" 
       android:paddingTop="15dp" 
       android:textColor="#ffffff" 
       android:layout_marginLeft="5dp" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/volHSD1" 
       android:layout_column="1" 
       android:textStyle="bold" 
       android:textSize="30sp" 
       android:textColor="#ffffff" /> 
     </TableRow> 

     <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginTop="10dp"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="MS Volume" 
       android:id="@+id/lableVolMS" 
       android:layout_column="0" 
       android:textStyle="bold" 
       android:textSize="30sp" 
       android:paddingTop="15dp" 
       android:textColor="#ffffff" 
       android:layout_marginLeft="5dp" 
       android:paddingBottom="10dp" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/volMS1" 
       android:layout_column="1" 
       android:textStyle="bold" 
       android:textSize="30sp" 
       android:textColor="#ffffff" 
       android:paddingBottom="10dp" /> 
     </TableRow> 

    </TableLayout> 




    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/clearBtn" 
     android:id="@+id/clearBtn1" 
     android:textSize="25sp" 
     android:clickable="true" 
     android:onClick="clearFun1" 
     android:background="#fa0690" 
     android:padding="8dp" 
     android:paddingLeft="8dp" 
     android:paddingTop="2dp" 
     android:paddingRight="8dp" 
     android:paddingBottom="2dp" 
     android:textAlignment="center" 
     android:textStyle="bold" 
     android:layout_column="0" 
     android:layout_below="@+id/contentView" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="40dp" 
     android:layout_toStartOf="@+id/calcBtn1" 
     android:layout_toLeftOf="@+id/calcBtn1" 
     android:alpha="0.8" 
     android:layout_marginRight="10dp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/calcBtn" 
     android:id="@+id/calcBtn1" 
     android:textSize="25sp" 
     android:clickable="true" 
     android:onClick="clickFun1" 
     android:background="#fa0690" 
     android:padding="8dp" 
     android:paddingLeft="8dp" 
     android:paddingTop="2dp" 
     android:paddingRight="8dp" 
     android:paddingBottom="2dp" 
     android:textAlignment="center" 
     android:textStyle="bold" 
     android:layout_column="0" 
     android:layout_alignTop="@+id/clearBtn1" 
     android:layout_alignRight="@+id/contentView" 
     android:layout_alignEnd="@+id/contentView" 
     android:alpha="0.8" /> 

</RelativeLayout> 

私はそれが間違ってやっているところを教えてください。私がコードを実行すると、アプリケーションがクラッシュしています。エラーログにclickFun1()が見つかりません。

// logcat

09-10 13:49:24.823 2718-2718/com.example.siv.mahalaxmipetroleums E/AndroidRuntime: FATAL EXCEPTION: main 
                        java.lang.IllegalStateException: Could not find method clickFun1(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'calcBtn' 
                         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) 
                         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
                         at android.view.View.performClick(View.java:4240) 
                         at android.view.View$PerformClick.run(View.java:17721) 
                         at android.os.Handler.handleCallback(Handler.java:730) 
                         at android.os.Handler.dispatchMessage(Handler.java:92) 
                         at android.os.Looper.loop(Looper.java:137) 
                         at android.app.ActivityThread.main(ActivityThread.java:5103) 
                         at java.lang.reflect.Method.invokeNative(Native Method) 
                         at java.lang.reflect.Method.invoke(Method.java:525) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                         at dalvik.system.NativeStart.main(Native Method) 

次のように私はmainActivity.javaを変更しました。 mainActivity.java

package com.example.siv.mahalaxmipetroleums; 

import android.support.design.widget.TabLayout; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link FragmentPagerAdapter} derivative, which will keep every 
    * loaded fragment in memory. If this becomes too memory intensive, it 
    * may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    private SectionsPagerAdapter mSectionsPagerAdapter; 


    Button clearBtn_f1,calcBtn_f1,clearBtn_f2,calcBtn_f2; 



    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    private ViewPager mViewPager; 

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

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the activity. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(mViewPager); 

     clearBtn_f1 = (Button) findViewById(R.id.clearBtn1); 
     clearBtn_f2 = (Button) findViewById(R.id.clearBtn2); 
     calcBtn_f1 = (Button) findViewById(R.id.calcBtn1); 
     calcBtn_f2 = (Button) findViewById(R.id.calcBtn2); 

     clearBtn_f1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       clearFun1(v); 
      } 
     }); 
     clearBtn_f2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       clearFun2(v); 
      } 
     }); 

     calcBtn_f1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       clickFun1(v); 
      } 
     }); 

     calcBtn_f2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       clickFun2(v); 
      } 
     }); 


     } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.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); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     // */ 
     private static final String ARG_SECTION_NUMBER = "section_number"; 

     public PlaceholderFragment() { 
     } 

     /** 
     * Returns a new instance of this fragment for the given section 
     * number. 
     */ 
     public static PlaceholderFragment newInstance(int sectionNumber) { 
      PlaceholderFragment fragment = new PlaceholderFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     /* @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_one, container, false); 
      TextView textView = (TextView) rootView.findViewById(R.id.section_label); 
      textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); 
      return rootView; 
     }*/ 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 

      // getItem is called to instantiate the fragment for the given page. 
      // Return a PlaceholderFragment (defined as a static inner class below). 

      if (position == 0) { 
       return new Fragment_One(); 
      } else if (position == 1) { 
       return new Fragment_Two(); 
      } else 
       return new Fragment_Three();//PlaceholderFragment.newInstance(position + 1); 

     } 
     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      switch (position) { 
       case 0: 
        return "Dip-Vol"; 
       case 1: 
        return "Vol-Dip"; 
       case 2: 
        return "Temp-Dens"; 
      } 
      return null; 
     } 
    } 

    public void clickFun1 (View view) 
    { 
     short dipHSD = 0, dipMS = 0; 
     final short radHSD = 1219; 
     final short lenHSD = 6810; 
     final short radMS = 999; 
     final short lenMS = 6804; 
     double volHSD, volMS; 

     EditText dipH = (EditText) view.findViewById(R.id.dipHSD1); 
     EditText dipM = (EditText) view.findViewById(R.id.dipMS1); 

     TextView volH = (TextView) view.findViewById(R.id.volHSD1); 
     TextView volM = (TextView) view.findViewById(R.id.volMS1); 
     try { 
      dipHSD = Short.parseShort(dipH.getText().toString()); 
      Liquid_Volume dtvh = new Liquid_Volume(radHSD,lenHSD,dipHSD); 
      volHSD = dtvh.getVolume(); 
      volHSD = Math.round(volHSD); 
      volH.setText(volHSD + ""); 
     } 
     catch (NumberFormatException e) { 
      volH.setText(""); 
     } 
     try{ dipMS = Short.parseShort(dipM.getText().toString()); 
      Liquid_Volume dtvm = new Liquid_Volume(radMS,lenMS,dipMS); 
      volMS = dtvm.getVolume(); 
      volM.setText(volMS + ""); 
     }catch(NumberFormatException e){volM.setText("");} 

    } 

    public void clearFun1(View view){ 
     EditText dipH = (EditText) view.findViewById(R.id.dipHSD1); 
     EditText dipM = (EditText) view.findViewById(R.id.dipMS1); 

     TextView volH = (TextView) view.findViewById(R.id.volHSD1); 
     TextView volM = (TextView) view.findViewById(R.id.volMS1); 

     volH.setText(""); 
     volM.setText(""); 
     dipH.setText(""); 
     dipM.setText(""); 
    } 

    public void clickFun2 (View view) 
    { 
     EditText volH = (EditText) view.findViewById(R.id.volHSD2); 
     EditText volM = (EditText) view.findViewById(R.id.volMS2); 

     TextView dipH = (TextView) view.findViewById(R.id.dipHSD2); 
     TextView dipM = (TextView) view.findViewById(R.id.dipMS2); 

     double volHSD = 0, volMS = 0; 
     final double radHSD = 121.9; 
     final double lenHSD = 681.0; 
     final double radMS = 99.9; 
     final double lenMS = 680.4; 
     double dipHSD, dipMS; 

     try { 
      volHSD = Double.parseDouble(volH.getText().toString()); 
      Liquid_Height HSD = new Liquid_Height(radHSD,lenHSD,volHSD); 
      dipHSD = HSD.getDip(); 
      //String.format("%f",dipHSD); 
      //dipH.setText(Double.toString(dipHSD)); 
      dipH.setText(dipHSD + ""); 
     } 
     catch (NumberFormatException e) { 
      dipH.setText(""); 
     } 
     try{ volMS = Double.parseDouble(volM.getText().toString()); 
      Liquid_Height MS = new Liquid_Height(radMS,lenMS,volMS); 
      dipMS = MS.getDip(); 
      //String.format("%f",dipHSD); 
      //dipM.setText(String.format("%f",dipMS)); 
      dipM.setText(dipMS + ""); 
     } 
     catch(NumberFormatException e){ 
      dipM.setText(""); 
     } 

    } 

    public void clearFun2(View view){ 
     EditText volH = (EditText) view.findViewById(R.id.volHSD2); 
     EditText volM = (EditText) view.findViewById(R.id.volMS2); 

     TextView dipH = (TextView) view.findViewById(R.id.dipHSD2); 
     TextView dipM = (TextView) view.findViewById(R.id.dipMS2); 

     volH.setText(""); 
     volM.setText(""); 
     dipH.setText(""); 
     dipM.setText(""); 
    } 

} 

にフラグメントサブクラスからコードを移動それから私は私のfragment_one.javaを持っていると同じように他のフラグメントを休みます。

public class Fragment_One extends Fragment { 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.fragment_one, container, false); 

    } 

} 

ここで、アプリケーションを開くとすぐにクラッシュします。

Logcatには、以下の

09-12 07:17:40.600 2283-2291/com.google.android.gms E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. 
                    java.lang.Throwable: Explicit termination method 'close' not called 
                     at dalvik.system.CloseGuard.open(CloseGuard.java:184) 
                     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:190) 
                     at ... 
+0

フラグメントを含むアクティビティで 'clickFun1()'メソッドを移動するとどうなりますか? –

+0

@Rotwang私はmainFunction()メソッドをmainActivity.javaに移動しました。私のDipToVolクラスは、onCreateViewのためだけにフラグメントレイアウトファイルを膨張させました。ボタンをクリックするとアプリがクラッシュする –

+0

あなたが 'app crashing'と言うときは、少なくともあなたのlogcatを投稿するべきです。 –

答えて

1

厥ないエラーを言います。 (メソッドの警告ではなく、アプリケーションのクラッシュ)。 Javaコンパイラは、XMLでリスナーを定義したことを知らないため、これらのメソッドを明示的に呼び出すことはないので、単に「使用されていない」と考えるだけです。

個人的に、私は常に私のビューのsetOnClickListenerをJavaコード

+0

これを実行すると、アプリケーションがレイアウトを表示していますが、clickFun1またはclearFun1のいずれかをクリックすると、アプリがクラッシュしています。 LogCampはclickFun1関数が親や祖先などで見つからなかったと言っています。OnCreateView内のボタンでsetOnClickListenerを使用すると、アプリケーションは開いた直後にクラッシュします。 –

+0

@SivaPrasad: "OnCreateView内のボタンでsetOnClickListenerを使用したとき、アプリケーションが開いた直後にクラッシュする" - 別のスタックオーバーフローの質問をしてください。*このコードは*および*このJavaスタックトレース*を提供します。 – CommonsWare

+0

@CommonsWare前回同様の質問を投稿しましたが、アンドロイドスタジオはこのメソッドが使用されていないことを警告していませんでした。しかし、今日、私はいくつかのコードを編集し、それが動作しないことがわかったとき、私は初期のコードに来て、今回はそれについて警告します。 –

1

Androidが実行され、onClickメソッドがチェックされているときは、メインのアクティビティクラスのみを調べます。 できませんフラグメントにonClickメソッドを配置します。断片は活動と同じではないため、虐待されるべきではありません。他のユーザーが言ったようにhttps://stackoverflow.com/a/39429157/6754053

+0

あなたはリンク先の投稿がこのユーザーと同じユーザーによって尋ねられたことを認識していますか? –

+0

@ cricket_007私はコモンズウェアが提案したものを試しました。私は最初にonClickListenerを使って最初のものを試しました。しかし、アプリはすぐにクラッシュします。次に、私は2番目の提案を試み、XMLレイアウトでonClickを維持しました。しかし、前の質問で引用したのと同じように起こっています。つまり、いずれかのボタンをクリックするとクラッシュします。それから、すべてのコードをmainActivity.javaに移動し、ボタン上でonClickListenersを使用してみました。今回は、レイアウトを表示する前にすぐに再びクラッシュします。 –

+0

@ cricket_007これらは重複した質問です。それを指摘してくれてありがとう。 –

0

もう一つの問題は、ここで同じ問題を抱えています。 FragmentsにonClick属性を使用することはできません。しかし、この宇宙にはJake WhartonとButterknifeを含む彼のすばらしい図書館があります。 Check this out.

0

レイアウトのルートからこの行を削除すると役立ちます。

tools:context=".MainActivity" 
+0

これは、実際にはIDEエディタによって制御されるアクティビティ以外は何も制御しません。アプリがコンパイルされると削除されます –

関連する問題