gpt4 book ai didi

java - 安卓工作室 : Using ImageButton to start new activity crashes the app

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:21:23 25 4
gpt4 key购买 nike

我正在设计一个主屏幕带有 6 个图像按钮的应用程序,所有这些按钮都会启动新的 Activity 。

目前,当我按下按钮时,应用程序崩溃了。然而这很奇怪,因为我用常规按钮完成了这个项目并且它工作正常,而且我也做到了,所以当按下图像按钮时它只打印“已点击!”在你的屏幕上,这也工作正常,所以问题是开始一个新的 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();




}

最佳答案

我猜问题出在这两行:

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

您应该首先放置 setContentView(),然后放置 ActionBarTitle

如果我是你,我会开始使用 [Toolbar](http://developer.android.com/reference/android/widget/Toolbar.html)
按如下方式执行
setTitle()`:

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

查看此 answer for more detail

关于java - 安卓工作室 : Using ImageButton to start new activity crashes the app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35479239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com