空指针异常日志Cat错误

时间:2014-09-14 00:53:18

标签: java android logcat

在我的项目文件和源代码中没有错误。没有构建错误,没有项目错误,没有语法错误......由于某些原因,当我在模拟器上运行我的应用程序时,它崩溃了。 这里是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

}
}

登录CAT

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

2 个答案:

答案 0 :(得分:3)

基于您的stacktrace和TabHost.addTab源代码。您在TabWidget内错过了@android:id/tabs TabHost

如果您想使用TabHost,您必须拥有此原型的布局:

<?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_TABSActionBarActivity一起使用并使用Fragments

如果要迁移,请查看本教程:http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

答案 1 :(得分:0)

它看起来像一个空指针异常。

根据您的日志,如果我是您,我会好好看看com.overworldinnovations.datatool.MainActivity.onCreate(MainActivity.java:41)(第41行onCreate()的{​​{1}}方法)

您的MainActivity方法中可能存在一些问题,即您尝试使用的某些变量为onCreate()。 (我不能确定,因为我不知道null的第41行上的内容是什么

根据空指针异常的Java Documentation

  

当应用程序在某个情况下尝试使用null时抛出   对象是必需的。其中包括:

     
      
  • 调用a的实例方法   null对象。
  •   
  • 访问或修改空对象的字段。
  •   
  • 将null的长度视为数组。
  •   
  • 访问或修改null的插槽,就像它是一个数组一样。
  •   
  • 抛出null,就好像它是一个Throwable值。
  •