无法弄清楚为什么我的第二个活动没有加载?

时间:2013-06-13 21:42:25

标签: java android

我是Android的初学者当我在MainActivity中按下一个按钮时,它显示加载我的第二个活动,但它不是我的应用程序只是说“不幸的是已停止工作”?我跑了一个调试器,但仍然无法弄清楚是什么错?任何帮助将不胜感激。

我的主要活动课程:

public class MainActivity extends Activity {
/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

         //start second activity when play button pressed
    Button myButton = (Button) findViewById(R.id.play_button);
    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, Second.class));


        }
    });
}
}

我的主要活动XML:

    ?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="@drawable/woood_back"


        >



   <Button
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:drawableRight="@drawable/wood_backnw"
           android:layout_centerHorizontal="true"
           android:enabled="false"
           android:id="@+id/button"/>


    <Button
        android:id="@+id/play_button"
        android:layout_height="50dp"
        android:layout_width="220dp"
        android:text="@string/play_button"

        android:textSize="22dp"
        android:textStyle="bold"
        android:background="@color/MediumPurple"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="200dp"
        android:layout_marginBottom="10dp"

        />
    <Button
   </RelativeLayout>

我的第二个活动类:

    public class Second extends MainActivity{

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(Second.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });

    }
   }

我的第二个活动XML:

   <?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/gridview"
          android:background="@color/silver"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:columnWidth="90dp"
          android:numColumns="auto_fit"
          android:verticalSpacing="10dp"
          android:horizontalSpacing="10dp"
          android:stretchMode="columnWidth"
          android:gravity="center"
        />

我的图像适配器类:

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(200, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,

        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,

        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,

        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,

        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,

};
}

和我的androidManifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.twekstr.stateFlags"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17"/>
    <application android:label="@string/app_name" android:icon="@drawable/icon_america">
        <activity android:name="MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <activity android:name="Second"></activity>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

2 个答案:

答案 0 :(得分:1)

<activity android:name="Second"></activity>直接放在<application>而不是<intent-filter>内。

<application android:label="@string/app_name" android:icon="@drawable/icon_america">
    <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="Second"></activity>
</application>

答案 1 :(得分:1)

首先,请勿在{{1​​}}代码中定义您的活动。

其次,请勿将您的第二项活动延长至<intent-filter>

MainActivity