ADT简单应用程序 - 致命异常

时间:2014-05-16 16:48:05

标签: java android eclipse adt

我刚刚开始使用Android和Java,我刚刚创建了一个带有几个textview,一个按钮和一个edittext的简单应用程序。

当我运行应用程序时,我得到一个错误说:不幸的是,FirstApp已停止。 我熟悉编程,并想知道我是否错过了一些库,或者ADT是否配置错误。

我尝试清理并重建,右键单击Package explorer和Android Tools中的项目 - >添加支持库,重新启动模拟器,重新启动ADT并重新启动计算机。不知道这是什么。

你们中间有人看错了吗?

MainActivity.java导入

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.os.Build;

MainActivity.java

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


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

    btnCalc.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            EditText number = (EditText)findViewById(R.id.num);
            TextView display = (TextView)findViewById(R.id.display);

            double num = Double.parseDouble(number.getText().toString());
            num = num * 5;
            display.setText(num + "");
        }
    });




    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.danlevi.testproject.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/lblTitle" />

<EditText
    android:id="@+id/num"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:ems="10"
    android:inputType="numberDecimal|numberSigned" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/btnCalc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/num"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:text="@string/btnCalc" />

<TextView
    android:id="@+id/display"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/btnCalc"
    android:layout_marginTop="16dp" />

</RelativeLayout>

控制台

[2014-05-16 18:33:16 - TestProject] Dx 
trouble writing output: already prepared
[2014-05-16 18:33:17 - TestProject] ------------------------------
[2014-05-16 18:33:17 - TestProject] Android Launch!
[2014-05-16 18:33:17 - TestProject] adb is running normally.
[2014-05-16 18:33:17 - TestProject] Performing com.danlevi.testproject.MainActivity activity launch
[2014-05-16 18:33:17 - TestProject] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'Android'
[2014-05-16 18:33:17 - TestProject] Uploading TestProject.apk onto device 'emulator-5554'
[2014-05-16 18:33:18 - TestProject] Installing TestProject.apk...
[2014-05-16 18:33:30 - TestProject] Success!
[2014-05-16 18:33:30 - TestProject] Starting activity com.danlevi.testproject.MainActivity on device emulator-5554
[2014-05-16 18:33:33 - TestProject] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.danlevi.testproject/.MainActivity }

logcat的

05-16 12:33:37.412: D/AndroidRuntime(1579): Shutting down VM
05-16 12:33:37.412: W/dalvikvm(1579): threadid=1: thread exiting with uncaught exception (group=0xb2b08ba8)
05-16 12:33:37.432: E/AndroidRuntime(1579): FATAL EXCEPTION: main
05-16 12:33:37.432: E/AndroidRuntime(1579): Process: com.danlevi.testproject, PID: 1579
05-16 12:33:37.432: E/AndroidRuntime(1579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.danlevi.testproject/com.danlevi.testproject.MainActivity}: java.lang.NullPointerException
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.os.Looper.loop(Looper.java:136)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at java.lang.reflect.Method.invokeNative(Native Method)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at java.lang.reflect.Method.invoke(Method.java:515)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at dalvik.system.NativeStart.main(Native Method)
05-16 12:33:37.432: E/AndroidRuntime(1579): Caused by: java.lang.NullPointerException
05-16 12:33:37.432: E/AndroidRuntime(1579):     at com.danlevi.testproject.MainActivity.onCreate(MainActivity.java:25)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.Activity.performCreate(Activity.java:5231)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-16 12:33:37.432: E/AndroidRuntime(1579):     ... 11 more
05-16 12:33:44.352: I/Process(1579): Sending signal. PID: 1579 SIG: 9

1 个答案:

答案 0 :(得分:1)

这是因为 findViewById() activity_main 布局中搜索,而按钮位于片段的布局 fragment_main 中。

在片段的onCreateView()方法中移动该段代码:

//...

View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button buttonClick = (Button)rootView.findViewById(R.id.button);
buttonClick.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        onButtonClick((Button) view);
    }
});

请注意,现在您可以通过rootView view访问它:

Button buttonClick = (Button)rootView.findViewById(R.id.button);

否则你会再次NullPointerException

在您的代码中,移动

changer = (Button) findViewById(R.id.change1);
changeme = (TextView) findViewById (R.id.changeme1);
changer.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
changeme.setText("Changed");
}
});

到片段的onCreateView()方法