我是Android开发的新手,我遇到了一个问题,即应用程序本身不幸停止了运行,但没有错误。
这是我的代码。
MainActivity.java
package com.mobilebasedsignlanguage;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button conv = (Button)findViewById(R.id.btn_conv);
Button inst = (Button)findViewById(R.id.btn_ins);
Button view = (Button)findViewById(R.id.btn_view);
Button ext = (Button)findViewById(R.id.btn_ex);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in = new Intent(MainActivity.this, viewmenu.class);
startActivity(in);
}
});
}
@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=".MainActivity">
<Button
android:id="@+id/btn_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_ex"
android:layout_alignLeft="@+id/btn_conv"
android:layout_alignRight="@+id/btn_conv"
android:layout_marginBottom="60dp"
android:text="View" />
<Button
android:id="@+id/btn_conv"
android:layout_width="250sp"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_ins"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp"
android:text="Convert" />
<Button
android:id="@+id/btn_ex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_view"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/btn_view"
android:layout_marginBottom="112dp"
android:text="Exit" />
<Button
android:id="@+id/btn_ins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_view"
android:layout_alignLeft="@+id/btn_view"
android:layout_alignRight="@+id/btn_conv"
android:layout_marginBottom="46dp"
android:text="Instruction" />
</RelativeLayout>
这是第二个屏幕。
viewmenu.java
package com.mobilebasedsignlanguage;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class viewmenu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.viewlayout);
Button alphaconv = (Button)findViewById(R.id.bt_Alphabet);
alphaconv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent alpha = new Intent(viewmenu.this, alphaconv.class);
startActivity(alpha);
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobilebasedsignlanguage"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mobilebasedsignlanguage.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>
<activity android:name="com.mobilebasedsignlanguage.viewmenu"></activity>
</application>
</manifest>
这是原木猫
02-18 10:23:32.314: E/Trace(2192): error opening trace file: No such file or directory (2)
02-18 10:23:32.695: D/AndroidRuntime(2192): Shutting down VM
02-18 10:23:32.695: W/dalvikvm(2192): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
02-18 10:23:32.794: E/AndroidRuntime(2192): FATAL EXCEPTION: main
02-18 10:23:32.794: E/AndroidRuntime(2192): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mobilebasedsignlanguage/com.mobilebasedsignlanguage.MainActivity}: java.lang.NullPointerException
02-18 10:23:32.794: E/AndroidRuntime(2192): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
02-18 10:23:32.794: E/AndroidRuntime(2192): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-18 10:23:32.794: E/AndroidRuntime(2192): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-18 10:23:32.794: E/AndroidRuntime(2192): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-18 10:23:32.794: E/AndroidRuntime(2192): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 10:23:32.794: E/AndroidRuntime(2192): at android.os.Looper.loop(Looper.java:137)
02-18 10:23:32.794: E/AndroidRuntime(2192): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-18 10:23:32.794: E/AndroidRuntime(2192): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 10:23:32.794: E/AndroidRuntime(2192): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 10:23:32.794: E/AndroidRuntime(2192): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-18 10:23:32.794: E/AndroidRuntime(2192): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-18 10:23:32.794: E/AndroidRuntime(2192): at dalvik.system.NativeStart.main(Native Method)
02-18 10:23:32.794: E/AndroidRuntime(2192): Caused by: java.lang.NullPointerException
02-18 10:23:32.794: E/AndroidRuntime(2192): at android.app.Activity.findViewById(Activity.java:1839)
02-18 10:23:32.794: E/AndroidRuntime(2192): at com.mobilebasedsignlanguage.MainActivity.<init>(MainActivity.java:12)
02-18 10:23:32.794: E/AndroidRuntime(2192): at java.lang.Class.newInstanceImpl(Native Method)
02-18 10:23:32.794: E/AndroidRuntime(2192): at java.lang.Class.newInstance(Class.java:1319)
02-18 10:23:32.794: E/AndroidRuntime(2192): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
02-18 10:23:32.794: E/AndroidRuntime(2192): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
02-18 10:23:32.794: E/AndroidRuntime(2192): ... 11 more
最佳答案
移动这些行
Button conv = (Button)findViewById(R.id.btn_conv);
Button inst = (Button)findViewById(R.id.btn_ins);
Button view = (Button)findViewById(R.id.btn_view);
Button ext = (Button)findViewById(R.id.btn_ex);
在
onCreate()
调用后转到Activity
的setContentView()
...