gpt4 book ai didi

java - 致命异常/无法在 Android 中实例化错误

转载 作者:行者123 更新时间:2023-11-29 05:53:22 25 4
gpt4 key购买 nike

请看下面的代码

activity_main.xml

<LinearLayout 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" >

<EditText
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>

MainActivity.java

package com.example.textbox;

import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

private EditText text = (EditText)findViewById(R.id.text);

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

text.setOnKeyListener(new TextMaker());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

private class TextMaker implements OnKeyListener
{

String str = text.getText().toString();


public boolean onKey(View arg0, int keyCode, KeyEvent event)
{
if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
return true;
}
else
{
return false;
}
}
}
}

当我运行这个应用程序时,出现以下错误

10-27 19:05:46.487: D/AndroidRuntime(339): Shutting down VM
10-27 19:05:46.487: W/dalvikvm(339): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-27 19:05:46.527: E/AndroidRuntime(339): FATAL EXCEPTION: main
10-27 19:05:46.527: E/AndroidRuntime(339): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.textbox/com.example.textbox.MainActivity}: java.lang.NullPointerException
10-27 19:05:46.527: E/AndroidRuntime(339): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
10-27 19:05:46.527: E/AndroidRuntime(339): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-27 19:05:46.527: E/AndroidRuntime(339): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-27 19:05:46.527: E/AndroidRuntime(339): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-27 19:05:46.527: E/AndroidRuntime(339): at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 19:05:46.527: E/AndroidRuntime(339): at android.os.Looper.loop(Looper.java:123)
10-27 19:05:46.527: E/AndroidRuntime(339): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-27 19:05:46.527: E/AndroidRuntime(339): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 19:05:46.527: E/AndroidRuntime(339): at java.lang.reflect.Method.invoke(Method.java:507)
10-27 19:05:46.527: E/AndroidRuntime(339): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-27 19:05:46.527: E/AndroidRuntime(339): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-27 19:05:46.527: E/AndroidRuntime(339): at dalvik.system.NativeStart.main(Native Method)
10-27 19:05:46.527: E/AndroidRuntime(339): Caused by: java.lang.NullPointerException
10-27 19:05:46.527: E/AndroidRuntime(339): at android.app.Activity.findViewById(Activity.java:1647)
10-27 19:05:46.527: E/AndroidRuntime(339): at com.example.textbox.MainActivity.<init>(MainActivity.java:15)
10-27 19:05:46.527: E/AndroidRuntime(339): at java.lang.Class.newInstanceImpl(Native Method)
10-27 19:05:46.527: E/AndroidRuntime(339): at java.lang.Class.newInstance(Class.java:1409)
10-27 19:05:46.527: E/AndroidRuntime(339): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-27 19:05:46.527: E/AndroidRuntime(339): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
10-27 19:05:46.527: E/AndroidRuntime(339): ... 11 more

这是为什么?请帮忙!

最佳答案

在指定我们正在考虑的布局后,我们需要引用 EditText。我认为 setContentView() 可以帮助您了解我们正在考虑的布局。因此,请尝试在语句 setContentView() 之后使用 findViewById() 初始化 EditText。

替换

private EditText text = (EditText)findViewById(R.id.text);

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

text.setOnKeyListener(new TextMaker());
}

private EditText text ;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText)findViewById(R.id.text);
text.setOnKeyListener(new TextMaker());
}

关于java - 致命异常/无法在 Android 中实例化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13100694/

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