Android ListView新活动启动
我一直在尝试许多解决方案和代码组合,但我总是让我的进程在应用程序中被迫关闭。您有什么建议吗?
public class CompaniesViewActivity extends ListActivity {
ArrayList<String> _listItems = new ArrayList<String>(); // Stores data from Company object
ArrayList<String> _companyNames = new ArrayList<String>();
ArrayAdapter<String> _adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.companiesview_layout); // Setting the appropriate layout
GlobalState globalState = (GlobalState)getApplicationContext(); // Setting the global vars
ArrayList<Company> companies = globalState.loadCompanies(); // Retrieving the companies list
// that was created during the
// first view
_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, _listItems);
// Constructing an ArrayAdapter in order to create a dynamic ListView
// associated with the _listItems Dynamic String Array List
setListAdapter(_adapter); // Setting the adapter
addItems(companies); // Calls the method to add the items in the ListView
}
// Adding items to the ListView dynamically for all the companies in the retrieved companies list
public void addItems(ArrayList<Company> companies) {
for (int i=0; i<companies.size(); i++) {
_listItems.add(companies.get(i).getName() + "\n" + "ΑΦΜ: " + companies.get(i).getVat());
_companyNames.add(companies.get(i).getName());
_adapter.notifyDataSetChanged(); // Refreshing the View with the data filled
}
}
public void onListItemClick(ListView l, View v, int position, long id) {
//String companyName = _companyNames.get(position);
Intent intent = new Intent();
intent.setClass(CompaniesViewActivity.this, CompanyTabViewActivity.class);
startActivity(intent);
//myIntent.putExtra("companyName", companyName);
}
}
日志猫:
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): FATAL EXCEPTION: main
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): java.lang.RuntimeException: Unable to start activity ComponentInfo{gr.sigmasolutions/gr.sigmasolutions.CompanyTabViewActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{gr.sigmasolutions/gr.sigmasolutions.CompanyInfoViewActivity}: java.lang.NullPointerException
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.os.Looper.loop(Looper.java:123)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.main(ActivityThread.java:3647)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at java.lang.reflect.Method.invoke(Method.java:507)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at dalvik.system.NativeStart.main(Native Method)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{gr.sigmasolutions/gr.sigmasolutions.CompanyInfoViewActivity}: java.lang.NullPointerException
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1462)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.widget.TabHost.addTab(TabHost.java:216)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at gr.sigmasolutions.CompanyTabViewActivity.onCreate(CompanyTabViewActivity.java:36)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): ... 11 more
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): Caused by: java.lang.NullPointerException
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at gr.sigmasolutions.CompanyInfoViewActivity.showCompanyInfoServices(CompanyInfoViewActivity.java:54)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at gr.sigmasolutions.CompanyInfoViewActivity.onCreate(CompanyInfoViewActivity.java:32)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): ... 20 more
I have been trying with many solutions and combinations of code, but I always get my process forced to close in the application.. Do you have any suggestions?
public class CompaniesViewActivity extends ListActivity {
ArrayList<String> _listItems = new ArrayList<String>(); // Stores data from Company object
ArrayList<String> _companyNames = new ArrayList<String>();
ArrayAdapter<String> _adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.companiesview_layout); // Setting the appropriate layout
GlobalState globalState = (GlobalState)getApplicationContext(); // Setting the global vars
ArrayList<Company> companies = globalState.loadCompanies(); // Retrieving the companies list
// that was created during the
// first view
_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, _listItems);
// Constructing an ArrayAdapter in order to create a dynamic ListView
// associated with the _listItems Dynamic String Array List
setListAdapter(_adapter); // Setting the adapter
addItems(companies); // Calls the method to add the items in the ListView
}
// Adding items to the ListView dynamically for all the companies in the retrieved companies list
public void addItems(ArrayList<Company> companies) {
for (int i=0; i<companies.size(); i++) {
_listItems.add(companies.get(i).getName() + "\n" + "ΑΦΜ: " + companies.get(i).getVat());
_companyNames.add(companies.get(i).getName());
_adapter.notifyDataSetChanged(); // Refreshing the View with the data filled
}
}
public void onListItemClick(ListView l, View v, int position, long id) {
//String companyName = _companyNames.get(position);
Intent intent = new Intent();
intent.setClass(CompaniesViewActivity.this, CompanyTabViewActivity.class);
startActivity(intent);
//myIntent.putExtra("companyName", companyName);
}
}
Logcat:
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): FATAL EXCEPTION: main
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): java.lang.RuntimeException: Unable to start activity ComponentInfo{gr.sigmasolutions/gr.sigmasolutions.CompanyTabViewActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{gr.sigmasolutions/gr.sigmasolutions.CompanyInfoViewActivity}: java.lang.NullPointerException
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.os.Looper.loop(Looper.java:123)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.main(ActivityThread.java:3647)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at java.lang.reflect.Method.invoke(Method.java:507)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at dalvik.system.NativeStart.main(Native Method)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{gr.sigmasolutions/gr.sigmasolutions.CompanyInfoViewActivity}: java.lang.NullPointerException
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1462)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.widget.TabHost.addTab(TabHost.java:216)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at gr.sigmasolutions.CompanyTabViewActivity.onCreate(CompanyTabViewActivity.java:36)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): ... 11 more
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): Caused by: java.lang.NullPointerException
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at gr.sigmasolutions.CompanyInfoViewActivity.showCompanyInfoServices(CompanyInfoViewActivity.java:54)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at gr.sigmasolutions.CompanyInfoViewActivity.onCreate(CompanyInfoViewActivity.java:32)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
10-06 11:13:34.549: ERROR/AndroidRuntime(1569): ... 20 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Hear 是列表视图的最佳教程 http://www.vogella.de/articles/AndroidListView/文章.html
这可能有帮助...
Hear is the best tutorial for listview http://www.vogella.de/articles/AndroidListView/article.html
that might help...