空指针异常和应用程序崩溃

时间:2014-09-14 06:10:30

标签: java android eclipse nullpointerexception

所有东西都能完美地编译,但是当我在eclipse中运行模拟器时 - 应用程序崩溃了。这里的Log Cat。我也发布了main_activity.java!如果您需要任何其他源代码,请告诉我:)感谢任何帮助:)

登录CAT

09-14 05:56:56.363: D/dalvikvm(771): Not late-enabling CheckJNI (already on)
09-14 05:56:57.613: E/Trace(771): error opening trace file: No such file or directory (2)
09-14 05:56:58.222: D/dalvikvm(771): GC_FOR_ALLOC freed 85K, 8% free 2560K/2768K, paused 32ms, total 39ms
09-14 05:56:58.222: I/dalvikvm-heap(771): Grow heap (frag case) to 3.221MB for 635812-byte allocation
09-14 05:56:58.262: D/dalvikvm(771): GC_FOR_ALLOC freed 2K, 7% free 3179K/3392K, paused 36ms, total 36ms
09-14 05:56:58.312: D/AndroidRuntime(771): Shutting down VM
09-14 05:56:58.312: W/dalvikvm(771): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-14 05:56:58.322: D/dalvikvm(771): GC_CONCURRENT freed 16K, 6% free 3188K/3392K, paused 24ms+12ms, total 56ms
09-14 05:56:58.332: E/AndroidRuntime(771): FATAL EXCEPTION: main
09-14 05:56:58.332: E/AndroidRuntime(771): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.overworldinnovations.datatool/com.overworldinnovations.datatool.MainActivity}: java.lang.NullPointerException
09-14 05:56:58.332: E/AndroidRuntime(771):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-14 05:56:58.332: E/AndroidRuntime(771):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-14 05:56:58.332: E/AndroidRuntime(771):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-14 05:56:58.332: E/AndroidRuntime(771):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-14 05:56:58.332: E/AndroidRuntime(771):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 05:56:58.332: E/AndroidRuntime(771):  at android.os.Looper.loop(Looper.java:137)
09-14 05:56:58.332: E/AndroidRuntime(771):  at android.app.ActivityThread.main(ActivityThread.java:5041)
09-14 05:56:58.332: E/AndroidRuntime(771):  at java.lang.reflect.Method.invokeNative(Native Method)
09-14 05:56:58.332: E/AndroidRuntime(771):  at java.lang.reflect.Method.invoke(Method.java:511)
09-14 05:56:58.332: E/AndroidRuntime(771):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-14 05:56:58.332: E/AndroidRuntime(771):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-14 05:56:58.332: E/AndroidRuntime(771):  at dalvik.system.NativeStart.main(Native Method)
09-14 05:56:58.332: E/AndroidRuntime(771): Caused by: java.lang.NullPointerException
09-14 05:56:58.332: E/AndroidRuntime(771):  at com.overworldinnovations.datatool.MainActivity.onCreate(MainActivity.java:36)
09-14 05:56:58.332: E/AndroidRuntime(771):  at android.app.Activity.performCreate(Activity.java:5104)
09-14 05:56:58.332: E/AndroidRuntime(771):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-14 05:56:58.332: E/AndroidRuntime(771):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-14 05:56:58.332: E/AndroidRuntime(771):  ... 11 more

这是MainActivity.java

package com.overworldinnovations.datatool;

import info.datatool.tabsswipe.adapter.TabsPagerAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Convert", "Data Ranges", "" };

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

    final EditText editDecimal = (EditText) findViewById(R.id.editDecimal);

    final EditText editBinary = (EditText) findViewById(R.id.editBinary);

    Button buttonConvert = (Button) findViewById(R.id.buttonConvert);

    buttonConvert.setOnClickListener(new OnClickListener() {



        public void onClick(View arg0) {



            int decimal = Integer.valueOf(editDecimal.getText().toString());

            editBinary.setText(Integer.toBinaryString(decimal));

        }

    });

    // Initialization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}
}

fragment_convert.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ff8400" >
<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>

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

3 个答案:

答案 0 :(得分:1)

你显然正在使用碎片。您需要将与片段相关的逻辑与与活动相关的逻辑分开。特别是,活动的onCreate()方法中没有任何视图可用。相反,您需要将所有findViewById()次调用移至片段的onCreateView()方法。这意味着您还必须移动所有依赖于这些视图的逻辑。

应该保留在您的活动onCreate()中的唯一逻辑是处理操作栏中标签的代码。

答案 1 :(得分:0)

buttonConvert为null。发布您的activity_main.xml 你确定你有一个带有id&#34; buttonConvert&#34;的按钮。在您的activity_main.xml?

更新: 根据您的评论,您试图从活动中访问一个显然不存在的视图。您应该在相应的片段中获得引用而不是活动

答案 2 :(得分:0)

错误消息清楚地显示问题所在。

09-14 05:56:58.332: E/AndroidRuntime(771):  at com.overworldinnovations.datatool.MainActivity.onCreate(MainActivity.java:36)

buttonConvert.setOnClickListener(new OnClickListener() {

您已设置布局&#39; activity_main &#39;那里(onCreate方法)作为布局。然后你得到按钮:

Button buttonConvert = (Button) findViewById(R.id.buttonConvert);

但您在buttonConvert文件中提到过fragment_convert.xml,而不是在activity_main.xml文件中提及。