如何使用GridView打开活动。 Android的

时间:2017-06-09 09:16:08

标签: java android android-studio gridview

我在打开一个新活动时遇到问题,当点击元素0向我发送活动但这不会显示内容并且不会向我发送一些错误,并在androidManifest.xml中添加新活动。 我将留下我的代码:

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ivann.diuxgridmain" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".leon">
    </activity>


</application>

</manifest>

MainActivity.java

package com.example.ivann.diuxgridmain;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
GridView androidGridView;

String[] gridViewString = {
        "Leon1", "Ave1", "Perro1", "Cara1", "Corazòn1", "Enojado1",
        "Leon2", "Ave2", "Perro2", "Cara2", "Corazòn2", "Enojado2",
        "Leon3", "Ave3", "Perro3", "Cara3", "Corazòn3", "Enojado3",

} ;
int[] gridViewImageId = {
        R.drawable.leon, R.drawable.bird, R.drawable.cachorro, R.drawable.fb, R.drawable.heart, R.drawable.angry,
        R.drawable.leon, R.drawable.bird, R.drawable.cachorro, R.drawable.fb, R.drawable.heart, R.drawable.angry,
        R.drawable.leon, R.drawable.bird, R.drawable.cachorro, R.drawable.fb, R.drawable.heart, R.drawable.angry,


};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CustomGridViewActivity adapterViewAndroid = new CustomGridViewActivity(MainActivity.this, gridViewString, gridViewImageId);
    androidGridView=(GridView)findViewById(R.id.grid_view_image_text);
    androidGridView.setAdapter(adapterViewAndroid);
    androidGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int i, long id) {
             Intent intent=null;
            switch (i){
                case 0:
// I think here is the error *******
                    intent  = new Intent(MainActivity.this, leon.class);
                    startActivity(intent);
                    break;
            }
            Toast.makeText(MainActivity.this, "GridView Item: " + gridViewString[+i ], Toast.LENGTH_LONG).show();
        }
    });

}

}

leon.java

package com.example.ivann.diuxgridmain;

import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

/**
 * Created by ivann on 09/06/2017.
 */

public class leon extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.leonactividad);

    }
}

3 个答案:

答案 0 :(得分:1)

删除

 @Override
public void onCreate(Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.leonactividad);

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.leonactividad);

}

答案 1 :(得分:1)

因为您在onCreate方法中使用了PersistableBundle而在API级别21中使用了PersistableBundle。因此您可以替换onCreate方法,如下所示:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.leonactividad);

    }

答案 2 :(得分:1)

Button或ImageButton的优先级高于itemView。如果您的项目视图包含这些,则可能是原因,您可以改为使用TextView / ImageView。