不幸的是,(我的应用程序)已停止。日蚀 Android

Unfortunately, (My app) has stopped. Eclipse Android

我是 android 开发的初学者,我正在尝试构建一个简单的应用程序,但我在模拟器中遇到此错误。(不幸的是,(应用程序)意外停止了)。

LogCat

http://i.stack.imgur.com/VZhuL.png

package com.eebax.mjcalculator;

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.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
EditText one, two;
Button plus, minus, multiply, divide;
TextView showres;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    one = (EditText)findViewById(R.id.editText1);
    two = (EditText)findViewById(R.id.editText2);
    plus = (Button)findViewById(R.id.button1);
    minus = (Button)findViewById(R.id.button2);
    multiply = (Button)findViewById(R.id.button3);
    divide = (Button)findViewById(R.id.button4 );
    showres = (TextView)findViewById(R.id.textView3);

    plus.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            try {
                int n1 = Integer.parseInt(one.getText().toString());
                int n2 = Integer.parseInt(two.getText().toString());
                int sum = n1+n2;
                showres.setText(" "+sum);
            }
            catch(Exception e){
            }


        }
    });

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

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

}

清单

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

<uses-sdk
    android:minSdkVersion="10"
    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.eebax.mjcalculator.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>

在第 34 行的 MainActivity.java 中,您试图初始化一些在您的 xml 布局中不存在的小部件,您已在 setContentView(R.layout 中设置了它。 ...

这就是您获得空指针异常的原因。

编辑: 将您的 setContentView(R.layout.activity_main) 更改为 setContentView(R.layout.fragment_main)