关于Java:在Android应用中获取致命异常

getting fatal exception in android app

我正在制作一个应用程序,尝试获取edittext对象,但它返回null,因此引发致命异常。

translateActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package diverse.technologies.transcriber;

public class translateActivity extends AppCompatActivity {

Button go;
TextView tv;
EditText et;
TableLayout tl,tml;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    tl = (TableLayout) findViewById(R.id.tablelayouttexthistory);
    tml = (TableLayout) findViewById(R.id.tablemainlayout);
    et = (EditText) findViewById(R.id.entertext);
    tv = (TextView) findViewById(R.id.textView2);
    go = (Button) findViewById(R.id.button);
    et.clearFocus();
    if(tml.requestFocus())
        Log.d("focus","got on table");
    else
        Log.d("focus","couldn't get");
    showhistory();   //showing history

    //other code
}

activity_translate.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="diverse.technologies.transcriber.translateActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tablemainlayout">

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <EditText
                    android:layout_height="89dp"
                    android:id="@+id/entertext"
                    android:text="Enter Text(English)"
                    android:layout_marginTop="20dp"
                    android:layout_width="206dp" />

                <Button
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Go"
                    android:id="@+id/button"
                    android:gravity="left|center_vertical"
                    android:layout_marginTop="22dp" />
            </TableRow>

            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:layout_width="280dp"
                    android:layout_height="wrap_content"
                    android:text="Translated Text(Gujarati)"
                    android:id="@+id/textView2"
                    android:layout_row="1"
                    android:layout_columnSpan="2"
                    android:layout_marginTop="7dp"
                    android:textSize="25dp"
                    android:layout_span="2" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow3"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TableLayout
                    android:id="@+id/tablelayouttexthistory"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_span="2">
                </TableLayout>
            </TableRow>

        </TableLayout>

    </ScrollView>

</RelativeLayout>

我得到的异常:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: diverse.technologies.transcriber, PID: 4447
java.lang.RuntimeException: Unable to start activity ComponentInfo{diverse.technologies.transcriber/diverse.technologies.transcriber.translateActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.clearFocus()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2509)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.clearFocus()' on a null object reference
at diverse.technologies.transcriber.translateActivity.onCreate(translateActivity.java:59)
at android.app.Activity.performCreate(Activity.java:6262)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

请帮忙。 我被困在这里。


您不是在设置活动的内容视图,而是直接找到该视图。

使用设置

1
setContentView(R.layout.activity_translate);

您忘记为活动添加布局,在super.onCreate(savedInstanceState);行之后的onCreate()方法中添加setContentView(R.layout.activity_translate);