【问题标题】:Android App crashes due to adding a layout由于添加布局,Android 应用程序崩溃
【发布时间】:2013-07-25 14:22:49
【问题描述】:

我的 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);

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

Logcat:

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

【问题讨论】:

  • “除了主要”是什么意思?
  • 请同时发布 logcat 输出
  • 您可以初始化设置为活动的布局 xml 中的视图。如果它在不同的 xml 中并且你尝试初始化你会得到 NPE。
  • 那么如何从我的java文件中的result布局初始化一个按钮呢?
  • 您需要为该布局 result.xml 充气,然后使用该充气视图对象来初始化您的视图。

标签: java android android-layout forceclose


【解决方案1】:

如果您想使用除主布局之外的其他布局的视图,则需要对该布局进行膨胀。如果你不膨胀并直接使用它的视图,它将给 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);

【讨论】:

  • 我会在 onCreate() 方法下添加它吗?
  • 是的,你可以,在使用该布局中定义的视图之前确保它。我已经更新了代码。还要确保为 mFrame 使用适当的布局。如果它的LinearLayout 将RelativeLayout 更改为LinearLayout
  • 如果它使用多个布局怎么办?相对和线性,不是我使用两个。
  • 每个android布局文件只能有一个根/主布局。根/主布局里面可以有多个布局。因此,您使用主/根布局扩展布局 .xml 文件。
  • 啊啊啊啊……我明白了。谢谢!如果你不介意可以看看这个:stackoverflow.com/questions/17861585/…
【解决方案2】:

我从你的代码中看到的“也许”只有这段代码会产生错误:

showLog.setFocusable(false)

在设置任何值之前先尝试检查是否为空

if (showLog != null) showLog.setFocusable(false);

【讨论】:

  • 要完成分配,您需要在您膨胀适当布局的活动上设置 showLog.setFocusable(false)。因此你会做对的。
  • 我还有另一个问题。问题是,当单击当前布局文件时,我试图在不同的布局文件中显示一些值。你有我可以发给你我的 Java 代码的电子邮件吗?
  • 我相信如果您有更多问题,如果您将其发布在 SO 中会更合适和公平,因为任何人都可以提供建议或答案,对吗? :)
【解决方案3】:

在移动或重命名文件或资源后,您必须为新的引用清理和构建项目。

【讨论】:

  • 这与 NPE 无关。
  • 当然,如果对象不能被初始化,你会得到一个 NPE
【解决方案4】:

对于使用 setcontentview(layout) 时的活动。只有那个 xml 布局视图可以引用您的活动。对于其他 xml,您必须将该 XML 膨胀为视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-20
    • 2016-04-06
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多