Here is my problem, I want to make a stream player for my webradio. I got it to work, but I wanted to add swipe tabs. Now, when I run, i get a java null exception.
I looked a lot and I know what my problem is. The button I'm calling in main only exist in a fragment, so I get a null.
If I pass the problematic part in comment, I get my layout, and I can swipe between m two tabs.
What I would like is that, if a button is clicked in a fragment, something happens in the main activity. It might not be possible to do that, in that case, am I supposed to take most of my program into the fragment ?
MainActivity.java
package com.radioGMT.radio;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.radioGMT.tabsswipe.adapter.TabsPagerAdapter;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
private Button button_play;
private Button button_pause;
private AudioManager audioManager;
private TextView textView_info;
private PhoneStateListener phoneStateListener;
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "Direct", "Programme"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Add 2 tabs, specifying the tab's text and TabListener
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
MainActivity.this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
button_play = (Button) findViewById(R.id.button_play);
button_pause = (Button) findViewById(R.id.button_pause);
textView_info = (TextView) findViewById(R.id.textView_info);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//********Problem happens here********
button_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new PlayAsync().execute("http://ift.tt/1qCEnDK");
textView_info.setText("Pas de connexion internet");
}
});
button_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(MainActivity.this, MusicService.class));
}
});
phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
try {
button_pause.performClick();
} catch (Exception ex) {
}
} else if (state == TelephonyManager.CALL_STATE_IDLE) {
//Not in call: Play music
} else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
try {
button_play.performClick();
} catch (Exception ex) {
}
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if (mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
private class PlayAsync extends AsyncTask<String, Void, Boolean> {
private String url_to_play = "";
@Override
protected Boolean doInBackground(String... params) {
url_to_play = params[0];
Bundle b = new Bundle();
b.putString("url", url_to_play);
Intent i = new Intent(MainActivity.this, MusicService.class);
i.putExtras(b);
try {
stopService(new Intent(MainActivity.this, MusicService.class));
} catch (Exception ex) {
//
}
startService(i);
return true;
}
@Override
protected void onPostExecute(Boolean result) {
// Cette commande permet d'afficher l'url du stream
//textView_info.setText("Playing "+url_to_play);
// textView_info.setText("RadioGMT");
}
@Override
protected void onPreExecute() {
// textView_info.setText("Loading...");
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
// Check if there is an internet connection
public boolean hasConnection() {
ConnectivityManager cm = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnected()) {
return true;
}
NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileNetwork != null && mobileNetwork.isConnected()) {
return true;
}
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
return true;
}
return false;
}
//Following three allow for extends FragmentActivity implements ActionBar.TabListener
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
Lecteur_fragment.java
package com.radioGMT.radio;
import android.content.DialogInterface.OnClickListener;
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 android.widget.TextView;
public class LecteurFragment extends Fragment{
TextView textView_reinfo;
Button button_play;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_lecteur, container, false);
return view;
}
}
activity_main.xml
<android.support.v4.view.ViewPager xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
fragment_lecteur.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/Orange"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/Orange" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="@string/Header_image"
android:src="@drawable/logo_header_v2_4" />
</RelativeLayout>
<LinearLayout
android:id="@+id/Degrade_001"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@layout/gradient_noir_marron"
android:minHeight="10dp"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/Marron"
android:orientation="horizontal" >
<Button
android:id="@+id/button_play"
style="android:attr/buttonBarButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@android:drawable/ic_media_play"
android:text="@string/Lecture" />
<Button
android:id="@+id/button_pause"
style="android:attr/buttonBarButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@android:drawable/ic_media_pause"
android:text="@string/Pause" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/Marron"
android:orientation="vertical" >
<TextView
android:id="@+id/textView_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:id="@+id/Degrade_002"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@layout/gradient_marron_noir"
android:minHeight="10dp"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
There is also another tab, but its basically blank, just a background to know if it's working.
I know there is a lot of code, so thank you in advance for your help.
No comments:
Post a Comment