应用程序在转到某个 activity 时强制关闭

App force closes when going to a certain activity

我是 Whosebug 的新手,也是 Android 开发的新手,我 运行 遇到了我正在开发的某个应用程序的一个相当恼人的问题。我的主 activity 上有两个按钮,一个 "Add Customer" 按钮和一个 "Show Customers" 按钮,每个按钮都指向各自的活动。

起初,只有“添加客户”按钮可以正常工作并成功转到 activity,没有任何问题。 “显示客户”按钮会导致应用强制关闭。

此时,我以为我的“显示客户”按钮无法正常工作,因此为了测试这一点,我切换了每个按钮指向的活动,以查看“添加客户”按钮是否会再次成功导致展会客户 activity。对此进行测试后,“添加客户”按钮现在实际上导致应用程序强制关闭,“显示客户”按钮起作用并转到“添加客户”activity。

完成此操作后,我知道实际的 Show Customers activity 有问题,而不是 Show Customers 按钮本身,但我完全不知道为什么它不起作用。所有活动都在清单中。我认为我的 Show Customer xml 代码有问题。下面我将 post 目前我拥有的应用程序的所有代码:

MainActivity.java

package bcs421.jorgeramirez.lab.layouts;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button addCustButton = (Button)findViewById(R.id.button_add_customer);
    Button showCustButton = (Button)findViewById(R.id.button_show);
    addCustButton.setOnClickListener(this);
    showCustButton.setOnClickListener(this);
}

    @Override
    public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.button_add_customer:
            Intent intent1 = new Intent(MainActivity.this, AddCustomerActivity.class);
            startActivity(intent1);
            break;
        case R.id.button_show:
            Intent intent2 = new Intent(MainActivity.this, ShowCustomerActivity.class);
            startActivity(intent2);
            break;
        default:
            break;      
    }


}
    }

ShowCustomerActivity.java

package bcs421.jorgeramirez.lab.layouts;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class ShowCustomerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_customer);
    ListView listNames;
    ArrayAdapter<String> listAdapter;
    listNames = (ListView)findViewById(R.id.listView);

    String [] names = new String[] {"Derek Jeter", "David Robertson", "Mark Texiera"};

    ArrayList<String> nameArray = new ArrayList<String>();
    nameArray.addAll(Arrays.asList(names));

    listAdapter = new ArrayAdapter<String>(this,R.layout.activity_show_customer, nameArray);



    listNames.setAdapter(listAdapter);


}



}

activity_show_customer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >



    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </ListView>

    <TextView   
        android:layout_width="fill_parent"    
        android:layout_height="fill_parent"    
        android:padding="10dp"    
        android:textSize="16sp" >
    </TextView>



   </LinearLayout>

AndroidManifest.xml

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

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

<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=".AddCustomerActivity"
        android:label="@string/app_name" >

    </activity>

    <activity 
        android:name=".ShowCustomerActivity"
        android:label="@string/app_name" >

    </activity>

</application>

</manifest>

我知道我应该 post logcat 但老实说我不知道​​如何去做,我一直在我的 phone 上测试应用程序,因为我可以'出于某种原因无法启动 AVD。我已经在网站和网上搜索了解决方案,但似乎找不到。任何帮助将不胜感激!提前谢谢大家。

我看到的一个潜在问题是在以下行中:

listAdapter = new ArrayAdapter<String>(this,R.layout.activity_show_customer, nameArray);

这里你使用了R.layout.activity_show_customer,这是你的布局文件的id。您需要使用资源的 id 来告诉它列表视图中单个项目的布局。类似于 android.R.layout.simple_list_item_1。结帐 ArrayAdapter in android to create simple listview

是的,问题出在您的 ShowCustomerActivity 上。您将 activity 布局传递给 ArrayAdapter,这是错误的

public class ShowCustomerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_customer);
    ListView listNames;
    ArrayAdapter<String> listAdapter;
    listNames = (ListView)findViewById(R.id.listView);

    String [] names = new String[] {"Derek Jeter", "David Robertson", "Mark Texiera"};

    ArrayList<String> nameArray = new ArrayList<String>();
    nameArray.addAll(Arrays.asList(names));

    listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, nameArray);
//The layout should be your list Item layout and not the activity layout.



    listNames.setAdapter(listAdapter);


}