Java Android Studio:logcat表示空引用,但我不知道´;我不明白为什么?

Java Android Studio:logcat表示空引用,但我不知道´;我不明白为什么?,java,android,android-studio,logcat,Java,Android,Android Studio,Logcat,所以我有两个java类,一个是intents.java,另一个是BrowserView.java 我已经在android清单上声明了这两项,并添加了权限 主XML文件称为main_linearlayout 有人能发现错误吗 谢谢大家! 还有-点击搜索按钮时似乎什么都没做,不太清楚为什么 intents.java: package com.course.example; import android.app.Activity; import android.content.Intent; imp

所以我有两个java类,一个是intents.java,另一个是BrowserView.java

我已经在android清单上声明了这两项,并添加了权限

主XML文件称为main_linearlayout

有人能发现错误吗

谢谢大家!

还有-点击搜索按钮时似乎什么都没做,不太清楚为什么

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
}

浏览器视图。爪哇:

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);
}
}

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
编辑:以下是2个XML文件:

主要线性布局:

<?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>

屏幕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>

问题是您通过
setOnClickListener
按钮上设置了
onClickListener
,而您没有实现
视图。onClickListener
。 您创建了一个参数为
View
的方法,在
xml
上没有任何
onclick
属性。 实现
view.onclicklistener
并具有
setonclicklistener
或设置属性并具有该方法。 这应该能正常工作
如果你在
按钮
上添加
onclicklistener
,你应该使用
新视图。onclicklistener
普通的
onclicklistener
将不起作用

请发布相关布局文件,
screen2
main\u linearlayout
检查您的布局和视图id。在这一行中,它们似乎不匹配@a_local_没有人刚刚添加它!:)您是否在主布局中看到名为
btnSearch
的内容?(findview找不到视图,它为空,应用程序崩溃)@a_local_nobody噢,你说得对!真不敢相信我错过了这个,谢谢!否,在
main\u linearlayout
中,xml中没有ID为
btnSearch
的任何内容,因此findViewById应返回null。如果findView返回Null,则如何设置click侦听器无关紧要,可以尝试使按钮成为一个类字段来完成这项工作