如何在安卓模拟器中解决"The apps has stopped working"!重新安装安卓工作室,但同样的错误



模拟器工作正常,但是当我点击运行时,模拟器会闪烁我的界面设计(布局(一段时间然后退出,然后显示消息应用程序已停止工作

MainActivity.Java

package com.example.xh3al3r.myapplication2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
RelativeLayout rellay_mainentry,rellay_garden,rellay_app,rellay_salarm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rellay_mainentry=findViewById(R.id.rellay_mainentry);
rellay_app=findViewById(R.id.rellay_app);
rellay_garden=findViewById(R.id.rellay_garden);
rellay_mainentry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,MainEntry.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
});
rellay_app.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,MainHApp.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
});

rellay_garden.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,MainGarden.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
});
rellay_salarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,MainAlarm.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
});
}

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="240dp"
android:background="@drawable/b1"
android:gravity="bottom"
android:text="Smart Resort System"
android:textAlignment="center"
android:textColor="@color/darkblue"
android:textSize="24sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<RelativeLayout
android:id="@+id/rellay_mainentry"
android:layout_width="270dp"
android:layout_height="150dp"
android:layout_weight="0.75"
android:background="@color/red"
android:clickable="true">
<RelativeLayout
android:layout_width="161dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp">
<ImageView
android:id="@+id/imgView1"
android:layout_width="90dp"
android:layout_height="88dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="8dp"
android:scaleType="centerInside"
android:src="@drawable/door" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imgView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Entry Points"
android:textAppearance="@android:style/TextAppearance" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rellay_app"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.75"
android:background="@color/yellow"
android:clickable="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="156dp"
android:layout_centerInParent="true">
<ImageView
android:id="@+id/apps"
android:layout_width="86dp"
android:layout_height="94dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:adjustViewBounds="true"
android:padding="8dp"
android:scaleType="centerInside"
android:src="@drawable/homeapp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/apps"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Home Appliances"
android:textAppearance="@android:style/TextAppearance.Holo.Widget.TextView"/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<RelativeLayout
android:id="@+id/rellay_garden"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_weight="0.75"
android:background="@color/lime_dark"
android:clickable="true">
<RelativeLayout
android:layout_width="161dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp">
<ImageView
android:id="@+id/watering"
android:layout_width="127dp"
android:layout_height="88dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:adjustViewBounds="true"
android:padding="8dp"
android:scaleType="centerInside"
android:src="@drawable/water" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/watering"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Gardening"
android:textAppearance="@android:style/TextAppearance" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rellay_salarm"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.75"
android:background="@color/blue_dark"
android:clickable="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="156dp"
android:layout_centerInParent="true">
<ImageView
android:id="@+id/alarms"
android:layout_width="86dp"
android:layout_height="94dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:adjustViewBounds="true"
android:padding="8dp"
android:scaleType="centerInside"
android:src="@drawable/homeapp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/alarms"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Alarms"
android:textAppearance="@android:style/TextAppearance.Holo.Widget.TextView"/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>

安卓清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xh3al3r.myapplication2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<activity android:name=".MainEntry" />
<activity android:name=".MainGarden" />
<activity android:name=".MainHApp" />
<activity android:name=".MainAlarm"></activity>
</application>
</manifest>

日志猫。下面是错误。模拟器出现,但未在布局中显示我的设计

09-11 15:34:40.389 4910-4910/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.xh3al3r.myapplication2, PID: 4910
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xh3al3r.myapplication2/com.example.xh3al3r.myapplication2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.xh3al3r.myapplication2.MainActivity.onCreate(MainActivity.java:48)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

我卸载并重新安装agn,但错误仍然相同

你在没有实际初始化该变量的情况下调用rellay_salarm.setOnClickListener()

rellay_salarm = findViewById(R.id.rellay_salarm);

最新更新