0

When I declare and try to get a reference to my button, which I declare in XML, the app won't open and I don't know why. PLEASE try to explain to me why not -- don't just give a link.

Here's the code:

public class MainActivity extends AppCompatActivity {
final String stream="<iframe width=\"300\" height=\"193\" src=\"http://cdn.livestream.com/embed/sl48?layout=4&color=0xffad4b&autoPlay=true&mute=false&iconColorOver=0xe17b00&iconColor=0xb96500&allowchat=true&height=193&width=300\" style=\"border:0;outline:0\" frameborder=\"0\" scrolling=\"no\"></iframe>";
 // Button button = (Button) findViewById(R.id.home);
//final Button button1 = (Button) findViewById(R.id.stream);
//final Button button2 = (Button) findViewById(R.id.contatti);

WebView custumWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    custumWebView = (WebView) findViewById(R.id.webView);
    custumWebView.loadData(stream, "text/html", null);
    custumWebView.getSettings().setJavaScriptEnabled(true);
    custumWebView.getSettings().setLoadsImagesAutomatically(true);
    custumWebView.clearHistory();
    custumWebView.clearCache(true);
    custumWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
}

public class CustomWebView extends WebView {
    public CustomWebView (Context context) {
        super( context );
        init();
    }

    public CustomWebView (Context context, AttributeSet attrs) {
        super( context, attrs );
        init();
    }

    public CustomWebView (Context context, AttributeSet attrs, int defStyle) {
        super( context, attrs, defStyle );
        init();
    }

    public CustomWebView (Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
        super( context, attrs, defStyle, privateBrowsing );
        init();
    }

    protected void init () {
        getSettings().setJavaScriptEnabled( true );
        setWebViewClient( new CustomWebViewClient() );
        setDownloadListener( new CustomDownloadListener() );
    }

    protected boolean overrideUrlLoading (WebView view, String url) {
        // ANY CUSTOM LOGIC GOES HERE

        view.loadUrl(url);

        return true;
    }

    protected void pageFinished (WebView view, String url) {
    }

    protected void downloadStarted(String url, String mimetype) {
        try {
            //Start new activity to load the specific url
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), mimetype);
            getContext().startActivity(intent);
            //custumWebView.loadData(stream, "text/html", null);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Default webview client.
     */
    protected class CustomWebViewClient extends WebViewClient {

        /**
         * Constructor, default.
         */
        public CustomWebViewClient () {

        }

        @Override
        public boolean shouldOverrideUrlLoading (WebView view, String url) {
            return overrideUrlLoading( view, url );
        }

        @Override
        public void onPageFinished (WebView view, String url) {
            pageFinished(view, url);
        }
    }

    protected class CustomDownloadListener implements DownloadListener {
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
            downloadStarted(url, mimetype);
        }
    }
}

and here the 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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webView"
        android:layout_below="@+id/stream"
        android:layout_alignRight="@+id/stream"
        android:layout_alignEnd="@+id/stream"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/imageView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Home"
        android:id="@+id/home"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="stream"
        android:id="@+id/stream"
        android:layout_alignTop="@+id/home"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Contatti"
        android:id="@+id/contatti"
        android:layout_above="@+id/webView"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:minHeight="50dp"
        android:minWidth="400dp" />
</RelativeLayout>

The CustomWebView class is so I don't get errors with loadData for the streaming site. I don't know why this class is working but it is. I copied it from the livestream support site, I'm only a newbie and I'm trying to learn.

Raceimaztion
  • 9,494
  • 4
  • 26
  • 41
  • yet another ... button will be there(findViewById will not return null) only if you set the layout ... now think what is first: setting the layout or class field initialization? – Selvin Aug 14 '15 at 13:18
  • for layout do you mean the onclick set up? or what , if you mean the xml , yeah i set the button layout – marco newbie tatangeli Aug 14 '15 at 13:21
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Aug 14 '15 at 13:25
  • If I've answered your question, please accept it. If not, please edit your question to explain why it didn't work. – AutonomousApps Aug 17 '15 at 13:29

1 Answers1

0

If you're referring to these two lines of code at the top of your class:

 // Button button = (Button) findViewById(R.id.home);
//final Button     //final Button button2 = (Button) findViewById(R.id.contatti);

That will not work in that location. Those buttons are created in your XML layout. You cannot get a reference to a button declared in an XML layout before you first call setContentView() in your onCreate() method. You must do this.

This is what you need to do:

public class MainActivity extends AppCompatActivity {
    private Button mButton;
    private Button mButton1;
    private Button mButton2;

    // declare any other fields you require

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mButton = (Button) findViewById(R.id.home);     
    mButton1 = (Button) findViewById(R.id.stream);
    mButton2 = (Button) findViewById(R.id.contatti);

    // Your other initialization code
}

Please note that you cannot declare those Buttons as final fields, as they are not instantiated in a constructor (onCreate() is not a constructor).

AutonomousApps
  • 4,229
  • 4
  • 32
  • 42