在Android应用程序

时间:2017-02-26 07:19:42

标签: java android nullpointerexception

我正在创建一个应用程序,我正在尝试获取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:致命异常:主要                                                    流程:diverse.technologies.transcriber,PID:4447                                                    java.lang.RuntimeException:无法启动活动ComponentInfo {diverse.technologies.transcriber / diverse.technologies.transcriber.translateActivity}:java.lang.NullPointerException:尝试调用虚方法'void android.widget.EditText.clearFocus()'在null对象引用上                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2509)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)                                                        在android.app.ActivityThread.access $ 900(ActivityThread.java:150)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1399)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:168)                                                        在android.app.ActivityThread.main(ActivityThread.java:5885)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:797)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)                                                     引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.EditText.clearFocus()'                                                        at diverse.technologies.transcriber.translateActivity.onCreate(translateActivity.java:59)                                                        在android.app.Activity.performCreate(Activity.java:6262)                                                        在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)                                                        在android.app.ActivityThread.access $ 900(ActivityThread.java:150)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1399)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:168)                                                        在android.app.ActivityThread.main(ActivityThread.java:5885)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:797)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

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

2 个答案:

答案 0 :(得分:3)

您没有设置活动的内容视图并直接查找视图。

使用

设置
setContentView(R.layout.activity_translate);

答案 1 :(得分:0)

您忘记添加活动的布局,在onCreate()

之后的super.onCreate(savedInstanceState);方法中添加// dbs size $sql = 'SELECT table_schema DB_Name, Round(Sum(data_length + index_length) / 1024 / 1024, 2) DB_Size_in_MB FROM information_schema.tables GROUP BY table_schema'; $dbs_size = DB::select($sql);