【问题标题】:Unfortunately, (My app) has stopped. Eclipse Android [duplicate]不幸的是,(我的应用程序)已停止。 Eclipse Android [重复]
【发布时间】:2015-09-07 12:44:15
【问题描述】:

我是 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>

【问题讨论】:

  • 你有 NullPointerException,这是更容易调试的异常之一
  • 请看下面的代码..

标签: java android eclipse adt


【解决方案1】:

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

这就是你得到 nullpointerexception 的原因。

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

【讨论】:

猜你喜欢
  • 2016-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多