startActivity不起作用

时间:2015-10-27 21:15:47

标签: android android-intent android-activity

这个问题的所有解决方案都没有帮助,所以我在一个新问题中提出。

我需要从ListView的OnItemClickListener方法打开一个活动,但它什么都不做......新活动既不会打开也不会抛出异常。

这是尝试显示活动的代码:

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView <? > parent, View v, int position, long id) {
        try {
            ImageGalleryItem item = (ImageGalleryItem) parent.getItemAtPosition(position);
            //Create intent
            Intent intent = new Intent(v.getContext(), DetailsActivity.class);
            intent.putExtra("title", item.getTitle());
            intent.putExtra("image", item.getImage());

            //Start details activity
            startActivity(intent);
        } catch (Exception e) {
            Auditing.LogError(e);
        }
    }
});

这是清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cl.virtualice.tdc">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-feature android:name="android.hardware.Camera" android:required="true" />

    <application
        android:label="@string/app_title"
        android:icon="@drawable/ic_tdc"
        android:theme="@style/AppTheme"
        android:allowBackup="true">

        <activity
            android:name=".activities.MainActivity"
            android:theme="@style/AppTheme"
            android:label="@string/app_title">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.FormActivity"
            android:parentActivityName=".activities.MainActivity">
        </activity>
        <activity
            android:name=".activities.GalleryActivity"
            android:parentActivityName=".activities.FormActivity">
        </activity>
        <activity
            android:name=".activities.DetailsActivity"
            android:parentActivityName=".activities.GalleryActivity">
        </activity>
    </application>

</manifest>

这是新的活动代码:

package cl.virtualice.tdc.activities;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import cl.virtualice.tdc.R;

/**
 * Created by Jaime on 23-10-2015.
 */
public class DetailsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        String title = getIntent().getStringExtra("title");
        Bitmap bitmap = getIntent().getParcelableExtra("image");

        TextView titleTextView = (TextView) findViewById(R.id.title);
        titleTextView.setText(title);

        ImageView imageView = (ImageView) findViewById(R.id.image);
        imageView.setImageBitmap(bitmap);
    }
}

请帮忙吗?

3 个答案:

答案 0 :(得分:0)

你试过这个:

Intent intent = new Intent(getApplicationContext(), DetailsActivity.class);

答案 1 :(得分:0)

您的问题可能就是这一点:

gridView.setOnItemClickListener

应该是:

listView.setOnItemClickListener

确保您的程序进入try块。然后我想你可以使用:

Intent intent = new Intent(YOUR_CURRENT_ACTIVITY.this, DetailsActivity.class);

YOUR_CURRENT_ACTIVITY是包含ListView

的活动

答案 2 :(得分:0)

Activity有可能会启动,但会立即关闭,因为它缺少例如正确的intent参数或参数类型错误(例如Long和Integer将被区别对待)。

就像你在@oguzhand回答中评论的那样。确实存在意图问题,当你删除它时,激活就开始了。但在其他情况下,您需要在名为Activity的onCreate方法中放置断点,看看它是否正在运行。

至于第二个问题&#34;如何将Bitmap作为额外的&#34;,用户849998在这里回答:https://stackoverflow.com/a/8407644/4911442

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
R.drawable.ic_launcher);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
byte[] b = baos.toByteArray();

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);

然后在调用Activity中你会想要像这样检索它:

Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

Haven自己测试过。