gpt4 book ai didi

java - 在Android中使用listview启动另一个Activity

转载 作者:行者123 更新时间:2023-12-01 15:01:11 24 4
gpt4 key购买 nike

我想在用户单击列表项时启动另一个 Activity 。当我运行此命令时,虚拟机上没有出现错误,但屏幕完全空白。关于我的代码有什么错误有什么建议吗?

我计划启动另一个 Activity 的 Activity 代码是

public class Selector extends ListActivity {

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

ListView listView = (ListView) findViewById(android.R.id.list);
// storing string resources into Array
String[] story_titles = getResources().getStringArray(R.array.story_list);

// Binding resources Array to ListAdapter

ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,android.R.id.text1,story_titles);

listView.setAdapter(adapter);
}


public void onListItemClick(ListView parent,View view, int position, long id) {


// Launching new Activity on selecting single List Item
Intent i = new Intent(this, Descriptor.class);
// sending data to new activity
startActivity(i);
}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

Descriptor类的代码如下

public class Descriptor extends ListActivity {

public void onCreateBundle(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.descriptor);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

描述符 xml 为

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >



<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="40dip"
android:text="@string/select"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/background_dark" >
</TextView>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >




<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="480dp"
android:layout_height="wrap_content"
android:padding="40dip"
android:textSize="16dip"
android:textStyle="bold" >

</ListView>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="40dip"
android:text="@string/description"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/background_dark" >
</TextView>

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Story Description goes Here"
android:padding="40dip"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/background_dark"
/>

</LinearLayout>
</LinearLayout>
</LinearLayout>

list 是

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fableoid"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<activity android:name=".Selector" />
<activity android:name=".Descriptor" />
</application>

</manifest>

最佳答案

您的 Descriptor.class 中有一个错误的方法。

public void onCreateBundle(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.descriptor);
}

调用

public void onCreate(Bundle savedInstanceState)

相反

关于java - 在Android中使用listview启动另一个Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13620279/

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