gpt4 book ai didi

java - 从 ImageButton 启动新 Activity

转载 作者:太空宇宙 更新时间:2023-11-04 11:18:13 25 4
gpt4 key购买 nike

我正在尝试在我的 Android 应用程序中添加一个新 Activity ,并且我已经获得了以下代码:

public void BSettings() {
View.OnClickListener listSet = new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();
Intent intent= new Intent(MainActivity.this, Settings.class);
startActivity(intent);
}
};
b = (ImageButton) findViewById(R.id.imageButton7);
b.setOnClickListener(listSet);
}

尽管如此,单击按钮时应用程序会停止。问题是,当我只有 toast 时,它就起作用了,所以问题可能是 intentstartActivity(intent) 的创建。这两行有什么问题?

在logcat中,出现以下错误:

07-20 13:44:10.714 1534-1547/? E/ActivityManager: ANR in com.android.systemui
PID: 1625
Reason: Broadcast of Intent { act=android.intent.action.TIME_TICK flg=0x50000014 (has extras) }
Load: 0.0 / 0.0 / 0.0
CPU usage from 12435ms to 0ms ago (2017-07-20 13:43:57.570 to 2017-07-20 13:44:10.004):
18% 1534/system_server: 13% user + 4.9% kernel / faults: 10451 minor 12 major
5.3% 1625/com.android.systemui: 4.1% user + 1.2% kernel / faults: 5162 minor 8 major
4.5% 1890/adbd: 1.3% user + 3.1% kernel / faults: 587 minor
4.1% 1257/surfaceflinger: 0.4% user + 3.6% kernel / faults: 5 minor
3.1% 1731/com.android.phone: 2.4% user + 0.6% kernel / faults: 1052 minor
1% 1296/zygote: 0.6% user + 0.4% kernel / faults: 1635 minor
0.9% 1196/kworker/0:1H: 0% user + 0.9% kernel
0.6% 1277/kworker/u3:0: 0% user + 0.6% kernel
0.8% 1256/servicemanager: 0.2% user + 0.5% kernel
0.3% 1297/audioserver: 0.2% user + 0.1% kernel
0.2% 1306/netd: 0% user + 0.2% kernel / faults: 922 minor
0.2% 1203/logd: 0% user + 0.2% kernel / faults: 41 minor
0.2% 1279/dmcrypt_write: 0% user + 0.2% kernel
0.2% 1304/media.extractor: 0.2% user + 0% kernel / faults: 16 minor
0.1% 1305/mediaserver: 0.1% user + 0% kernel / faults: 163 minor
0.1% 3/ksoftirqd/0: 0% user + 0.1% kernel
0% 4/kworker/0:0: 0% user + 0% kernel
0.1% 1829/com.android.cellbroadcastreceiver: 0% user + 0% kernel / faults: 126 minor
0% 8/rcu_preempt: 0% user + 0% kernel
0% 1254/lmkd: 0% user + 0% kernel / faults: 1 minor
0% 1284/kworker/u3:1: 0% user + 0% kernel
0% 1290/jbd2/dm-0-8: 0% user + 0% kernel
0% 1298/cameraserver: 0% user + 0% kernel / faults: 126 minor
0% 1744/com.android.settings: 0% user + 0% kernel / faults: 37 minor
+0% 1899/com.google.android.gms.persistent: 0% user + 0% kernel
+0% 1914/android.process.media: 0% user + 0% kernel
+0% 1930/com.google.process.gapps: 0% user + 0% kernel
+0% 1943/com.google.android.googlequicksearchbox:interactor: 0% user + 0% kernel
+0% 1949/com.android.printspooler: 0% user + 0% kernel
+0% 1971/com.android.launcher3: 0% user + 0% kernel
+0% 2005/com.google.android.googlequicksearchbox:search: 0% user + 0% kernel
+0% 2073/logcat: 0% user + 0% kernel
+0% 2084/com.google.android.partnersetup: 0% user + 0% kernel
+0% 2136/android.process.acore: 0% user + 0% kernel
+0% 2160/com.android.keychain: 0% user + 0% kernel
100% TOTAL: 61% user + 26% kernel + 10% iowait + 1.2% softirq
CPU usage from 54628ms to 54628ms ago (1970-01-01 00:00:00.000 to 1970-01-01 00:00:00.000) with 0% awake:

编辑

将 Activity 添加到 list 后,它会启动该 Activity ,但在显示新窗口后,它会停止。这是设置代码:

public class Settings extends Activity 
{
protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.set);
}
}

设置代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_dark"
android:minHeight="?attr/actionBarSize"
android:visibility="visible"
>

<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/toolbar"
android:layout_gravity="center"
android:textColor="@android:color/background_dark"
android:textSize="18sp"
android:textStyle="bold"
/>
</android.support.v7.widget.Toolbar>

<ImageButton
android:id="@+id/back"
android:layout_width="61dp"
android:layout_height="56dp"
app:srcCompat="?attr/actionModeCloseDrawable"
tools:backgroundTint="@android:color/holo_green_dark"
tools:ignore="ContentDescription" />

<LinearLayout
android:layout_width="360dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">

<TextView
android:id="@+id/Configuración"
android:layout_width="384dp"
android:layout_height="wrap_content"
android:layout_weight="0.48"
android:ems="10"
android:inputType="textPersonName"
android:textAlignment="center"
android:textColor="@android:color/background_dark"
android:textSize="18sp"
android:textStyle="bold"
tools:ignore="TextViewEdits" />

<RelativeLayout
android:id="@+id/AutoLayout"
android:layout_width="wrap_content"
android:layout_height="77dp">

<TextView
android:id="@+id/auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/titulo1"
android:textColor="@android:color/background_dark"
android:textSize="18sp"
android:textStyle="bold"
tools:ignore="TextViewEdits" />

<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/autoarranuqe"
android:layout_toLeftOf="@+id/interruptor"
android:layout_toStartOf="@+id/interruptor"
android:text="@string/text1"
android:textAlignment="center" />

<Switch
android:id="@+id/int"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:checked="false"
android:textCursorDrawable="@android:color/holo_red_dark"
tools:checked="false" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/WIFILayout"
android:layout_width="match_parent"
android:layout_height="77dp">

<TextView
android:id="@+id/editText16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginStart="22dp"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/t2"
android:textColor="@android:color/background_dark"
android:textSize="18sp"
android:textStyle="bold"
tools:ignore="TextViewEdits" />

<TextView
android:id="@+id/Wifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText16"
android:text="TextView"
android:textAlignment="center"
tools:text="@string/texto2"
tools:ignore="HardcodedText" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/PlayerIDLayout"
android:layout_width="match_parent"
android:layout_height="103dp"
android:layout_weight="0.13">

<EditText
android:id="@+id/playerID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/gr"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="@string/texto3"
android:textColor="?android:attr/textColorTertiary"
android:textStyle="italic"
android:inputType="" />

<TextView
android:id="@+id/editText17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/t3"
android:textColor="@android:color/background_dark"
android:textSize="18sp"
android:textStyle="bold"
tools:ignore="TextViewEdits" />

<Button
android:id="@+id/gr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText17"
android:background="@android:color/holo_green_dark"
android:text="@string/bot1"
android:textColor="@android:color/background_light" />

<Button
android:id="@+id/br"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/gr"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignStart="@+id/gr"
android:layout_below="@+id/gr"
android:text="@string/bot2" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/ILayout"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_weight="0.57">

<RadioGroup
android:id="@+id/radio"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:weightSum="1"
tools:ignore="UselessParent">

<TextView
android:id="@+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/t0"
android:textAlignment="center"
android:textColor="@android:color/background_dark"
android:textSize="18sp"
android:textStyle="bold" />

<RadioButton
android:id="@+id/Fra "
android:layout_width="wrap_content"
android:layout_height="54dp"
android:checked="false"
tools:text="@string/tex" />

<RadioButton
android:id="@+id/En"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:checked="true"
tools:text="@string/texto5"
android:layout_weight="0.17" />

<RadioButton
android:id="@+id/Es"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_weight="0.41"
android:checked="true"
tools:text="@string/text4" />
</RadioGroup>
</RelativeLayout>
</LinearLayout>
</FrameLayout>

最佳答案

SettingsAndroidManifest.xml 中声明的 Activity 吗?如果您尝试启动 list 中未声明的Activity,您的应用将会崩溃。

关于java - 从 ImageButton 启动新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45219753/

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