java - Android 应用程序因添加布局而崩溃

标签 java android android-layout forceclose

我的 java 文件中有以下初始化:

Button btnCalc = (Button) findViewById(R.id.btnCalculate);
final Button btnClearWin = (Button) findViewById(R.id.btnClear);
final Button btnSaveTrip = (Button) findViewById(R.id.btnSave);
final EditText nameOfInf = (EditText)findViewById(R.id.etName);
final EditText tollAmount = (EditText)findViewById(R.id.etToll);
final EditText showLog = (EditText)findViewById(R.id.etShowLog);
showLog.setFocusable(false);

final View lineView = (View) findViewById(R.id.vwLine);
final TextView tvTotalLabel = (TextView) findViewById(R.id.tvTotal);
final TextView tvTotalAmountLabel = (TextView) findViewById(R.id.tvTotalAmount);

final TextView tvNameLabel = (TextView) findViewById(R.id.tvName);
final TextView tvTollLabel = (TextView) findViewById(R.id.tvToll);

final RadioGroup rgTypeOfInf = (RadioGroup) findViewById(R.id.rgType);
final RadioGroup rgTypeOfTrip = (RadioGroup) findViewById(R.id.rgTripType);

最初一切正常,直到我将一些对象移动到一个不同的布局,该布局不再位于 main 布局文件中,现在我的应用程序 FC 打开时。

以下是不同的布局,result.xml。我必须单独初始化它们吗?

final EditText showLog = (EditText)findViewById(R.id.etShowLog);
showLog.setFocusable(false);
final Button btnClearWin = (Button) findViewById(R.id.btnClear);
final Button btnSaveTrip = (Button) findViewById(R.id.btnSave);

当我注释掉以上四行时,应用程序打开得很好。

日志:

07-25 10:27:27.955: E/AndroidRuntime(13791): FATAL EXCEPTION: main
07-25 10:27:27.955: E/AndroidRuntime(13791): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.testing/com.test.testing.MainActivity}: java.lang.NullPointerException
07-25 10:27:27.955: E/AndroidRuntime(13791):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
07-25 10:27:27.955: E/AndroidRuntime(13791):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
07-25 10:27:27.955: E/AndroidRuntime(13791):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
07-25 10:27:27.955: E/AndroidRuntime(13791):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
07-25 10:27:27.955: E/AndroidRuntime(13791):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 10:27:27.955: E/AndroidRuntime(13791):    at android.os.Looper.loop(Looper.java:137)
07-25 10:27:27.955: E/AndroidRuntime(13791):    at android.app.ActivityThread.main(ActivityThread.java:5195)
07-25 10:27:27.955: E/AndroidRuntime(13791):    at java.lang.reflect.Method.invokeNative(Native Method)
07-25 10:27:27.955: E/AndroidRuntime(13791):    at java.lang.reflect.Method.invoke(Method.java:511)
07-25 10:27:27.955: E/AndroidRuntime(13791):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-25 10:27:27.955: E/AndroidRuntime(13791):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
07-25 10:27:27.955: E/AndroidRuntime(13791):    at dalvik.system.NativeStart.main(Native Method)
07-25 10:27:27.955: E/AndroidRuntime(13791): Caused by: java.lang.NullPointerException
07-25 10:27:27.955: E/AndroidRuntime(13791):    at com.test.testing.MainActivity.onCreate(MainActivity.java:51)
07-25 10:27:27.955: E/AndroidRuntime(13791):    at android.app.Activity.performCreate(Activity.java:5104)
07-25 10:27:27.955: E/AndroidRuntime(13791):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-25 10:27:27.955: E/AndroidRuntime(13791):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
07-25 10:27:27.955: E/AndroidRuntime(13791):    ... 11 more
07-25 10:27:27.963: W/ActivityManager(381):   Force finishing activity com.test.testing/.MainActivity
07-25 10:27:28.517: W/ActivityManager(381): Activity pause timeout for ActivityRecord{4163fe28 u0 com.test.testing/.MainActivity}
07-25 10:27:28.666: I/qtaguid(381): Failed write_ctrl(s 0 10116) res=-1 errno=1
07-25 10:27:28.666: W/NetworkManagementSocketTagger(381): setKernelCountSet(10116, 0) failed with errno -1
07-25 10:27:38.666: W/ActivityManager(381): Activity destroy timeout for ActivityRecord{4163fe28 u0 com.test.testing/.MainActivity}
07-25 10:28:00.166: D/dalvikvm(2069): GC_CONCURRENT freed 3198K, 52% free 13534K/27904K, paused 3ms+3ms, total 29ms
07-25 10:28:00.166: D/dalvikvm(2069): WAIT_FOR_CONCURRENT_GC blocked 23ms

最佳答案

如果您想使用主布局以外的其他布局的 View ,则需要扩充该布局。如果不inflate直接使用它的views就会给NPE

示例代码:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout mFrame = (RelativeLayout)inflater.inflate(R.layout.results, null);   
//Now you can use/reference its resources/views etc.
final EditText showLog = (EditText)mFrame.findViewById(R.id.etShowLog);
showLog.setFocusable(false);
final Button btnClearWin = (Button) mFrame.findViewById(R.id.btnClear);
final Button btnSaveTrip = (Button) mFrame.findViewById(R.id.btnSave);

关于java - Android 应用程序因添加布局而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17860750/

相关文章:

java - 如何在使用 Maven 构建期间读取来自 system.out.println 的警告

java - 从Spring Webflow调用函数存储在数据库(MySQL)中

java - 将 JSF Managed Beans 移动到 Spring beans

android - 何时以及如何退出文件上传服务

java - Android 工具栏不显示图标和标题

java - 如何在列表单元格中添加组件?

java - 显示 SwarmConnect 的排行榜

用于绑定(bind)通用 View 的 Android Kotlin 扩展

java - 如何解决 MediaPlayer 错误 "Can' t 播放此视频”

android - 是否可以在水平方向的布局编辑器中显示 RecyclerView 预览?