2015-03-03 80 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

相關問題