【问题标题】:I am using Intent to call another Activity and not sure what's the error我正在使用 Intent 调用另一个 Activity,但不确定是什么错误
【发布时间】:2013-08-14 07:52:06
【问题描述】:

我正在使用意图调用另一个活动并将列表视图的值传递给第二个活动,但在单击列表视图选择时出现错误。得到

下面是我的 MainActivity

package com.listview_gps;

public class MainActivity extends ListActivity {

String [] locations = {
    "KFC",
    "McDonalds",
    "A & W",
    "Burger King",
    "Tesco",
    "Home"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    setListAdapter(new ArrayAdapter<String>(this,    android.R.layout.simple_list_item_1, locations));
}

public void onListItemClick(
        ListView parent, View v, int position, long id)
{
    Intent intent = new Intent(MainActivity.this, location_gps.class);
    intent.putExtra("Location", locations[position].toString());
    startActivity(intent);      

}

我的第二个活动

Package com.listview_gps;

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

public class location_gps extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.locationgps);

    TextView nameView = (TextView) findViewById(R.id.locationgps);
    nameView.setText(getIntent().getExtras().getString("Location"));
}

}

我的清单

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.listview_gps.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 = ".location_gps"
        android:label="Location GPS" >
        <intent-filter>
            <action android:name = "com.listview_gps" />
            <category android:name = "android.intent.category.DEFAULT" />
            </intent-filter>
        ></activity>
</application>

</manifest>

【问题讨论】:

  • 你检查过'position'的值吗,它一定不是你的数组'locations'的有效索引
  • 是的。我从这里得到了错误:
  • 无法启动活动组件信息{com.listview_gps/com.listview_gps.LocationGps}:java.lang.NullPointerException

标签: listview android-intent


【解决方案1】:

类名应以大写字母开头,因此将类名 location_gps 更改为 Location_gps 并确保在清单内的每个位置也进行更改

【讨论】:

    猜你喜欢
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多