我尝试开发一个Android应用程序 - 包含三个片段(A,B& C)的Swipe Tabs。它适用于我的代码。下一步我尝试在片段A中添加列表视图。
然后我尝试填充Main Activity.java文件中的列表视图。这是我的错误。在我的MainActivity.java文件中,有两个类用作适配器。错误是所有关于运行时,我的应用程序不幸被停止!!!
以下是我使用的代码。
package com.techcruise.swipe;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.app.Fragment;
import android.content.Context;
import android.content.res.Resources;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
ActionBar actionBar;
ViewPager viewpager;
ListView list;
String[] titles;
String[] descr;
int[] images = {R.drawable.aavishkaar,R.drawable.addidas,R.drawable.addons_women,R.drawable.airtel,R.drawable.allen_solly,R.drawable.archies,R.drawable.arrow,R.drawable.basics_life,R.drawable.bata,R.drawable.beanstalk,R.drawable.biba,R.drawable.black_berrys,R.drawable.casio,R.drawable.citibank_atm,R.drawable.clarks,R.drawable.classic_polo,R.drawable.cocktail,R.drawable.crocodile,R.drawable.crusoe,R.drawable.csb_atm,R.drawable.cub_atm,R.drawable.dar_jewellery,R.drawable.derby,R.drawable.diva,R.drawable.druken,R.drawable.faces_canada,R.drawable.fashion_place,R.drawable.funskool,R.drawable.giny_jony,R.drawable.gocolors,R.drawable.green_gold_store,R.drawable.hidesign,R.drawable.home_centre,R.drawable.hp_world,R.drawable.indian_terrian,R.drawable.iob,R.drawable.isha_shop,R.drawable.jashn,R.drawable.jockey,R.drawable.kvb_atm,R.drawable.lavie,R.drawable.lawrence_mayo,R.drawable.lee,R.drawable.legends,R.drawable.levis,R.drawable.lifestyle,R.drawable.maybell,R.drawable.mehandi,R.drawable.methiyadi,R.drawable.mochi,R.drawable.mom_me,R.drawable.mufti,R.drawable.naturals_longue,R.drawable.new_indian_emporium,R.drawable.nike,R.drawable.nokia,R.drawable.odyssey,R.drawable.otto,R.drawable.pantaloons,R.drawable.pepe_jeans,R.drawable.peter_england,R.drawable.planet_sports,R.drawable.poorvika,R.drawable.propel_fitness,R.drawable.puma,R.drawable.queens_ethinic_trends,R.drawable.rathna_video_audio,R.drawable.relaince_footprint,R.drawable.relaince_trends,R.drawable.reynolds_write_site,R.drawable.rmkv_kids,R.drawable.rmkv,R.drawable.safari,R.drawable.samsonite,R.drawable.samsung_smart_phine_cafe,R.drawable.savithri_photo_house,R.drawable.sbi_atm,R.drawable.soch,R.drawable.sony_vaio,R.drawable.spar,R.drawable.spice_hotspot,R.drawable.sss_car_spa,R.drawable.sthri_elite,R.drawable.sting,R.drawable.the_chennai_mobiles,R.drawable.titan,R.drawable.united_colors,R.drawable.van_heusen,R.drawable.westside,R.drawable.woodland,R.drawable.wrangler,R.drawable.zimson};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
titles= res.getStringArray(R.array.title);
descr = res.getStringArray(R.array.desc);
list = (ListView) findViewById(R.id.listView1);
ivAdapter adapter = new ivAdapter(this,titles,images,descr);
list.setAdapter(adapter);
viewpager = (ViewPager) findViewById (R.id.pager);
viewpager.setAdapter(new VineshAdapter(getSupportFragmentManager()));
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
Log.d("techcruise","onPageSelected at "+" position "+arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
Log.d("techcruise","onPageScrolled at "+" position "+arg0+" from "+arg1+" with number of pixels="+arg2);
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
if(arg0==ViewPager.SCROLL_STATE_IDLE)
{
Log.d("techcruise", "onPageScrollStateChanged Idle");
}
if(arg0==ViewPager.SCROLL_STATE_DRAGGING)
{
Log.d("techcruise", "onPageScrollStateChanged Dragging");
}
if(arg0==ViewPager.SCROLL_STATE_SETTLING)
{
Log.d("techcruise", "onPageScrollStateChanged Settling");
}
}
});
}
}
class ivAdapter extends ArrayAdapter<String>
{
Context context;
int[] images;
String[] mytitle;
String[] mydescp;
ivAdapter(Context c, String[] tittles, int[] imgs, String[] desc)
{
super(c, R.layout.single_row, R.id.listView1, tittles);
this.context=c;
this.images=imgs;
this.mytitle= tittles;
this.mydescp=desc;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
try
{
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflator.inflate(R.layout.single_row, parent, false);
ImageView myImage = (ImageView) row.findViewById(R.id.imageViewsr);
TextView myText = (TextView) row.findViewById(R.id.textViewsr);
TextView mydesc = (TextView) row.findViewById(R.id.textViewsr1);
myImage.setImageResource(images[position]);
myText.setText(mytitle[position]);
mydesc.setText(mydescp[position]);
return row;
}
catch (Exception ex)
{
return convertView;
}
}
}
class VineshAdapter extends FragmentPagerAdapter
{
public VineshAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public android.support.v4.app.Fragment getItem(int arg0) {
// TODO Auto-generated method stub
android.support.v4.app.Fragment fragment=null;
if(arg0==0)
{
fragment=new FragmentA();
}
if(arg0==1)
{
fragment =new FragmentB();
}
if(arg0==2)
{
fragment =new FragmentC();
}
return fragment;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
我的应用程序有替代方法吗? 需要帮助??
以下是LogCAt:
03-13 11:58:56.186: D/ActivityThread(30403): hoder:android.app.IActivityManager$ContentProviderHolder@41d867c8,provider,holder.Provider:android.content.ContentProviderProxy@41d86f40
03-13 11:58:56.189: D/asset(30403): AssetManager-->addDefaultAssets CIP path not exsit!
03-13 11:58:56.192: D/Proxy(30403): setHttpRequestCheckHandler
03-13 11:58:56.193: D/dalvikvm(30403): open_cached_dex_file : /data/app/com.techcruise.swipe-2.apk /data/dalvik-cache/data@app@com.techcruise.swipe-2.apk@classes.dex
03-13 11:58:56.196: D/ActivityThread(30403): BIND_APPLICATION handled : 0 / AppBindData{appInfo=ApplicationInfo{41d84120 com.techcruise.swipe}}
03-13 11:58:56.341: D/AndroidRuntime(30403): Shutting down VM
03-13 11:58:56.341: W/dalvikvm(30403): threadid=1: thread exiting with uncaught exception (group=0x41a2fc98)
03-13 11:58:56.341: W/dalvikvm(30403): threadid=1: uncaught exception occurred
03-13 11:58:56.342: W/System.err(30403): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.techcruise.swipe/com.techcruise.swipe.MainActivity}: java.lang.NullPointerException
03-13 11:58:56.342: W/System.err(30403): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
03-13 11:58:56.342: W/System.err(30403): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
03-13 11:58:56.343: W/System.err(30403): at android.app.ActivityThread.access$800(ActivityThread.java:151)
03-13 11:58:56.343: W/System.err(30403): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
03-13 11:58:56.343: W/System.err(30403): at android.os.Handler.dispatchMessage(Handler.java:110)
03-13 11:58:56.343: W/System.err(30403): at android.os.Looper.loop(Looper.java:193)
03-13 11:58:56.343: W/System.err(30403): at android.app.ActivityThread.main(ActivityThread.java:5292)
03-13 11:58:56.343: W/System.err(30403): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 11:58:56.343: W/System.err(30403): at java.lang.reflect.Method.invoke(Method.java:515)
03-13 11:58:56.344: W/System.err(30403): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
03-13 11:58:56.344: W/System.err(30403): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
03-13 11:58:56.344: W/System.err(30403): at dalvik.system.NativeStart.main(Native Method)
03-13 11:58:56.344: W/System.err(30403): Caused by: java.lang.NullPointerException
03-13 11:58:56.345: W/System.err(30403): at com.techcruise.swipe.MainActivity.onCreate(MainActivity.java:49)
03-13 11:58:56.345: W/System.err(30403): at android.app.Activity.performCreate(Activity.java:5264)
03-13 11:58:56.345: W/System.err(30403): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
03-13 11:58:56.346: W/System.err(30403): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
03-13 11:58:56.346: W/System.err(30403): ... 11 more
03-13 11:58:56.346: W/dalvikvm(30403): threadid=1: calling UncaughtExceptionHandler
03-13 11:58:56.347: E/AndroidRuntime(30403): FATAL EXCEPTION: main
03-13 11:58:56.347: E/AndroidRuntime(30403): Process: com.techcruise.swipe, PID: 30403
03-13 11:58:56.347: E/AndroidRuntime(30403): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.techcruise.swipe/com.techcruise.swipe.MainActivity}: java.lang.NullPointerException
03-13 11:58:56.347: E/AndroidRuntime(30403): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
03-13 11:58:56.347: E/AndroidRuntime(30403): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
03-13 11:58:56.347: E/AndroidRuntime(30403): at android.app.ActivityThread.access$800(ActivityThread.java:151)
03-13 11:58:56.347: E/AndroidRuntime(30403): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
03-13 11:58:56.347: E/AndroidRuntime(30403): at android.os.Handler.dispatchMessage(Handler.java:110)
03-13 11:58:56.347: E/AndroidRuntime(30403): at android.os.Looper.loop(Looper.java:193)
03-13 11:58:56.347: E/AndroidRuntime(30403): at android.app.ActivityThread.main(ActivityThread.java:5292)
03-13 11:58:56.347: E/AndroidRuntime(30403): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 11:58:56.347: E/AndroidRuntime(30403): at java.lang.reflect.Method.invoke(Method.java:515)
03-13 11:58:56.347: E/AndroidRuntime(30403): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
03-13 11:58:56.347: E/AndroidRuntime(30403): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
03-13 11:58:56.347: E/AndroidRuntime(30403): at dalvik.system.NativeStart.main(Native Method)
03-13 11:58:56.347: E/AndroidRuntime(30403): Caused by: java.lang.NullPointerException
03-13 11:58:56.347: E/AndroidRuntime(30403): at com.techcruise.swipe.MainActivity.onCreate(MainActivity.java:49)
03-13 11:58:56.347: E/AndroidRuntime(30403): at android.app.Activity.performCreate(Activity.java:5264)
03-13 11:58:56.347: E/AndroidRuntime(30403): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
03-13 11:58:56.347: E/AndroidRuntime(30403): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
03-13 11:58:56.347: E/AndroidRuntime(30403): ... 11 more
答案 0 :(得分:1)
-Hey! Vinesh。你得到空指针异常&#34; 49&#34;可能在setadapter的行检查你&#34;标题,图像,描述&#34;获取null检查你得到的是什么。
-Thanks
答案 1 :(得分:0)
你的logcat在第49行显示NullPointerException。也许你正在使用的列表对象为null,因为你的错误id.Check是否有效。