我在模拟器中运行应用程序时遇到问题。每当我尝试运行应用程序时,它都会向我提供消息"不幸的是,你好已经停止了工作"。我找到了类似问题的答案,但没有一个帮助过我。我的代码是:
主要活动.java
package com.example.hello;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button btn1 = (Button)findViewById(R.id.button1);
private Button btn2=(Button)findViewById(R.id.button2);
private TextView ans=(TextView)findViewById(R.id.answer);
private EditText first =(EditText)findViewById(R.id.firstnotxt);
private EditText second =(EditText)findViewById(R.id.firstnotxt);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listener();
}
private void listener()
{
btn1.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
String txt1=first.getText().toString();
int no1=Integer.parseInt(txt1);
String txt2=second.getText().toString();
int no2=Integer.parseInt(txt2);
int add=no1+no2;
ans.setText(""+add);
}
}
);
btn2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String txt1=first.getText().toString();
int no1=Integer.parseInt(txt1);
String txt2=second.getText().toString();
int no2=Integer.parseInt(txt2);
int sub=no1-no2;
ans.setText(""+sub);
}
}
);
}
@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;
}
}
布局(activity_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.example.hello.MainActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/firstno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:text="@string/FirstNumber"
android:textColor="#ff0000"
android:textSize="15sp" />
<EditText
android:id="@+id/firstnotxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_toEndOf="@+id/firstno"
android:layout_toRightOf="@+id/firstno"
android:inputType="numberDecimal"
/>
<TextView
android:id="@+id/secondno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/firstno"
android:text="@string/SecondNumber"
android:textColor="#ff0000"
android:textSize="15sp" />
<EditText
android:id="@+id/secondnotxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/secondno"
android:layout_alignBottom="@+id/secondno"
android:layout_toEndOf="@+id/firstno"
android:layout_toRightOf="@+id/secondno"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/secondno"
android:clickable="true"
android:text="@string/Add" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:clickable="true"
android:text="@string/Subtract" />
<TextView
android:id="@+id/answer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_marginTop="54dp" />
</RelativeLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
logcat的
06-03 05:00:10.356: E/Trace(2903): error opening trace file: No such file or directory (2)
06-03 05:00:10.818: D/AndroidRuntime(2903): Shutting down VM
06-03 05:00:10.818: W/dalvikvm(2903): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-03 05:00:10.916: E/AndroidRuntime(2903): FATAL EXCEPTION: main
06-03 05:00:10.916: E/AndroidRuntime(2903): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hello/com.example.hello.MainActivity}: java.lang.NullPointerException
06-03 05:00:10.916: E/AndroidRuntime(2903): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
06-03 05:00:10.916: E/AndroidRuntime(2903): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-03 05:00:10.916: E/AndroidRuntime(2903): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-03 05:00:10.916: E/AndroidRuntime(2903): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-03 05:00:10.916: E/AndroidRuntime(2903): at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 05:00:10.916: E/AndroidRuntime(2903): at android.os.Looper.loop(Looper.java:137)
06-03 05:00:10.916: E/AndroidRuntime(2903): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-03 05:00:10.916: E/AndroidRuntime(2903): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 05:00:10.916: E/AndroidRuntime(2903): at java.lang.reflect.Method.invoke(Method.java:511)
06-03 05:00:10.916: E/AndroidRuntime(2903): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-03 05:00:10.916: E/AndroidRuntime(2903): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-03 05:00:10.916: E/AndroidRuntime(2903): at dalvik.system.NativeStart.main(Native Method)
06-03 05:00:10.916: E/AndroidRuntime(2903): Caused by: java.lang.NullPointerException
06-03 05:00:10.916: E/AndroidRuntime(2903): at android.app.Activity.findViewById(Activity.java:1839)
06-03 05:00:10.916: E/AndroidRuntime(2903): at com.example.hello.MainActivity.<init>(MainActivity.java:16)
06-03 05:00:10.916: E/AndroidRuntime(2903): at java.lang.Class.newInstanceImpl(Native Method)
06-03 05:00:10.916: E/AndroidRuntime(2903): at java.lang.Class.newInstance(Class.java:1319)
06-03 05:00:10.916: E/AndroidRuntime(2903): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
06-03 05:00:10.916: E/AndroidRuntime(2903): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
06-03 05:00:10.916: E/AndroidRuntime(2903): ... 11 more
06-03 05:00:14.086: I/Process(2903): Sending signal. PID: 2903 SIG: 9
06-03 05:01:01.957: E/Trace(2965): error opening trace file: No such file or directory (2)
06-03 05:01:02.247: D/AndroidRuntime(2965): Shutting down VM
06-03 05:01:02.296: W/dalvikvm(2965): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-03 05:01:02.336: E/AndroidRuntime(2965): FATAL EXCEPTION: main
06-03 05:01:02.336: E/AndroidRuntime(2965): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hello/com.example.hello.MainActivity}: java.lang.NullPointerException
06-03 05:01:02.336: E/AndroidRuntime(2965): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
06-03 05:01:02.336: E/AndroidRuntime(2965): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-03 05:01:02.336: E/AndroidRuntime(2965): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-03 05:01:02.336: E/AndroidRuntime(2965): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-03 05:01:02.336: E/AndroidRuntime(2965): at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 05:01:02.336: E/AndroidRuntime(2965): at android.os.Looper.loop(Looper.java:137)
06-03 05:01:02.336: E/AndroidRuntime(2965): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-03 05:01:02.336: E/AndroidRuntime(2965): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 05:01:02.336: E/AndroidRuntime(2965): at java.lang.reflect.Method.invoke(Method.java:511)
06-03 05:01:02.336: E/AndroidRuntime(2965): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-03 05:01:02.336: E/AndroidRuntime(2965): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-03 05:01:02.336: E/AndroidRuntime(2965): at dalvik.system.NativeStart.main(Native Method)
06-03 05:01:02.336: E/AndroidRuntime(2965): Caused by: java.lang.NullPointerException
06-03 05:01:02.336: E/AndroidRuntime(2965): at android.app.Activity.findViewById(Activity.java:1839)
06-03 05:01:02.336: E/AndroidRuntime(2965): at com.example.hello.MainActivity.<init>(MainActivity.java:16)
06-03 05:01:02.336: E/AndroidRuntime(2965): at java.lang.Class.newInstanceImpl(Native Method)
06-03 05:01:02.336: E/AndroidRuntime(2965): at java.lang.Class.newInstance(Class.java:1319)
06-03 05:01:02.336: E/AndroidRuntime(2965): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
06-03 05:01:02.336: E/AndroidRuntime(2965): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
06-03 05:01:02.336: E/AndroidRuntime(2965): ... 11 more
答案 0 :(得分:1)
您首先需要setContentView()
,然后才能获得要分配或添加听众的组件。
试试这个
public class MainActivity extends Activity {
private Button btn1, btn2;
private TextView ans;
private EditText first, second;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.button1);
btn2 = (Button)findViewById(R.id.button2);
ans = (TextView)findViewById(R.id.answer);
first = (EditText)findViewById(R.id.firstnotxt);
second = (EditText)findViewById(R.id.firstnotxt);
listener();
}
答案 1 :(得分:0)
您无法在Views
方法之前初始化onCreate()
,否则会抛出空指针异常,因为最初它将为null,因为视图始终在onCreate()
方法中初始化。因为第一次super.onCreate()
来电。所以你需要移动所有这些
private Button btn1 = (Button)findViewById(R.id.button1);
private Button btn2=(Button)findViewById(R.id.button2);
private TextView ans=(TextView)findViewById(R.id.answer);
private EditText first =(EditText)findViewById(R.id.firstnotxt);
private EditText second =(EditText)findViewById(R.id.firstnotxt);
在setContentView(R.layout.activity_main);
之后