[英]launching a new activity from an ImageButton
I'm trying to add a new activity in my android app and I've got this code for it: 我试图在我的android应用程序中添加一个新的活动,并且我有下面的代码:
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);
}
Nevertheless, when clicking the button the app stops. 但是,单击按钮时,应用程序停止。 The thing is that, when I only had the
toast
, it worked, so the problem may be the creation of the intent
or the startActivity(intent)
. 问题是,当我
toast
,它就起作用了,所以问题可能出在创建intent
或startActivity(intent)
。 What's wrong with these two lines? 这两行有什么问题?
In the logcat, this error appears: 在日志猫中,出现此错误:
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:
EDIT 编辑
After adding the activity to the manifest, it launche the activity, but after showing the new window, it stops. 在将活动添加到清单后,它将启动活动,但是在显示新窗口之后,它将停止。 Tis is Settings code:
这是设置代码:
public class Settings extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.set);
}
}
set code: 设置代码:
<?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>
Is Settings
a declared activity in your AndroidManifest.xml
? Settings
是否已在AndroidManifest.xml
声明为活动? If you try to start an Activity
that is not declared in the manifest, your app will crash. 如果您尝试启动清单中未声明的
Activity
,则您的应用程序将崩溃。
You should add Settings Activity to the AndroidManifest.xml like this code : 您应该将“设置活动”添加到AndroidManifest.xml,例如以下代码:
<activity
android:name=".Settings"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.