gpt4 book ai didi

java - 无法弄清楚为什么我的第二个 Activity 没有加载?

转载 作者:行者123 更新时间:2023-12-02 06:57:42 25 4
gpt4 key购买 nike

我是android初学者当在我的MainActivity中按下按钮时,它显示加载我的第二个 Activity ,但它不是,而是我的应用程序只是说“不幸的是已经停止工作”?我运行了调试器,但仍然无法找出问题所在?任何帮助将不胜感激。

我的主要 Activity 类别:

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


}
});
}
}

我的主要 Activity 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>

我的第二个 Activity 课:

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

}
}

我的第二个 Activity 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>

最佳答案

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

关于java - 无法弄清楚为什么我的第二个 Activity 没有加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17097623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com