我正在尝试在Android中运行一个项目但是在尝试执行它时崩溃了:我真的不理解我得到的logCat消息所以我想看看是否有人可以帮助我理解它们。
以下是我的一些错误:
FATAL EXCEPTION: MAIN
java.lang.RunTimeException: Unable to start activity componentInfo (com.example.logger/com.example.logger.ThirdActivity):java.lang.RuntimeException: your content must have a ListView whose id attribe is 'android.R.id.list';
Caused by: java.lang.RuntimeException: your content must have a ListView whose id attribute is 'android.R.id.list';
at.com.example.logger.ThirdActivity.onCreate (ThirdActivity:java:18)
Android清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.logger"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.logger.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="com.example.logger.SecondActivity"
android:label="@string/title_activity_second"
></activity>
<activity
android:name="com.example.logger.ThirdActivity"
android:label="@string/title_activity_third" >
</activity>
</application>
</manifest>
结束清单
主要活动
package com.example.logger;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
attachHandlers();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void attachHandlers() {
findViewById(R.id.login).setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.login)
{
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}
}
结束主要活动
SecondActivity
package com.example.logger;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.view.ViewGroup;
public class SecondActivity extends Activity {
private static final int NUM_PAGES = 10;
private ViewPager mpager;
private MyPagerAdapter mPagerAdapter;
private int [] pics = {R.drawable.android_interfaz_layout_estructura_final, R.drawable.baixa, R.drawable.baixada, R.drawable.greenprogressbar,
R.drawable.layout_keyboard, R.drawable.linerlay, R.drawable.merge1, R.drawable.o2zds, R.drawable.regbd,
R.drawable.s7qrs};
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mPagerAdapter = new MyPagerAdapter(null);
mpager = (ViewPager) findViewById(R.id.viewer);
mpager.setAdapter (mPagerAdapter);
}
public void showImage (int position, View view)
{
//View is your view that you returned from instantiateItem
//and position is it's position in array you can get the image resource id
//using this position and set it to the ImageView
}
private class MyPagerAdapter extends PagerAdapter
{
SecondActivity activity;
public MyPagerAdapter (SecondActivity activity){
this.activity=activity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return pics.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return false;
}
@Override
public Object instantiateItem(View collection, int position) {
ImageView view = new ImageView (SecondActivity.this);
view.setImageResource (pics[position]);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void setPrimaryItem (ViewGroup container, int position, Object object)
{
super.setPrimaryItem (container, position, object);
activity.showImage(position,(View)object);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
private OnClickListener mPageClickListener = new OnClickListener()
{
public void onClick (View v)
{
// TODO Auto-generated method stub
//aquí anirà la funció per traslladar la image de la gallery a la pantalla
Integer picId = (Integer) v.getTag();
mpager.setVisibility(View.VISIBLE);
mpager.setCurrentItem(v.getId());
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
};
}
结束第二项活动
第三项活动
package com.example.logger;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class ThirdActivity extends ListActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
final ListView listview = (ListView) findViewById(android.R.id.list);
String[] values = new String[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen",
"Twenty"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_third, android.R.id.list, values);
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.third, menu);
return true;
}
public void attachButton()
{
findViewById(R.id.add).setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
结束第三项活动
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_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"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Login"
android:layout_alignParentBottom="true"
android:background="@drawable/shapes"
android:textColor="@color/text"
/>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/input_u"
android:layout_marginTop="10dp"
android:textColor="@color/text"
/>
<EditText
android:id="@+id/password"
android:layout_below="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/input_p"
android:layout_marginTop="10dp"
android:textColor="@color/text"
/>
</RelativeLayout>
结束Activity_Main.xml
Activity_Second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_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"
tools:context=".SecondActivity" >
<android.support.v4.view.ViewPager
android:padding="16dp"
android:id="@+id/viewer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0.70"
/>
<ImageView
android:id="@+id/big_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="@+id/viewer"
android:layout_weight="0.30"
/>
</RelativeLayout>
结束Activity_Second.xml
**Activity_Third.xml**
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_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"
tools:context=".ThirdActivity" >
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/add"
/>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_below="@+id/add"
android:layout_height="wrap_content"
></ListView>
</RelativeLayout>
结束Activity_Third.xml
如果有人能帮助我弄清楚我在这个项目上做错了什么,我将非常感激。
您诚挚的,
莫罗。
答案 0 :(得分:2)
更改ListView的ID
这
android:id="@+id/list"
到
android:id="@android:id/list"
Android无法找到您的ListView
,因为您提供了错误的ID。
答案 1 :(得分:0)
它说在ThirdActivity.java的onCreate方法中,你需要设置一个包含id为“android.R.id.list”的ListView的内容
您的内容在third_activity.xml文件中描述。
因此在third_activity.xml中,您的列表视图应具有此ID:
<ListView
android:id="@android:id/list"
而不是
android:id="@+id/list"
(如果需要,请查看此帖子Listview error: "Your content must have a ListView whose id attribute is 'android.R.id.list'")