私はアンドロイドスタジオを新しくしています。私は仕事で数日間座っていましたが、断片的に私のボタンを管理しなかったので、期待どおりに働きました。フラグメントコードonClick()をメインアクティビティとして認識させるにはどうすればよいですか?
マイ構造:私のフラグメントのために
、私はボタンのための私のメソッドが定義されているJavaクラスを持っています。 (onClickの())
私は私のボタンをクリックすると、私はアプリのクラッシュ、...フラグメント
のフラグメント/ Javaのクラスを/が/知っている含まれている主な活性を持っています。 私は、フラグメントを主なアクティビティにバインドして何かをしなければならないと思います。私はすでにそれを言ったと思った:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sub_page01, container, false);
return rootView;
}
主な活動の中で。
しかし、私は何か重要なことを忘れているようです。誰かが私を助けることができるなら、私はそれを感謝します。
MainActivity.java
package com.example.yasars.my_fernbedienung;
import android.os.AsyncTask;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
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.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.HttpAuthHandler;
import android.widget.TextView;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.app.Activity;
import android.util.Log;
import org.json.JSONException;
import org.w3c.dom.Text;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private SeekBar mySoundBar;
private TextView textView;
private HttpRequest httpRequest;
private String ipaddress;
private void initializeVariables() {
mySoundBar = (SeekBar) findViewById(R.id.mySoundBar); // initiate the Seekbar
textView = (TextView) findViewById(R.id.volumeView); // initiate volume print
ipaddress = "192.168.56.1";
httpRequest = new HttpRequest(ipaddress, 4, true); //4 stands for 4 milliseconds to wait for a response
}
public void onClick(View v) {
}
private static final String Tag = "myDebugAid";
/**
* 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;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
public void addContentView(View view, ViewGroup.LayoutParams params) {
super.addContentView(view, params);
Log.i(Tag, "addContentView");
}
@Override
protected void onStop() {
super.onStop();
Log.i(Tag, "onStop");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(Tag, "onSaveInstanceState");
}
@Override
protected void onPause() {
super.onPause();
Log.i(Tag, "onPause");
}
@Override
protected void onResume() {
super.onResume();
Log.i(Tag, "onResume");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeVariables();
mySoundBar.setMax(100); // 100 maximum value for the Seek bar
mySoundBar.setProgress(10);
textView.setText(mySoundBar.getProgress() + "/" + mySoundBar.getMax());
mySoundBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progress = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
progress = progressValue;
textView.setText(progress + "/" + seekBar.getMax());
try {
httpRequest.execute("volume=" + progress);
} catch (IOException | JSONException | IllegalArgumentException e) {
}
//Toast.makeText(getApplicationContext(), "Changing volume", Toast.LENGTH_SHORT).show();
//we don't need above line. It is irritating for the user
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
textView.setText(progress + "/" + seekBar.getMax());
try {
httpRequest.execute("volume=" + progress);
} catch (IOException | JSONException | IllegalArgumentException e) {
}
//Toast.makeText(getApplicationContext(), "Started tracking volume", Toast.LENGTH_SHORT).show();
//we don't need above line. It is irritating for the user
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
textView.setText(progress + "/" + seekBar.getMax());
try {
httpRequest.execute("volume=" + progress);
} catch (IOException | JSONException | IllegalArgumentException e) {
}
}
});
//HttpRequest httpReq = new HttpRequest(); // an instance of HttpRequest
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);
try {
httpRequest.execute("channelMain=8a");
} catch (IOException | JSONException | IllegalArgumentException e) {
}
}
@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();
return super.onOptionsItemSelected(item);
}
// seekBarInstanceVariable.setOnSeekBarChangeListener(new OnSeekBarChangeListener()) {
//
// } //This method is used to notify the user changes/actions in the SeekBar.
public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
} //This listener method will be invoked if any change is made in the SeekBar.
public void onStartTrackingTouch(SeekBar seekBar) {
} //This listener method will be invoked at the start of user’s touch event. Whenever a user
// touch the thumb for dragging this method will automatically called.
public void onStopTrackingTouch(SeekBar seekBar) {
} //This listener method will be invoked at the end of user touch event. Whenever a user stop
// dragging the thump this method will be automatically called.
/**
* 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) {
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
View rootView = inflater.inflate(R.layout.fragment_sub_page01, container, false);
return rootView;
} else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
View rootView = inflater.inflate(R.layout.fragment_sub_page02, container, false);
return rootView;
} else if (getArguments().getInt(ARG_SECTION_NUMBER) == 3) {
View rootView = inflater.inflate(R.layout.fragment_sub_page03, container, false);
return rootView;
} else {
View rootView = inflater.inflate(R.layout.fragment_main, 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).
return 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 "Home";
case 1:
return "Favoriten";
case 2:
return "Sender";
}
return null;
}
}
}
SubPage01.java:
package layout;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import java.io.IOException;
import java.util.Vector;
import java.util.Map;
import com.example.yasars.my_fernbedienung.HttpRequest;
import com.example.yasars.my_fernbedienung.R;
import org.json.JSONException;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link SubPage01.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link SubPage01#newInstance} factory method to
* create an instance of this fragment.
*/
public class SubPage01 extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private Button button_tri_right;
private Button button_tri_left;
private HttpRequest httpRequest;
private String ipaddress;
private int onChannel = 1; //it is for the channel recognition.
private Map<String, String> ChannelMap;
private void initializeHttpRequest() {
ipaddress = "192.168.56.1";
httpRequest = new HttpRequest(ipaddress, 4, true);
}
private void fillChannelContainer() {
ChannelMap.put("1", "8a");
ChannelMap.put("2", "8b");
ChannelMap.put("3", "8c");
ChannelMap.put("4", "22a");
ChannelMap.put("5", "22b");
ChannelMap.put("6", "22c");
ChannelMap.put("7", "22d");
ChannelMap.put("8", "34a");
ChannelMap.put("9", "34b");
ChannelMap.put("10", "34c");
ChannelMap.put("11", "34d");
ChannelMap.put("12", "37a");
ChannelMap.put("13", "37b");
ChannelMap.put("14", "37c");
/* ChannelMap.put("Phoenix", "8a");
ChannelMap.put("BR", "8b");
ChannelMap.put("SWR", "8c");
ChannelMap.put("ZDF", "22a");
ChannelMap.put("3Sat", "22b");
ChannelMap.put("ZDFinfo" "22c");
ChannelMap.put("KiKa", "22d");
ChannelMap.put("RTL", "34a");
ChannelMap.put("RTL2", "34b");
ChannelMap.put("Super RTL", "34c");
ChannelMap.put("Vox", "34d");
ChannelMap.put("Das Erste", "37a");
ChannelMap.put("hr", "37b");
ChannelMap.put("arte", "37c");*/
}
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public SubPage01() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment SubPage01.
*/
// TODO: Rename and change types and number of parameters
public static SubPage01 newInstance(String param1, String param2) {
SubPage01 fragment = new SubPage01();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
initializeHttpRequest();
fillChannelContainer();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//return inflater.inflate(R.layout.fragment_sub_page01, container, false);
View v = inflater.inflate(R.layout.fragment_sub_page01, container, false);
Button b1 = (Button) v.findViewById(R.id.right_triangle_button);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.right_triangle_button: {
if (onChannel == 14) {
onChannel = 1;
} else {
onChannel = onChannel + 1;
}
String tmpString = String.valueOf(onChannel);
try {
httpRequest.execute("channelMain=" + ChannelMap.get(tmpString));
System.out.println(ChannelMap.get(tmpString));
} catch (IOException | JSONException | IllegalArgumentException e) {
}
}
}
}
});
Button b2 = (Button) v.findViewById(R.id.left_triangle_button);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.left_triangle_button: {
if (onChannel == 1)
{
onChannel = 14;
} else
{
onChannel = onChannel - 1;
}
String tmpString = String.valueOf(onChannel);
try
{
httpRequest.execute("channelMain=" + ChannelMap.get(tmpString));
System.out.println(ChannelMap.get(tmpString));
} catch (IOException | JSONException |
IllegalArgumentException e)
{
}
}
}
}
});
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
button_tri_right = (Button) getView().findViewById(R.id.right_triangle_button); //this is necessary because you can't
button_tri_left = (Button) getView().findViewById(R.id.left_triangle_button); //use findViewById() directly, like in
//activities.
}
/*// TODO: Rename method, update argument and hook method into UI event
public void onButtonRightPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
String tmpString = String.valueOf(onChannel);
try {
httpRequest.execute("channelMain=" + ChannelMap.get(tmpString));
} catch (IOException | JSONException | IllegalArgumentException e) {
}
onChannel = onChannel + 1;
}
public void onButtonLeftPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
String tmpString = String.valueOf(onChannel);
try {
httpRequest.execute("channelMain=" + ChannelMap.get(tmpString));
System.out.println(ChannelMap.get(tmpString));
} catch (IOException | JSONException | IllegalArgumentException e) {
}
onChannel = onChannel + 1;
}*/
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
fragment_sub_page01.xml
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="layout.SubPage01">
<Button
android:id="@+id/left_triangle_button"
android:layout_width="60dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginStart="-100dp"
android:background="@drawable/triangle_button_left"
android:clickable="true"
app:backgroundTint="#076a24" />
<Button
android:id="@+id/right_triangle_button"
android:layout_width="60dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginStart="100dp"
android:background="@drawable/triangle_button_right"
android:clickable="true"
android:onClick="onClick"
app:backgroundTint="#076a24" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Sendung"
android:layout_gravity="center"
android:layout_marginTop="-100dp"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Sender"
android:layout_gravity="center"
android:layout_marginTop="-150dp" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="4"
android:hint="Nr."
android:inputType="number"
android:textAlignment="center" />
</FrameLayout>
私はいくつかの可能な解決策を試してみましたが、私の問題を解決するために管理していませんでした。
ありがとうございます。
編集:トークボタンについてです:
- left_triangle_button right_triangle_button
クラッシュログを提供する – Yupi