Java 启动应用程序时出现致命异常
我是android studio的新手。我正在创建一个应用程序,使用socket在PC和手机之间传递数据。 我已经将基于命令的程序与android相结合。 在这种情况下,客户端将号码发送到服务器,服务器将检查它是否为prime,根据它,服务器将向客户端发送回复。但是,我在启动主活动时遇到致命异常 Logcat是:Java 启动应用程序时出现致命异常,java,android,exception,Java,Android,Exception,我是android studio的新手。我正在创建一个应用程序,使用socket在PC和手机之间传递数据。 我已经将基于命令的程序与android相结合。 在这种情况下,客户端将号码发送到服务器,服务器将检查它是否为prime,根据它,服务器将向客户端发送回复。但是,我在启动主活动时遇到致命异常 Logcat是: 08-18 11:59:26.977 1728-1728/com.example.admin.test_purpose W/dalvikvm: threadid=1: thread e
08-18 11:59:26.977 1728-1728/com.example.admin.test_purpose W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb4e68288)
08-18 11:59:26.977 1728-1728/com.example.admin.test_purpose E/AndroidRuntime:
FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.admin.test_purpose/com.example.admin.test_purpose.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1825)
at com.example.admin.test_purpose.MainActivity.<init>(MainActivity.java:15)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_gravity="center_horizontal"
android:hint="@string/user_value"
android:layout_marginTop="15dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.test_purpose">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
更新您的主要活动
EditText user_num;
TextView reply;
Button btnsbm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user_num = (EditText)findViewById(R.id.editText);
reply = (TextView)findViewById(R.id.textView);
btnsbm = (Button)findViewById(R.id.button);
onclicklistener();
}
更新您的主要活动
EditText user_num;
TextView reply;
Button btnsbm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user_num = (EditText)findViewById(R.id.editText);
reply = (TextView)findViewById(R.id.textView);
btnsbm = (Button)findViewById(R.id.button);
onclicklistener();
}
问题在于这三条线:
EditText user_num = (EditText)findViewById(R.id.editText);
TextView reply = (TextView)findViewById(R.id.textView);
Button btnsbm = (Button)findViewById(R.id.button);
问题是,在视图初始化之前,您正在调用findViewById()
将findViewById
-组件移动到onCreate()
,然后它应该可以工作:
EditText user_num;
TextView reply;
Button btnsbm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user_num = (EditText)findViewById(R.id.editText);
reply = (TextView)findViewById(R.id.textView);
btnsbm = (Button)findViewById(R.id.button);
onclicklistener();
}
问题在于这三条线:
EditText user_num = (EditText)findViewById(R.id.editText);
TextView reply = (TextView)findViewById(R.id.textView);
Button btnsbm = (Button)findViewById(R.id.button);
问题是,在视图初始化之前,您正在调用findViewById()
将findViewById
-组件移动到onCreate()
,然后它应该可以工作:
EditText user_num;
TextView reply;
Button btnsbm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user_num = (EditText)findViewById(R.id.editText);
reply = (TextView)findViewById(R.id.textView);
btnsbm = (Button)findViewById(R.id.button);
onclicklistener();
}
将您的
findViewByid
移动到setContentView(布局)
后setContentView(布局)
移动您的findViewByid
后setContentView(布局)
谢谢合作伙伴:)工作正常。。。!!!谢谢伙计:)它工作了。。。!!!