如何在菜单项中创建列表视图?

时间:2014-08-08 06:37:44

标签: java android xml listview android-listview

我是android的新手,我尝试使用This教程向我的应用添加一个简单的列表视图。当我把它作为一个单独的项目时,我是成功的。我想在我当前的Unversity项目中使用相同的代码,将新闻作为菜单项。在此菜单项中,我想显示新闻标题的列表视图。我根据教程相应地工作,但没有运气。请sombody帮助我。我发布了我的代码Logcat以供参考。

MainActivity.java     包com.example.university;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.home:
Intent h = new Intent(getApplicationContext(), Home.class);
startActivity(h);
return true;    
case R.id.about:
Intent a = new Intent(getApplicationContext(), About.class);
startActivity(a);
return true;
case R.id.news:
Intent n = new Intent(getApplicationContext(), News.class);
startActivity(n);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

RES /布局/ news.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<string-array android:name="adobe_products">

    <item android:layout_width="wrap_content">Lorem Lipsum1</item>
    <item>Lorem Lipsum2</item>
</string-array>
</ListView>

News1.java     包com.example.university;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class News1 extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.news1);

    TextView txtProduct = (TextView) findViewById(R.id.news1);

    Intent i = getIntent();
    // getting attached intent data
    String product = i.getStringExtra("product");
    // displaying selected product name
    txtProduct.setText(product);

}
}

RES /布局/ news1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<TextView
    android:id="@+id/news1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Lorem Lipsum1"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="16dp" />
</RelativeLayout>

同样创建了News2.java和news2.xml

的Manifest.xml

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

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

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
 <!-- The following two permissions are required for location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.university.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=".About"
        android:label="@string/title_activity_about" >
    </activity>
    <activity
        android:name=".Home"
        android:label="@string/title_activity_home" >
    </activity>
    <activity
        android:name=".Contact"
        android:label="@string/title_activity_contact" >
    </activity>



    <activity
        android:name=".News1"
        android:label="Lorem Lipsum1" >
    </activity>

    <activity
        android:name=".News2"
        android:label="Lorem Lipsum2">
    </activity>

</application>

</manifest>

这是我的Logcat:     [2014-08-08 11:33:40 - 大学] Dx     写输出有问题:已经准备好了     [2014-08-08 11:33:41 - Dex Loader]无法执行dex:多个dex文件定义了Landroid / support / annotation / AnimRes;     [2014-08-08 11:33:41 - 大学]转换为Dalvik格式失败:无法执行dex:多个dex文件定义Landroid / support / annotation / AnimRes;

1 个答案:

答案 0 :(得分:1)

如果您的构建路径中包含多个版本的库,则也会发生此问题。从libs路径中删除未使用的库,清理并保存项目,重新启动Eclipse并重新编译它。 右键单击您的项目&gt;去物业&gt;构建路径 检查android-support-v4库