简体   繁体   中英

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

I am designing an app that has a home screen with 6 image buttons, that all start new activities.

Currently when I press the button, the app crashes. This is strange however as I have done this project with regular buttons and it works fine, and I have also made it so when the image button is pressed it just prints "Clicked!" on thee screen and this also works fine, so the problem is starting a new 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 package 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();




}

}

The problem I guess comes with this two lines :

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

You should put frist the setContentView() and then the Title of your ActionBar

If I were you I'd start using [ Toolbar ](http://developer.android.com/reference/android/widget/Toolbar.html) Do the setTitle()` as follows :

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

See this answer for more detail

Not correct:
setContentView(R.layout.activity_light); -> activity_lighting.xml
Different name.

But you have another different name:
setContentView(R.layout.activity_main); -> activity_mainn.xml
(Maybe typing error)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM