gpt4 book ai didi

java - 在 Android 应用程序中获取致命异常

转载 作者:行者123 更新时间:2023-11-29 04:34:09 24 4
gpt4 key购买 nike

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

translateActivity.java

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

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

请帮忙。我被困在这里..

最佳答案

您不是设置 Activity 的内容 View 并直接找到 View 。

使用

设置
setContentView(R.layout.activity_translate);

关于java - 在 Android 应用程序中获取致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42465814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com