Android Studio:使用 ImageButton 启动新 activity 会使应用程序崩溃

Android Studio: Using ImageButton to start new activity crashes the app

我正在设计一个主屏幕有 6 个图像按钮的应用程序,所有按钮都可以启动新活动。

目前,当我按下按钮时,应用程序崩溃了。然而这很奇怪,因为我用常规按钮完成了这个项目并且它工作正常,而且我也这样做了所以当按下图像按钮时它只在你的屏幕上打印 "Clicked!" 而且这也工作正常,所以问题是开始一个新的 activity.

MainActivity.java

package com.example.darren1.homemanagementsystem;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.ImageButton;
import android.view.View.OnClickListener;
import android.view.View;

public class MainActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findAllViewsById();
}


private void findAllViewsById(){

    ImageButton lightButton = (ImageButton) findViewById(R.id.lightButton);
    lightButton.setOnClickListener(new View.OnClickListener() {


        public void onClick (View v){


            Intent intent = new Intent(MainActivity.this, LightingActivity.class);
            startActivity(intent);
        }
    });
}

}

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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#feae5e"
android:clickable="true">

<GridLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:background="#feae5e"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true">

    <ImageButton
        android:layout_width="137dp"
        android:layout_height="133dp"
        android:id="@+id/cameraButton"
        android:layout_row="0"
        android:layout_column="0"
        android:src="@drawable/rsz_cameraicon"
        android:background="#feae5e" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/alarmButton"
        android:layout_row="0"
        android:layout_column="13"
        android:src="@drawable/rsz_alarmicon"
        android:background="#feae5e" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fireButton"
        android:layout_row="1"
        android:layout_column="0"
        android:src="@drawable/rsz_fireicon"
        android:background="#feae5e"
        android:layout_marginTop="40dp" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lightButton"
        android:layout_row="1"
        android:layout_column="13"
        android:src="@drawable/rsz_lightbulbicon"
        android:background="#feae5e"
        android:layout_marginTop="40dp"
        />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/thermoButton"
        android:layout_row="2"
        android:layout_column="0"
        android:src="@drawable/rsz_1thermometericon"
        android:background="#feae5e"
        android:layout_marginTop="40dp" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvButton"
        android:layout_row="2"
        android:layout_column="13"
        android:src="@drawable/rsz_1tvicon"
        android:background="#feae5e"
        android:layout_marginTop="40dp" />

</GridLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.darren1.homemanagementsystem" >
<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>
    <activity
        android:name=".LightingActivity"
        android:label="Light" >
    </activity>
    <activity android:name=".TelevisionActivity"
        android:label="TV">
    </activity>
</application>

activity_lighting.xml

<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.darren1.homemanagementsystem.LightingActivity"
android:background="#beddeb">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="false"
    android:id="@+id/linearLayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/lighting"
        android:id="@+id/lightingView"
        android:layout_gravity="center_horizontal"
        android:textSize="32dp" />

</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="150dp"
    android:layout_height="200dp"
    android:layout_below="@+id/linearLayout"
    android:layout_alignParentStart="true"
    android:layout_marginTop="66dp"
    android:id="@+id/linearLayout2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/kitchen"
        android:id="@+id/kitchenLight" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/sittingroom"
        android:id="@+id/sittingroomLight"
        android:layout_marginTop="20dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/bedroom"
        android:id="@+id/bedroomLight"
        android:layout_marginTop="20dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/outside"
        android:id="@+id/outsideLight"
        android:layout_marginTop="20dp" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="150dp"
    android:layout_height="200dp"
    android:layout_alignTop="@+id/linearLayout2"
    android:layout_alignEnd="@+id/linearLayout"
    android:id="@+id/linearLayout3">

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/switch1" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/switch2"
        android:layout_marginTop="20dp" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/switch3"
        android:layout_marginTop="20dp" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/switch4"
        android:checked="false"
        android:layout_marginTop="20dp" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true">

    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="@string/backButton"
        android:id="@+id/backButton"
        android:layout_gravity="center_horizontal"
        android:background="#010101"
        android:textColor="#fefdfd"
        android:textStyle="bold"
        android:textSize="20dp" />
</LinearLayout>

LightingActivity.java 包裹 com.example.darren1.homemanagementsystem;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View.OnClickListener;

public class LightingActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setTitle("Light");
    setContentView(R.layout.activity_lighting);


    onClickListenerButton();
}


public void onClickListenerButton(){


    Intent i = getIntent();




}

}

不正确:
setContentView(R.layout.activity_light); -> activity_lighting.xml
不同的名字。

但是你有另一个不同的名字:
setContentView(R.layout.activity_main); -> activity_mainn.xml
(可能是输入错误)

我猜问题出在这两行:

 getActionBar().setTitle("Light");
 setContentView(R.layout.activity_lighting);

你应该先放 setContentView(),然后放 ActionBar

Title

If I were you I'd start using [Toolbar](http://developer.android.com/reference/android/widget/Toolbar.html) Do thesetTitle()` 如下:

ActionBar actionBar = getActionBar();
actionBar.setTitle("Light");

看到这个answer for more detail