正在学习 Android 列表但没有任何反应

Learning Android Lists and nothing happens

好的,我正在做一个来自 Android 训练营的非常基本的新手应用程序(第 5 章,第一个 activity)

objective 是做一个列表,然后当你点击第一项时 "Family Photography" 它应该会显示另一个显示家庭照片的页面。 点击第二个 "Portrait Photography" 它显示了一张女士的照片。 单击第三个 "Picture People full site" 它会打开一个新的浏览器 window 进入该网站。

所以我相信我已经正确构建了字符串和 类 来执行此操作,但是当我在模拟器中单击每个 link 时,我什么也得不到。没有错误,没有错误,没有崩溃...只是从屏幕上确认我确实点击了它。

主要Activity

    package net.androidbootcamp.photographystudio;


import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends ListActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] phototype={"Family Photography", "Portrait Photography", "Picture People Full Site"};
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, phototype));
    }

    protected void onLIstItemClick(ListView l, View v, int position, long id) {
       switch(position){
       case 0:
          startActivity(new Intent(MainActivity.this, Portrait.class));
           break;
       case 1:
           startActivity(new Intent(MainActivity.this, Family.class));
           break;
       case 2:
           startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.picturepeople.com")));
           break;
       }
    }


    @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;
    }

    @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.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Family.java

    package net.androidbootcamp.photographystudio;

import android.app.Activity;
import android.os.Bundle;

public class Family extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.family);
}
}

Portrait.java 包裹 net.androidbootcamp.photographystudio;

import android.app.Activity;
import android.os.Bundle;

public class Portrait extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.family);
}
}

布局 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"
    tools:context="net.androidbootcamp.photographystudio.MainActivity" >



</RelativeLayout>

family.xml

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/family"
        android:src="@drawable/family" />

</RelativeLayout>

portrait.xml

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:contentDescription="@string/portrait"
        android:src="@drawable/portrait" />

</RelativeLayout>

AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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=".Portrait"></activity>
        <activity android:name=".Family"></activity>
    </application>

</manifest>

这个:

protected void onLIstItemClick(ListView l, View v, int position, long id) {

应该是:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    //your code...
}
package com.example.listviewexample;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity {

    String[] phototype={"Family Photography", "Portrait Photography", "Picture People Full Site"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, phototype));
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
       switch(position){
       case 0:
          startActivity(new Intent(MainActivity.this, Portrait.class));
           break;
       case 1:
           startActivity(new Intent(MainActivity.this, Family.class));
           break;
       case 2:
           startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.picturepeople.com")));
           break;
       }
    }
}

由于这个拼写错误 onLIstItemClick()[=16=,您在类似 onLIstItemClick() 中添加了 I(使用小写的 i 而不是大写的 I) ] 被视为正常方法而不是覆盖 ListView.This 的方法应该是 onListItemClick()