Android开发者应用程序说应用程序意外停止

时间:2014-06-16 08:32:31

标签: java android xml eclipse

嗨,我是Android应用程序的初学者。我正在使用一些教程编写计算器代码。但是应用程序无法运行并且一直说应用程序意外停止。我附上了我的密码。有人能找出错误。

我的MainActivity.java

package com.example.mycalc;

import android.support.v7.app.ActionBarActivity;
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.TextView;

public class MainActivity extends ActionBarActivity {
TextView disp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    disp=(TextView)findViewById(R.id.textView1);
    disp.setText("0");
}

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

}

我的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mycalc"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.mycalc.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
   </application>

 </manifest>

log-cat错误:

06-16 14:14:48.585: D/AndroidRuntime(5199): Shutting down VM
06-16 14:14:48.585: W/dalvikvm(5199): threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-16 14:14:48.585: E/AndroidRuntime(5199): FATAL EXCEPTION: main
06-16 14:14:48.585: E/AndroidRuntime(5199): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mycalc/com.example.mycalc.MainActivity}: java.lang.NullPointerException
06-16 14:14:48.585: E/AndroidRuntime(5199):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
06-16 14:14:48.585: E/AndroidRuntime(5199):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
06-16 14:14:48.585: E/AndroidRuntime(5199):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-16 14:14:48.585: E/AndroidRuntime(5199):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
06-16 14:14:48.585: E/AndroidRuntime(5199):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 14:14:48.585: E/AndroidRuntime(5199):     at android.os.Looper.loop(Looper.java:130)
06-16 14:14:48.585: E/AndroidRuntime(5199):     at android.app.ActivityThread.main(ActivityThread.java:3687)
06-16 14:14:48.585: E/AndroidRuntime(5199):     at java.lang.reflect.Method.invokeNative(Native Method)
06-16 14:14:48.585: E/AndroidRuntime(5199):     at java.lang.reflect.Method.invoke(Method.java:507)
06-16 14:14:48.585: E/AndroidRuntime(5199):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-16 14:14:48.585: E/AndroidRuntime(5199):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-16 14:14:48.585: E/AndroidRuntime(5199):     at dalvik.system.NativeStart.main(Native Method)
06-16 14:14:48.585: E/AndroidRuntime(5199): Caused by: java.lang.NullPointerException
06-16 14:14:48.585: E/AndroidRuntime(5199):     at com.example.mycalc.MainActivity.onCreate(MainActivity.java:21)
06-16 14:14:48.585: E/AndroidRuntime(5199):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-16 14:14:48.585: E/AndroidRuntime(5199):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
06-16 14:14:48.585: E/AndroidRuntime(5199):     ... 11 more

2 个答案:

答案 0 :(得分:2)

你应该在片段中完成你的工作:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container,
            false);
    TextView disp = (TextView)rootView.findViewById(R.id.textView1);
    disp.setText("0");
    return rootView;
}

答案 1 :(得分:0)

试试这个......

public class MainActivity extends Activity { // changed extended activity
TextView disp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);//changed layout name
    disp=(TextView)findViewById(R.id.textView1);
    disp.setText("0");
   }
}

并删除那里的所有其他受保护的方法..你没有使用任何人......