在我的项目文件和源代码中没有错误。没有构建错误,没有项目错误,没有语法错误......并且由于某种原因,当我在模拟器上运行我的应用程序时它崩溃了。 这是 Log Cat 信息 - 我已经搜索了所有内容,但编译后没有标记任何错误,我很难弄清楚应用程序崩溃的原因。
提前致谢!
这是activity_main.xml的源代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.overworldinnovations.datatool.MainActivity"
tools:ignore="MergeRootFrame" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonConvert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="49dp"
android:text="Convert" />
<EditText
android:id="@+id/editDecimal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView3"
android:layout_marginBottom="52dp"
android:layout_toRightOf="@+id/textView2"
android:ems="10"
android:gravity="center_vertical|right"
android:inputType="numberSigned"
android:maxLength="9" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:text="Enter the decimal value to be converted :)"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editDecimal"
android:layout_alignParentLeft="true"
android:text="Decimal"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/buttonConvert"
android:layout_alignParentLeft="true"
android:layout_marginBottom="94dp"
android:text="Binary"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editBinary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/editDecimal"
android:ems="10"
android:inputType="number" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</RelativeLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
</RelativeLayout>
这是MainActivity.java的源代码
package com.overworldinnovations.datatool;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create the TabHost that will contain the Tabs
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabSpec tab3 = tabHost.newTabSpec("Third tab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,Tab1Activity.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this,Tab2Activity.class));
tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this,Tab3Activity.class));
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
日志猫
09-14 00:39:23.213: D/AndroidRuntime(774): Shutting down VM
09-14 00:39:23.213: W/dalvikvm(774): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-14 00:39:23.333: E/AndroidRuntime(774): FATAL EXCEPTION: main
09-14 00:39:23.333: E/AndroidRuntime(774): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.overworldinnovations.datatool/com.overworldinnovations.datatool.MainActivity}: java.lang.NullPointerException
09-14 00:39:23.333: E/AndroidRuntime(774): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-14 00:39:23.333: E/AndroidRuntime(774): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-14 00:39:23.333: E/AndroidRuntime(774): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-14 00:39:23.333: E/AndroidRuntime(774): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-14 00:39:23.333: E/AndroidRuntime(774): at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 00:39:23.333: E/AndroidRuntime(774): at android.os.Looper.loop(Looper.java:137)
09-14 00:39:23.333: E/AndroidRuntime(774): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-14 00:39:23.333: E/AndroidRuntime(774): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 00:39:23.333: E/AndroidRuntime(774): at java.lang.reflect.Method.invoke(Method.java:511)
09-14 00:39:23.333: E/AndroidRuntime(774): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-14 00:39:23.333: E/AndroidRuntime(774): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-14 00:39:23.333: E/AndroidRuntime(774): at dalvik.system.NativeStart.main(Native Method)
09-14 00:39:23.333: E/AndroidRuntime(774): Caused by: java.lang.NullPointerException
09-14 00:39:23.333: E/AndroidRuntime(774): at android.widget.TabHost.addTab(TabHost.java:236)
09-14 00:39:23.333: E/AndroidRuntime(774): at com.overworldinnovations.datatool.MainActivity.onCreate(MainActivity.java:41)
09-14 00:39:23.333: E/AndroidRuntime(774): at android.app.Activity.performCreate(Activity.java:5104)
09-14 00:39:23.333: E/AndroidRuntime(774): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-14 00:39:23.333: E/AndroidRuntime(774): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-14 00:39:23.333: E/AndroidRuntime(774): ... 11 more
09-14 00:39:23.353: D/dalvikvm(774): GC_CONCURRENT freed 118K, 9% free 2704K/2944K, paused 8ms+65ms, total 166ms
09-14 00:43:31.776: D/AndroidRuntime(817): Shutting down VM
09-14 00:43:31.776: W/dalvikvm(817): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-14 00:43:31.836: E/AndroidRuntime(817): FATAL EXCEPTION: main
09-14 00:43:31.836: E/AndroidRuntime(817): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.overworldinnovations.datatool/com.overworldinnovations.datatool.MainActivity}: java.lang.NullPointerException
09-14 00:43:31.836: E/AndroidRuntime(817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-14 00:43:31.836: E/AndroidRuntime(817): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-14 00:43:31.836: E/AndroidRuntime(817): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-14 00:43:31.836: E/AndroidRuntime(817): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-14 00:43:31.836: E/AndroidRuntime(817): at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 00:43:31.836: E/AndroidRuntime(817): at android.os.Looper.loop(Looper.java:137)
09-14 00:43:31.836: E/AndroidRuntime(817): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-14 00:43:31.836: E/AndroidRuntime(817): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 00:43:31.836: E/AndroidRuntime(817): at java.lang.reflect.Method.invoke(Method.java:511)
09-14 00:43:31.836: E/AndroidRuntime(817): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-14 00:43:31.836: E/AndroidRuntime(817): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-14 00:43:31.836: E/AndroidRuntime(817): at dalvik.system.NativeStart.main(Native Method)
09-14 00:43:31.836: E/AndroidRuntime(817): Caused by: java.lang.NullPointerException
09-14 00:43:31.836: E/AndroidRuntime(817): at android.widget.TabHost.addTab(TabHost.java:236)
09-14 00:43:31.836: E/AndroidRuntime(817): at com.overworldinnovations.datatool.MainActivity.onCreate(MainActivity.java:41)
09-14 00:43:31.836: E/AndroidRuntime(817): at android.app.Activity.performCreate(Activity.java:5104)
09-14 00:43:31.836: E/AndroidRuntime(817): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-14 00:43:31.836: E/AndroidRuntime(817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-14 00:43:31.836: E/AndroidRuntime(817): ... 11 more
09-14 00:43:31.976: D/dalvikvm(817): GC_CONCURRENT freed 109K, 8% free 2678K/2908K, paused 9ms+43ms, total 187ms
最佳答案
基于您的堆栈跟踪和 TabHost.addTab
源代码。您的 TabHost
中缺少 id 为 @android:id/tabs
的 TabWidget
。
如果您想使用 TabHost
,您必须具有具有以下原型(prototype)的布局:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
别忘了调用mTabHost.setup();
在添加标签之前!喜欢:
TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
tabHost.setup();
// Add your tabs now
编辑:因为您使用的是 Tab.setContent(Intent i)
,所以必须调用 TabHost.setup(LocalActivityManager ActivityGroup)
而不是TabHost.setup()
.
所以我会是:
TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
tabHost.setup(this);
// Add your tabs now
更多详细信息:http://blog.vogella.com/2011/05/17/android-tabs/
但是 TabHost
已“弃用”,我建议您将 ActionBar.NAVIGATION_MODE_TABS
与 ActionBarActivity
一起使用并使用 Fragments
.
如果您想迁移,请查看本教程:http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
关于java - 空指针异常日志 Cat Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25829139/