0

So I have 2 java classes, the main one being intents.java and the second one being BrowserView.java

I´ve declared both on the android manifest and added the permissions as well.

The main XML file is called main_linearlayout

Can anyone spot the error?

Thank you!

Also - the search button seems to be doing nothing when clicked, not quite sure why?

intents.java:

package com.course.example;

import android.app.Activity;
import android.content.Intent;
import android.content.ComponentName;
import android.os.Bundle;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class intents extends Activity implements OnClickListener {
private EditText checkAmount, numOfPeople;
private double tip = 0.15;
private TextView viewBill, viewPerson, viewTip;
double totalB, totalP, totalTP, totalT;

// just in case:
//set a request code number to identify a particular request
public static final int requestCode_235 = 235;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(android.R.style.Theme_Dialog);
    setContentView(R.layout.main_linearlayout);

    // Set up click listeners for all the buttons
   // Button continueButton = (Button)findViewById(R.id.first_button);
    //continueButton.setOnClickListener(this);

    Button goButton = (Button)findViewById(R.id.btnGo);
    goButton.setOnClickListener(this);

    Button searchButton = (Button)findViewById(R.id.btnSearch);
    searchButton.setOnClickListener(this);

    Button mapButton = (Button)findViewById(R.id.btnMap);
    mapButton.setOnClickListener(this);

    Button dialButton = (Button)findViewById(R.id.btnDial);
    dialButton.setOnClickListener(this);

}



//avoids runtime check for permission to CALL_PHONE
public void onClick(View v)  {
    switch (v.getId()) {

        // 1) get the calculations
        case R.id.btnGo:
            // get info from text box called checkAmount:
            checkAmount = (EditText) findViewById(R.id.checkAmount);
            String checkString = checkAmount.getText().toString();
            double checkAmountD = Double.parseDouble(checkString);
            double checkAmountR = Math.round(checkAmountD * 100.0) / 100.0;

            // get info from number of people
            numOfPeople = (EditText) findViewById(R.id.numOfPeople);
            String numPeopleS = numOfPeople.getText().toString();
            double numPeopleD = Double.parseDouble(numPeopleS);

            // calculate total tip, total bill, total per person, and total tip per person
            totalT = checkAmountR  * tip;
            totalB = checkAmountR + totalT;
            totalP = totalB / numPeopleD;
            totalTP = totalT / numPeopleD;
            setNewText();           // print out calculations on app! :)
            break;


        //2) implicit intent, open dialer and make call
        case R.id.btnSearch:
            Intent i1 = new Intent(this, BrowserView.class);
            startActivityForResult(i1, requestCode_235);
            //startActivity(i1);
            break;

        //3) implicit intent, call GoogleMaps
        case R.id.btnMap:

            Uri uri2 = Uri.parse("geo:42.3889167,-71.2208033?z=18");

            Intent i2 = new Intent(Intent.ACTION_VIEW,uri2);

            if (i2.resolveActivity(getPackageManager()) != null) {
                startActivity(i2);
            }
            break;

        //4) implicit intent, open dialer and make call
        case R.id.btnDial:
            Uri uri3 = Uri.parse("tel:7818912000");
            Intent i3 = new Intent(Intent.ACTION_CALL,uri3);
            startActivity(i3);
            break;

    }
}

public void setNewText(){
    // print these 4 out on the edit texts
    TextView viewBill = (TextView) findViewById(R.id.totalBill);
    double totalBRounded = Math.round(totalB * 100.0) / 100.0;
    String totalBillS = String.valueOf(totalBRounded);   // before rounding this was valueof(totalB)
    viewBill.setText("$" + totalBillS);

    TextView viewPerson = (TextView) findViewById(R.id.totalPerPerson);
    double totalPRounded = Math.round(totalP * 100.0) / 100.0;
    String totalPerPS = String.valueOf(totalPRounded);
    viewPerson.setText("$" + totalPerPS);

    TextView viewTip = (TextView) findViewById(R.id.totalTip);
    double totalTipRounded = Math.round(totalT * 100.0) / 100.0;
    String totalTipS = String.valueOf(totalTipRounded);
    viewTip.setText("$" + totalTipS);

    TextView viewTipPerPerson = (TextView) findViewById(R.id.tipPerPerson);
    double totalTipPerPRounded = Math.round(totalTP * 100.0) / 100.0;
    String tipPerPersonS = String.valueOf(totalTipPerPRounded);
    viewTipPerPerson.setText("$" + tipPerPersonS);
}


//listen for event of requested activity finishing
@Override
protected void onActivityResult(int requestCode,
                                int resultCode,  Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode){
        case (requestCode_235): {

            if (resultCode == Activity.RESULT_OK)
                Toast.makeText(this, "WebLookup finished", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(this, "WebLookup is NOT working", Toast.LENGTH_LONG).show();
            break;
        }

        default : Toast.makeText(this, "", Toast.LENGTH_LONG).show();
    }//switch

}// onActivityResult

}

browser view. java:

package com.course.example;
        import android.app.Activity;
        import android.os.Bundle;
        import android.view.KeyEvent;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.view.View.OnKeyListener;
        import android.webkit.WebView;
        import android.webkit.WebViewClient;
        import android.widget.Button;
        import android.widget.EditText;

public class BrowserView extends Activity {
private EditText urlText;
private Button searchButton;
private WebView webView;

@Override
public void onCreate(Bundle savedInstanceState) {
    // ...
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen2);

    // Get a handle to all user interface elements
    urlText = (EditText) findViewById(R.id.url_field);
    searchButton = (Button) findViewById(R.id.btnSearch);
    webView = (WebView) findViewById(R.id.web_view);
    webView.getSettings().setJavaScriptEnabled(true);

    //intercept URL loading and load in widget
    webView.setWebViewClient(new WebViewClient(){
        @SuppressWarnings("deprecation")
        public boolean shouldOverrideUrlLoading(WebView view, String url){
            view.loadUrl(url);
            return true;
        }
    });

    // Set button to open browser
    searchButton.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            webView.loadUrl(urlText.getText().toString());
        }
    });

    //set listener on EditText
    urlText.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View view, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                webView.loadUrl(urlText.getText().toString());
                return true;
            }
            return false;
        }
    });
}

//the back key navigates back to the previous web page - we were looking at this
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

}

first couple of lines of logcat:

21-02-27 08:10:35.387 2853-2853/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.course.example, PID: 2853
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.course.example/com.course.example.intents}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.course.example.intents.onCreate(intents.java:49)
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
2021-02-27 08:11:23.928 5064-5064/com.course.example E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.course.example, PID: 5064
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.course.example/com.course.example.intents}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.course.example.intents.onCreate(intents.java:49)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
2021-02-27 08:11:27.268 5101-5101/com.course.example E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.course.example, PID: 5101

Edit: here are the 2 XML files:

main linear layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ACA6AA"
    android:orientation="vertical"
    android:padding="4dp" >

    <TextView
        android:id="@+id/labelUserName"
        android:layout_width="300dp"
        android:layout_height="80dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        android:background="#F688B4"
        android:fontFamily="@font/alfa_slab_one"
        android:gravity="center"
        android:text="Tip Calculator"
        android:textColor="#CCCECE"
        android:textSize="25sp"
        android:textStyle="bold"></TextView>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_weight="2"
            android:background="#F688B4"
            android:fontFamily="serif-monospace"
            android:gravity="center"
            android:text="Check Amount"
            android:textColor="@color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/checkAmount"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:textSize="18sp" >
        </EditText>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_weight="2"
            android:background="#F688B4"
            android:fontFamily="serif-monospace"
            android:gravity="center"
            android:text="Number of People"
            android:textColor="@color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/numOfPeople"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:textSize="18sp" >
        </EditText>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_weight="2"
            android:background="#F688B4"
            android:fontFamily="serif-monospace"
            android:gravity="center"
            android:text="Tip Percentage%"
            android:textColor="@color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/tipPercent"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:textSize="18sp" >
        </EditText>

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnGo"
            style="@style/demoStyle"
            android:text="Tip"></Button>

        <Button
            android:id="@+id/btnWeb"
            style="@style/demoStyle"
            android:text="WEB"></Button>

        <Button
            android:id="@+id/btnDial"
            style="@style/demoStyle"
            android:text="DIAL"></Button>

        <Button
            android:id="@+id/btnMap"
            style="@style/demoStyle"
            android:text="MAP"></Button>


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginLeft="2dp"
            android:layout_weight="2"
            android:background="#F688B4"
            android:fontFamily="@font/abhaya_libre"
            android:gravity="center"
            android:text="Total Bill"
            android:textColor="@color/black"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/totalBill"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginLeft="2dp"
            android:layout_weight="2"
            android:fontFamily="@font/abhaya_libre"
            android:gravity="center"
            android:textColor="@color/black"
            android:textSize="16sp" />
    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="2"
        android:background="#F688B4"
        android:fontFamily="@font/abhaya_libre"
        android:gravity="center"
        android:text="Total per Person"
        android:textColor="@color/black"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/totalPerPerson"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="2"
        android:fontFamily="@font/abhaya_libre"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="16sp" />
</LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginLeft="2dp"
            android:layout_weight="2"
            android:background="#F688B4"
            android:fontFamily="@font/abhaya_libre"
            android:gravity="center"
            android:text="Total Tip"
            android:textColor="@color/black"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/totalTip"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginLeft="2dp"
            android:layout_weight="2"
            android:fontFamily="@font/abhaya_libre"
            android:gravity="center"
            android:textColor="@color/black"
            android:textSize="16sp" />
    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="2"
            android:background="#F688B4"
            android:fontFamily="@font/abhaya_libre"
            android:gravity="center"
            android:text="Tip per Person"
            android:textColor="@color/black"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tipPerPerson"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="2"
            android:fontFamily="@font/abhaya_libre"
            android:gravity="center"
            android:textColor="@color/black"
            android:textSize="16sp" />
    </LinearLayout>



</LinearLayout>

screen 2:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/url_field"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4" />

    <Button
        android:id="@+id/btnSearch"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="search" />
</LinearLayout>

<WebView
    android:id="@+id/web_view"
    android:layout_width="match_parent"
    android:background="#ACA6AA"
    android:layout_height="match_parent" />

</LinearLayout>

1 Answers1

-1

The problem is you set the onClickListener on the button by setOnClickListener and you don't implement view.onClickListener. you made a method with parameter of View and you don't have any onclick attribute on your xml. either implement view.onclicklistener and have setonclicklistener or set attribute and have that method. this should work properly and also for your search button too if you are adding onclicklistener on button with that method you should use new View.OnClickListener the plain onclicklistener shall not work