[英]Intent Crashing. Android Studio
I just want to ask you why using intent makes my app crash? 我只想问你为什么使用intent会使我的应用崩溃? I've used same codes before and it works, now when I used it again it aint. 我以前使用过相同的代码,并且可以正常工作,现在当我再次使用它时,它不是。 What do you think the problem of this code. 您如何看待这段代码的问题。 I didnt find anything. 我什么都没找到。
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);
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
}
});
}
<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" />
<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>
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
Hope somebody can help me 希望有人能帮助我
Your provided Logcat is not useful to find the exact error. 您提供的Logcat对查找确切错误没有帮助。 But, change your okay
method to this. 但是,将您的okay
方法更改为此。
public void okay(View view) {
Intent i = new Intent(MainActivity.this, Login.class);
startActivity(i);
this
refers to onClick
, So, change to MainActivity.this
. this
是指onClick
,因此,请更改为MainActivity.this
。
And why you want to start the same activity again on onClick? 为什么要在onClick上再次开始相同的活动?
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);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.