为什么当 Activity 位于 ListActivity 上时会崩溃,但当 Activity 位于 Activity 上时却工作正常

发布于 2024-11-26 15:47:03 字数 3921 浏览 0 评论 0原文

public class test extends ListActivity{
/** Called when the activity is first created. */
protected Dialog mSplashDialog;
private static final String[] country = { "Iceland", "India", "Indonesia","Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia","Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania","Luxembourg" };
private static final String[] curr = { "ISK", "INR", "IDR", "IRR", "IQD","EUR", "ILS", "EUR", "LAK", "LVL", "LBP", "LSL ", "LRD", "LYD","LTL ", "EUR"};    

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MyStateSaver data = (MyStateSaver) getLastNonConfigurationInstance();
    if (data != null) {
        // Show splash screen if still loading
        if (data.showSplashScreen) {
            showSplashScreen();
        }
        setContentView(R.layout.main);        

        // Rebuild your UI with your saved state here
    } else { 
        showSplashScreen();
        setContentView(R.layout.main);
        ListView l1 = (ListView) findViewById(R.id.list);
        l1.setAdapter(new EfficientAdapter(this)); 
        l1.setItemsCanFocus(true);
        }
    }

@Override
public Object onRetainNonConfigurationInstance() {
    MyStateSaver data = new MyStateSaver();
    // Save your important data here

    if (mSplashDialog != null) {
        data.showSplashScreen = true;
        removeSplashScreen();
    }
    return data;
}

/**
 * Removes the Dialog that displays the splash screen
 */
protected void removeSplashScreen() {
    if (mSplashDialog != null) {
        mSplashDialog.dismiss();
        mSplashDialog = null;
    }
}

/**
 * Shows the splash screen over the full Activity
 */
protected void showSplashScreen() {
    mSplashDialog = new Dialog(this, R.style.SplashScreen);
    mSplashDialog.setContentView(R.layout.splashscreen);
    mSplashDialog.setCancelable(false);
    mSplashDialog.show();

    // Set Runnable to remove splash screen just in case
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        removeSplashScreen();
      }
    }, 3000);
}

/**
 * Simple class for storing important data across config changes
 */
private class MyStateSaver {
    public boolean showSplashScreen = false;
    // Your other important fields here
}
public static class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

public EfficientAdapter(Context context) {
    mInflater = LayoutInflater.from(context);   
}
public int getCount() {
    return country.length;
    }
public Object getItem(int position) {
    return position;
    }
public long getItemId(int position) {
    return position;
    }
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.listview, null);
        convertView.setClickable(true);
        convertView.setFocusable(true);

        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.TextView01);
        holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);
        holder.text3 = (TextView) convertView.findViewById(R.id.TextView03);
        holder.text4 = (TextView) convertView.findViewById(R.id.TextView04);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.text.setText(curr[position]);
    holder.text2.setText(country[position]);
    holder.text3.setText(country[position]);
    holder.text4.setText(country[position]);


    return convertView;
    }
    static class ViewHolder {
        TextView text4;
        TextView text;
        TextView text2;
        TextView text3;
}
}
public void onClick(View v) {
    Toast.makeText(getApplicationContext(), "Zipcode: ", Toast.LENGTH_SHORT).show();

}
};
public class test extends ListActivity{
/** Called when the activity is first created. */
protected Dialog mSplashDialog;
private static final String[] country = { "Iceland", "India", "Indonesia","Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia","Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania","Luxembourg" };
private static final String[] curr = { "ISK", "INR", "IDR", "IRR", "IQD","EUR", "ILS", "EUR", "LAK", "LVL", "LBP", "LSL ", "LRD", "LYD","LTL ", "EUR"};    

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MyStateSaver data = (MyStateSaver) getLastNonConfigurationInstance();
    if (data != null) {
        // Show splash screen if still loading
        if (data.showSplashScreen) {
            showSplashScreen();
        }
        setContentView(R.layout.main);        

        // Rebuild your UI with your saved state here
    } else { 
        showSplashScreen();
        setContentView(R.layout.main);
        ListView l1 = (ListView) findViewById(R.id.list);
        l1.setAdapter(new EfficientAdapter(this)); 
        l1.setItemsCanFocus(true);
        }
    }

@Override
public Object onRetainNonConfigurationInstance() {
    MyStateSaver data = new MyStateSaver();
    // Save your important data here

    if (mSplashDialog != null) {
        data.showSplashScreen = true;
        removeSplashScreen();
    }
    return data;
}

/**
 * Removes the Dialog that displays the splash screen
 */
protected void removeSplashScreen() {
    if (mSplashDialog != null) {
        mSplashDialog.dismiss();
        mSplashDialog = null;
    }
}

/**
 * Shows the splash screen over the full Activity
 */
protected void showSplashScreen() {
    mSplashDialog = new Dialog(this, R.style.SplashScreen);
    mSplashDialog.setContentView(R.layout.splashscreen);
    mSplashDialog.setCancelable(false);
    mSplashDialog.show();

    // Set Runnable to remove splash screen just in case
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        removeSplashScreen();
      }
    }, 3000);
}

/**
 * Simple class for storing important data across config changes
 */
private class MyStateSaver {
    public boolean showSplashScreen = false;
    // Your other important fields here
}
public static class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

public EfficientAdapter(Context context) {
    mInflater = LayoutInflater.from(context);   
}
public int getCount() {
    return country.length;
    }
public Object getItem(int position) {
    return position;
    }
public long getItemId(int position) {
    return position;
    }
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.listview, null);
        convertView.setClickable(true);
        convertView.setFocusable(true);

        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.TextView01);
        holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);
        holder.text3 = (TextView) convertView.findViewById(R.id.TextView03);
        holder.text4 = (TextView) convertView.findViewById(R.id.TextView04);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.text.setText(curr[position]);
    holder.text2.setText(country[position]);
    holder.text3.setText(country[position]);
    holder.text4.setText(country[position]);


    return convertView;
    }
    static class ViewHolder {
        TextView text4;
        TextView text;
        TextView text2;
        TextView text3;
}
}
public void onClick(View v) {
    Toast.makeText(getApplicationContext(), "Zipcode: ", Toast.LENGTH_SHORT).show();

}
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-12-03 15:47:03

如果你从 ListActivity 扩展,你需要在你的列表布局上添加 android:id="@android:id/list" 这样的东西

<LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <ListView android:id="@android:id/list" android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
    </LinearLayout>

我可能是错的,但如果你提供 LogCat 跟踪和你的布局我可以给你更好的建议。

If yuo are extending from ListActivity you need to add android:id="@android:id/list" on your list layout something like this

<LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <ListView android:id="@android:id/list" android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
    </LinearLayout>

I could be wrong but if you provide a LogCat trace and your layout i can give you a better advice.

﹏雨一样淡蓝的深情 2024-12-03 15:47:03

我认为您正在尝试在设置 ContentView 之前显示一个对话框

尝试编写此行 setContentView(R.layout.main);在 showSplashScreen() 之前

I think you are trying to show a Dialog before setting the ContentView

Try writing this line setContentView(R.layout.main); before showSplashScreen()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文