I'm a newbie in programming and stuff.. I'm using the android-youtube-player package from Pierfrancesco Soffritti.. and when I try to Get a reference to YouTubePlayer and run the app it crashes..
`
package com.example.youtuuuuuuuuuuuuuuube;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.YouTubePlayerListener;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.options.IFramePlayerOptions;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.ui.DefaultPlayerUiController;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.ui.PlayerUiController;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.ui.menu.YouTubePlayerMenu;
public class MainActivity extends AppCompatActivity {
private YouTubePlayer youTubePlayer;
private YouTubePlayerView youTubePlayerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void playvideo(View v){
youTubePlayer.loadVideo("S0Q4gqBUs7c", 1f);
}
}
`
my XML code
<LinearLayout
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"
android:orientation="vertical" >
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:id="#+id/youtube_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:videoId="S0Q4gqBUs7c"
app:autoPlay="true"
/>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="playvideo"
android:text="Button" />
I tried to get the reference using OnStart() method, added a button but all of that don't work :(
Related
i have a app set up which loads a website into the app which works fine, when i try to add another element to the content view the app crashes without a error, How can i add more than one element to my set content view? ive tried it with frame layout and more but they all dont seem to work.
Here is my code
package com.example.fiercepcs;
import android.content.Context;
import android.os.Bundle;
import com.example.fiercepcs.databinding.ActivityMainBinding;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.app.Application;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout screenLayout = new LinearLayout(this);
screenLayout.setOrientation(LinearLayout.VERTICAL);
Button button = (Button) findViewById(R.id.button);
WebView myWebView = new WebView(getApplicationContext());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://www.fiercepc.co.uk");
setContentView(myWebView);
setContentView(button);
}
}
my activity main xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.FiercePcs.AppBarOverlay" />
<include
android:id="#+id/include"
layout="#layout/content_main" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:translationZ="-5dp">
</WebView>
<Button
android:id="#+id/button"
android:layout_width="312dp"
android:layout_height="179dp"
android:scaleX="1"
android:scaleY="1"
android:text="#string/button"
android:visibility="visible"
app:layout_anchor="#+id/include"
app:layout_anchorGravity="center" />
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
your app crashes because your are trying to set button view and webView as the root layout, don't do that, inflate or bind the root of your layout with ActivityMainBinding
do it like this :
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bindnig = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
LinearLayout screenLayout = new LinearLayout(this);
screenLayout.setOrientation(LinearLayout.VERTICAL);
Button button = (Button) findViewById(R.id.button);
WebView myWebView = new WebView(getApplicationContext());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://www.fiercepc.co.uk");
}
}
ERROR:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText(java.lang.CharSequence)' on a null
object reference
ON LINE:
olxLoadingFragment.pricesTextView.setText("some text");
I have one Fragment class that's called OLXLoadingFragment and it has one TextView nested in ScrollView. I basically want to update that TextView's text dynamically in my OLXThread, but can't pass that TextView object to the OLXThread itself. Here are code parts that could help you:
OLXLoadingFragment class:
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import java.io.Serializable;
public class OLXLoadingFragment extends Fragment {
public static View OLXLoadingFragmentView;
public static ScrollView scrollViewPrices;
public static TextView pricesTextView;
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
OLXLoadingFragmentView = inflater.inflate(R.layout.fragment_olx_loading, container, false);
scrollViewPrices = OLXLoadingFragmentView.findViewById(R.id.olx_scroller);
pricesTextView = OLXLoadingFragmentView.findViewById(R.id.olx_prices_text);
return inflater.inflate(R.layout.fragment_olx_loading, container, false);
}
}
OLXThread class:
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.ScrollView;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.w3c.dom.Text;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import static android.app.PendingIntent.getActivity;
public class OLXThread extends Thread {
public static double fullSum = 0.0d;
public static ArrayList<String> pricesPerPage = new ArrayList<String>();
public static ArrayList<String> pricesPerPage2 = new ArrayList<String>();
public static ArrayList<String> prices = new ArrayList<String>();
public static String toSearch;
public static TextView olxpricesView;
public static Context context;
public OLXThread(String toSearch) {
this.toSearch = toSearch;
}
public void run() {
//SET SOME TEXT TO TEXTVIEW HERE
OLXLoadingFragment olxLoadingFragment = new OLXLoadingFragment();
olxLoadingFragment.pricesTextView.setText("some text"); //<- DOESN'T WORK!
}
OLXLoadingFragment xml layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/rectangle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.82" />
<TextView
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:gravity="center"
android:text="#string/olx_loading_engl_text"
android:textColor="#color/limegreen"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.82" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="287dp"
android:layout_height="284dp"
android:src="#drawable/rectangle"
app:layout_constraintBottom_toBottomOf="#+id/olx_scroller"
app:layout_constraintEnd_toEndOf="#+id/olx_scroller"
app:layout_constraintStart_toStartOf="#+id/olx_scroller"
app:layout_constraintTop_toTopOf="#+id/olx_scroller" />
<ScrollView
android:id="#+id/olx_scroller"
android:layout_width="287dp"
android:layout_height="284dp"
android:layout_marginBottom="36dp"
android:fillViewport="true"
android:scrollbars="vertical"
app:layout_constraintBottom_toTopOf="#+id/imageView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/olx_prices_text"
android:text="neki tekst"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
/>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Please help!
At the time you call the method pricesTextView.setText(), the OnCreateView method of the Fragment wasn't called yet, so the pricesTextView was still null, as you only set it's value on the onCreateView method.
You should ensure that pricesTextView is not null before you call any method on this.
Note also that the only Thread that can touch views is the Ui thread, so even if your TextView is not null you cannot modify it's properties in other threads
I am trying to pass an Intent from my mainScreen Activity class to a new android class with a layout that contains a ListView ..
MainActivityScreen Code :
(TimePassed is in the format of 'HH:MM:SS')
public void MoveToShiftsPage(View view) {
Intent shiftsIntent=new Intent(this,Shifts.class);
shiftsIntent.putExtra("time",TimePassed);
startActivity(shiftsIntent);
}
"MoveToShiftsPage" in the MainActivity.xml file :
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#mipmap/payment"
android:id="#+id/shiftbtn"
android:onClick="MoveToShiftsPage"
/>
Shifts.xml File( the layout which contains the ListView and the Intent sent to it)
<LinearLayout 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=".MainScreen"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:id="#+id/MyListView"
android:clickable="true"
style="#style/Base.Widget.AppCompat.ListView.DropDown">
</ListView>
/>
and the Shift.class which gets the Intent and it's supposed to represent the ListView with the time that passed :
import android.app.Activity;
import android.content.Intent;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.util.List;
/**
* Created by user on 19/09/2015.
*/
public class Shifts extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shifts);
Intent MyIntent=getIntent();
String TimePassed=MyIntent.getStringExtra("time");
String [] timepass=TimePassed.split(":");
ListAdapter theAdapter=new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_activated_1,timepass);
ListView thelist=(ListView)findViewById(R.id.MyListView);
thelist.setAdapter(theAdapter);
}
}
Unfortunately whenever I click on the image button which is supposed to be sent to the shift activity class and represents the layout with the ListView the app crashes ..
what should I do ?
try this one hopefully it works
Intent MyIntent=this.getIntent();
There is a good link on how to load large Bitmaps efficiently, so that you don't get an OutOfMemory-Error.
Here ist the link: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
But my problem is, that I am a very newbie and I don't know how to understand the link. Could you please help me and modify my codes?
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="Menue"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="200dp"
android:layout_height="100"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/pfeilrechts"
android:onClick="Picture2"/>
</RelativeLayout>
JAVA-Code:
package com.example.xxx;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class PictureOne extends Activity {
public ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pictureone);
iv = (ImageView)findViewById(R.id.imageView2);}
{
ImageView.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.imageView2, 100, 100));}
public void Picture2 (View view){
Intent i = new Intent(this, MainActivity.class);
startActivity(i);}}
I created a button to take me to another page and in that other page, there is another button that takes to another page, so page1, page2 and page3 that it's not working with me, check http://oi62.tinypic.com/2m7g4et.jpg when I click on "Fish guide" it takes me to the other one which has another button "Fish", when I click on "Fish" it doesn't take me to the 3rd page!
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#1d72c3" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:orientation="vertical"
android:textStyle="italic" >
<Button
android:id="#+id/fishguide"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Fish guide"
android:textSize="20sp"
android:textStyle="italic" />
</LinearLayout>
</RelativeLayout>
fishguide.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"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#1d72c3"
android:textStyle="italic" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:textStyle="italic" >
<Button
android:id="#+id/fish"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Fish"
android:textSize="22sp"
android:textStyle="italic" />
</LinearLayout>
</RelativeLayout>
fish.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1d72c3" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:textStyle="italic" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#drawable/bala" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Balabanka"
android:textSize="18sp"
android:textStyle="italic" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button3 = (Button) findViewById(R.id.fishguide);
button3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageThree.class);
startActivity(intent);
}
});
}
}
PageThree.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageThree extends Activity {
Button button3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fishguide);
}
}
I did the third page, it runs but it was .. wrong
MainActivity2.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity2 extends Activity {
Button fishlink;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fishguide);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
fishlink = (Button) findViewById(R.id.fish);
fishlink.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageFish.class);
startActivity(intent);
}
});
}
}
PageFish.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageFish extends Activity {
Button fishlink;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fish);
}
}
Help :)
Edit / it goes "Fish guide" to "Fish" when I click on "Fish" it takes me again to "Fish" and so on, it doesn't go to the third page!
PageThree.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PageThree extends Activity {
Button button3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fishguide);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button3 = (Button) findViewById(R.id.fish);
button3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageThree.class);
startActivity(intent);
}
});
}
}
You have used
setContentView(R.layout.fishguide);
in PageThree.java
Change it to the xml you want to show with that third image in image. say, third.xml
setContentView(R.layout.third);
And you are done.
NOTE: You are getting the same Layout again and again because in PageThree.java you are starting the activity with intent PageThree.java. Instead just change that to PageFish.java.
So when you are at second layout (PageThree.java), just start activity for third layout (PageFish.java) on button click as follows -
PageThree.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageThree extends Activity {
Button button3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fishguide);
button3 = (Button) findViewById(R.id.fish);
button3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(PageThree.this, PageFish.class);
startActivity(intent);
}
});
}
}