2015-03-03 79 views
-1

我是StackOverflow的新手,也是Android开发的新手,我遇到了一个相当恼人的问题,我正在开发某个应用程序。我的主要活动中有两个按钮,一个是“添加客户”按钮和一个“显示客户”按钮,每个按钮都会导致他们各自的活动。应用程序部队在进行某个活动时关闭

首先,只有“添加客户”按钮可以正常工作并且顺利地进入其活动,而不会出现任何问题。显示客户按钮会导致应用程序强制关闭。

在这一点上,我认为我的显示客户按钮不能正常工作,所以为了测试这一点,我已经切换了每个按钮导致的活动,以查看添加顾客按钮是否会再次成功导致到显示客户活动。经过测试,添加客户按钮实际上现在导致应用程序强制关闭,显示客户按钮工作并转到添加客户活动。

完成此操作后,我知道实际显示客户活动出现问题,而不是显示客户按钮本身,但我完全停留在为什么它不起作用。所有活动都在清单中。我相信我的Show Customer xml代码有问题。下面我将发布所有的代码我对应用至今:

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> 

我知道我应该张贴logcat的,但我真的不知道如何得到它,我一直在我的手机上测试应用程序,因为我不能让AVD由于某种原因启动。我已经在网站上搜索了一个解决方案,但我似乎无法找到一个解决方案。任何帮助将不胜感激!先谢谢你们。我看到

+0

学习如何先启用logcat的一个ArrayAdapter。或试图知道如何使用try catch – 2015-03-03 02:47:17

回答

0

有问题与您ShowCustomerActivity ..Your逝去的活动布局,这是不对的

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); 


} 
+0

非常感谢!我知道我必须做一些布局,但无法弄清楚 – 2015-03-03 18:30:34

0

一个潜在问题是以下行:

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

+0

谢谢!这帮助我,并感谢链接良好的信息。 – 2015-03-03 18:31:00

相关问题