我是android开发的初学者,我正在尝试构建一个简单的应用程序,但我在模拟器中收到此错误。(不幸的是,(App)意外停止了。)
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>
答案 0 :(得分:0)
在第34行的MainActivity.java中,您正在尝试初始化您在setContentView中设置的xml布局中不存在的窗口小部件(R.layout ....
那就是你为什么要提出nullpointerexception。
编辑:将您的setContentView(R.layout.activity_main)
更改为setContentView(R.layout.fragment_main)