为什么意图使我的应用程序崩溃?

时间:2015-09-28 17:10:43

标签: java android android-layout android-activity android-listview

我只想问你为什么使用intent会让我的系统崩溃?我之前使用过相同的代码并且它有效,然后当我再次使用它时,它现在就赢了。你觉得这个问题怎么样?

清单

<?xml version="1.0" encoding="utf-8"?>

<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>

MainActivity

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
        }
    });

}

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(第二个活动)

<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的

  .085    2019-2087/? I/InputDispatcher﹕ Delivering touch to current input target: action: 0x0
09-29 01:54:28.085    2019-2087/? I/InputDispatcher﹕ Delivering touch to current input target: action: 0x0
09-29 01:54:28.085    2019-2087/? I/InputDispatcher﹕ Delivering touch to current input target: action: 0x0
09-29 01:54:28.085    2019-2087/? I/InputDispatcher﹕ Delivering touch to current input target: action: 0x0
09-29 01:54:28.085    2019-2087/? I/InputDispatcher﹕ Delivering touch to current input target: action: 0x0
09-29 01:54:28.190    2019-2088/? I/InputReader﹕ Touch event's action is 0x1 (deviceType=0) [pCnt=1, s=]
09-29 01:54:28.285  30351-30351/? I/dalvikvm﹕ Could not find method android.graphics.Bitmap.getAllocationByteCount, referenced from method com.facebook.imagepipeline.a.d.a.a
09-29 01:54:28.285  30351-30351/? W/dalvikvm﹕ VFY: unable to resolve virtual method 625: Landroid/graphics/Bitmap;.getAllocationByteCount ()I
09-29 01:54:28.285  30351-30351/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0006
09-29 01:54:28.355  30482-30482/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 0ms
09-29 01:54:28.355    2019-2074/? D/BatteryService﹕ update start
09-29 01:54:28.360    2019-2079/? D/KeyguardViewMediator﹕ setHidden false
09-29 01:54:28.360    1732-1732/? I/SurfaceFlinger﹕ id=13169 Removed TcreenSaver idx=4 MapSz=10

希望有些亲帮我解决问题。有人说这很简单,但我无法解决先生。

2 个答案:

答案 0 :(得分:0)

您需要将登录活动添加到AndroidManifest.xml,以便Android操作系统可以找到意图引用的活动。

<activity
android:name=".Activities.Login"
android:label="Login" />

答案 1 :(得分:0)

您真的需要向我们展示您的堆栈跟踪以获得任何有用的反馈。我怀疑您没有正确地在handlebars中声明登录活动。

LogCat将包含错误堆栈跟踪。很可能你会看到一个巨大的红色文本块,表示我们感兴趣的部分。下面是我的一个堆栈跟踪的例子。希望它可以帮助您找到它。

api.use(['blaze-html-templates', 'handlebars'], 'client', {weak: true});