【发布时间】:2025-11-22 06:10:01
【问题描述】:
我只想问你,为什么使用 Intent 会导致我的应用崩溃?我以前使用过相同的代码并且它可以工作,现在当我再次使用它时它就不行了。你觉得这段代码有什么问题。我什么也没找到。
MainActivity JAVA
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void okay(View view) {
Intent i = new Intent(this, Login.class);
startActivity(i);
登录 JAVA(如果我调用它的 id,我只想弹出这个活动)
public class Login extends ActionBarActivity {
ListView listView;
ArrayAdapter<String> adapter;
String[] grocery_categories = {"Beverages", "Bakery", "Canned Goods", "Condiments", "Dairy", "Snacks", "Frozen Foods",
"Meat", "Produce", "Cleaners", "Paper Goods", "Personal Care", "Others"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
listView = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, grocery_categories);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
String grocery = (String) listView.getAdapter().getItem(position);
Intent intent = new Intent(listView.getContext(),Login.class);
listView.getContext().startActivity(intent);
//or create other intents here
}
});
}
MainActivity.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" tools:context=".MainActivity"
android:id="@+id/rl_main_activity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mobile_grocery_bckgrnd"
android:src="@drawable/mobile_grocery"
android:scaleType="centerCrop"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MOBILE GROCERY"
android:id="@+id/mobile_grocery_app"
android:textSize="45dp"
android:textColor="#000000"
android:gravity="center"
android:textStyle="bold|italic"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Application"
android:id="@+id/application"
android:textColor="#000000"
android:textSize="25dp"
android:layout_below="@+id/mobile_grocery_app"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/username"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/application"
android:layout_alignEnd="@+id/application"
android:layout_marginBottom="135dp"
android:hint="Username"
android:textColorHint="#000000"
/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/password"
android:layout_alignTop="@+id/username"
android:layout_alignRight="@+id/username"
android:layout_alignEnd="@+id/username"
android:layout_marginTop="52dp"
android:hint="Password"
android:textColorHint="#000000"
android:password="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/ok"
android:layout_below="@+id/password"
android:layout_alignLeft="@+id/application"
android:layout_alignStart="@+id/application"
android:onClick="okay" />
登录.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"
tools:context="com.example.admin.mobilegroceryapp.Login"
android:id="@+id/rl_login">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/login_bckgrnd"
android:src="@drawable/login_bckgrnd"
android:scaleType="centerCrop"
/>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.mobilegroceryapp" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:label="@string/title_activity_login" >
</activity>
</application>
</manifest>
LOGCAT
9-29 02:17:52.595 2019-2074/? D/BatteryService﹕ level:34 scale:100 status:2 health:2 present:true voltage: 3918 temperature: 350 technology: Li-ion
AC powered:false USB powered:true icon:17303267 invalid charger:0 online:1 charge type:0 current avg:1
09-29 02:17:52.610 2140-2140/? D/STATUSBAR-BatteryController﹕ onReceive() - ACTION_BATTERY_CHANGED
09-29 02:17:52.610 2140-2140/? D/STATUSBAR-BatteryController﹕ onReceive() - level:34
09-29 02:17:52.610 2140-2140/? D/STATUSBAR-BatteryController﹕ onReceive() - plugged:2
09-29 02:17:52.615 2140-2140/? D/STATUSBAR-BatteryController﹕ onReceive() - BATTERY_STATUS_CHARGING:
09-29 02:17:52.625 2019-2074/? D/BatteryService﹕ turn on LED for charging
09-29 02:17:52.635 2140-2140/? D/STATUSBAR-PhoneStatusBar﹕ ACTION_BATTERY_CHANGED
09-29 02:17:52.650 2140-2140/? D/STATUSBAR-PhoneStatusBar﹕ NORMAL_BATTERY
09-29 02:17:52.670 1381-1381/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
09-29 02:17:52.670 1381-1381/? E/MtpService﹕ battPlugged Type : 2
09-29 02:17:53.405 2019-2078/? V/AlarmManager﹕ waitForAlarm result :2
09-29 02:17:53.540 5709-5709/? D/InCarWidget:main﹕ VLG_updateAllWidgets false
09-29 02:17:53.540 5709-5709/? D/InCarWidget:main﹕ VLG_buildWidgetView enabled=false
09-29 02:17:53.555 2019-2031/? W/AlarmManager﹕ FACTORY_ON= 0
09-29 02:17:56.555 2019-2078/? V/AlarmManager﹕ waitForAlarm result :2
09-29 02:17:56.690 5709-5709/? D/InCarWidget:main﹕ VLG_updateAllWidgets false
09-29 02:17:56.690 5709-5709/? D/InCarWidget:main﹕ VLG_buildWidgetView enabled=false
09-29 02:17:56.695 2019-5434/? W/AlarmManager﹕ FACTORY_ON= 0
09-29 02:17:59.690 2019-2078/? V/AlarmManager﹕ waitForAlarm result :2
09-29 02:17:59.800 5709-5709/? D/InCarWidget:main﹕ VLG_updateAllWidgets false
09-29 02:17:59.800 5709-5709/? D/InCarWidget:main﹕ VLG_buildWidgetView enabled=false
09-29 02:17:59.805 2019-2019/? W/AlarmManager﹕ FACTORY_ON= 0
09-29 02:18:00.000 2019-2078/? V/AlarmManager﹕ waitForAlarm result :8
09-29 02:18:00.010 2019-2062/? V/AlarmManager﹕ ClockReceiver onReceive() ACTION_TIME_TICK
09-29 02:18:00.010 2019-2062/? W/AlarmManager﹕ FACTORY_ON= 0
09-29 02:18:00.055 2140-2140/? D/STATUSBAR-Clock﹕ onReceive() - ACTION_TIME_TICK
09-29 02:18:00.080 2140-2140/? D/STATUSBAR-Clock﹕ onReceive() - ACTION_TIME_TICK
09-29 02:18:00.100 2140-2140/? D/TextLayoutCache﹕ Cache value 0x5ea48dc0 deleted, size = 144
09-29 02:18:00.160 30579-30579/? D/Launcher﹕ onTrimMemory. Level: 80
09-29 02:18:00.160 30579-30579/? D/Launcher﹕ releaseShadows called
09-29 02:18:00.165 30579-30579/? W/ManagedEGLContext﹕ doTerminate failed: EGL count is 2 but managed count is 1
09-29 02:18:02.810 2019-2078/? V/AlarmManager﹕ waitForAlarm result :2
09-29 02:18:02.940 5709-5709/? D/InCarWidget:main﹕ VLG_updateAllWidgets false
09-29 02:18:02.945 5709-5709/? D/InCarWidget:main﹕ VLG_buildWidgetView enabled=false
09-29 02:18:02.955 2019-2031/? W/AlarmManager﹕ FACTORY_ON= 0
09-29 02:18:05.960 2019-2078/? V/AlarmManager﹕ waitForAlarm result :2
09-29 02:18:06.090 5709-5709/? D/InCarWidget:main﹕ VLG_updateAllWidgets false
09-29 02:18:06.090 5709-5709/? D/InCarWidget:main﹕ VLG_buildWidgetView enabled=false
09-29 02:18:06.095 2019-5434/? W/AlarmManager﹕ FACTORY_ON= 0
09-29 02:18:07.135 2019-2210/? E/Watchdog﹕ !@Sync 6737
09-29 02:18:09.095 2019-2078/? V/AlarmManager﹕ waitForAlarm result :2
09-29 02:18:09.200 5709-5709/? D/InCarWidget:main﹕ VLG_updateAllWidgets false
09-29 02:18:09.200 5709-5709/? D/InCarWidget:main﹕ VLG_buildWidgetView enabled=false
09-29 02:18:09.205 2019-2019/? W/AlarmManager﹕ FACTORY_ON= 0
希望有人可以帮助我
【问题讨论】:
-
为 MainActivity 和 Login 提供完整的两个 xml 文件,因为它在两个 xml 中都缺少 RelativeLayout 的结束标记。并尝试输入所有代码,包括导入和包名
-
请提供显示异常堆栈跟踪的有用日志。您当前显示的日志未向我们提供有关您的应用进程的任何信息。
标签: java android mysql android-layout date