不幸的是,android应用程序已停止

时间:2013-09-08 04:51:07

标签: android android-listview android-tabhost

我正在开发一个用于连接php和mssql与android的项目。每当我尝试调用一个类时,应用程序就会停止工作。看看下面的代码,并建议我做错了什么。 MainActivity.java

public class MainActivity extends Activity {
TabHost host;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Resources res = getResources();
    host = (TabHost) findViewById (R.id.tabhost);
    host.setup();

    TabSpec tspecMovies = host.newTabSpec("tag1");
    // tspecMovies.setContent(R.id.tab1);
    tspecMovies.setIndicator("Movies", res.getDrawable(R.drawable.movie_icon));
    Intent intentMovie = new Intent(this, MovieActivity.class);
    tspecMovies.setContent(intentMovie);
    host.addTab(tspecMovies);

    TabSpec tspecTv = host.newTabSpec("tag2");
    tspecTv.setContent(R.id.tab2);
    tspecTv.setIndicator("TV",  res.getDrawable(R.drawable.tv_icon));
    host.addTab(tspecTv);

    TabSpec tspecEvents = host.newTabSpec("tag3");
    tspecEvents.setContent(R.id.tab3);
    tspecEvents.setIndicator("Events",  res.getDrawable(R.drawable.event_icon));
    host.addTab(tspecEvents);

    for(int i=0;i<host.getTabWidget().getChildCount();i++){
                host.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#373737")); //unselected
                }
                host.getTabWidget().getChildAt(host.getCurrentTab()).setBackgroundColor(Color.parseColor("#717171"));// selected

    host.setOnTabChangedListener(new OnTabChangeListener(){
    @Override
        public void onTabChanged(String tabId) {
            for(int i=0;i<host.getTabWidget().getChildCount();i++){
                host.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#373737")); // unselected
                }
                host.getTabWidget().getChildAt(host.getCurrentTab()).setBackgroundColor(Color.parseColor("#717171"));// selected
    }});

}   

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

我目前正在使用电影标签。我想在通过调用以下MovieActivity点击Movies标签时列出我的tbl_cimemas表中的所有cinema_name。

public class MovieActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> productsList;

// url to get all products list
private static String url_all_cinemas = "http://localhost:8080/android/get_all_cinema.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CINEMAS = "tbl_cinemas";
private static final String TAG_CID = "cinema_id";
private static final String TAG_NAME = "cinema_name";

// products JSONArray
JSONArray products = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_cinema);

    // Hashmap for ListView
    productsList = new ArrayList<HashMap<String, String>>();

    // Loading products in Background Thread
    new LoadAllProducts().execute();
}

/**
 * Background Async Task to Load all product by making HTTP Request
 * */
class LoadAllProducts extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MovieActivity.this);
        pDialog.setMessage("Loading products. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_cinemas, "GET", params);

        // Check your log cat for JSON reponse
        Log.d("All Products: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_CINEMAS);

                // looping through All Products
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_CID);
                    String name = c.getString(TAG_NAME);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_CID, id);
                    map.put(TAG_NAME, name);

                    // adding HashList to ArrayList
                    productsList.add(map);
                }
            } else {
                // no products found
                // Launch Add New product Activity
                Intent i = new Intent(getApplicationContext(),
                        MovieActivity.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        MovieActivity.this, productsList,
                        R.layout.list_cinema, new String[] { TAG_CID,
                                TAG_NAME},
                        new int[] { R.id.cid, R.id.name });
                // updating listview
                setListAdapter(adapter);
            }
        });
      }
    }

all_cinema.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<!-- Main ListView
     Always give id value as list(@android:id/list)
-->
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

list_cinema.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<!-- Cinema id (cid) - will be HIDDEN - used to pass to other activity -->
            <TextView
                android:id="@+id/cid"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />

            <!-- Name Label -->
            <TextView
                android:id="@+id/name"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="6sp"
                android:paddingLeft="6sp"
                android:textSize="17sp"
                android:textStyle="bold" 
                android:clickable="true" 
                android:textIsSelectable="true" />
</LinearLayout>

logcat的

09-08 07:21:30.632: D/dalvikvm(778): GC_CONCURRENT freed 70K, 8% free 2735K/2948K,      paused 10ms+71ms, total 250ms
09-08 07:21:31.053: D/AndroidRuntime(778): Shutting down VM
09-08 07:21:31.083: W/dalvikvm(778): threadid=1: thread exiting with uncaught exception  (group=0x40a71930)
09-08 07:21:31.133: E/AndroidRuntime(778): FATAL EXCEPTION: main
09-08 07:21:31.133: E/AndroidRuntime(778): java.lang.RuntimeException: Unable to start         activity ComponentInfo{com.example.myandroidapp/com.example.myandroidapp.MainActivity}:     java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
09-08 07:21:31.133: E/AndroidRuntime(778):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-08 07:21:31.133: E/AndroidRuntime(778):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-08 07:21:31.133: E/AndroidRuntime(778):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-08 07:21:31.133: E/AndroidRuntime(778):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-08 07:21:31.133: E/AndroidRuntime(778):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 07:21:31.133: E/AndroidRuntime(778):  at android.os.Looper.loop(Looper.java:137)
09-08 07:21:31.133: E/AndroidRuntime(778):  at android.app.ActivityThread.main(ActivityThread.java:5041)
09-08 07:21:31.133: E/AndroidRuntime(778):  at java.lang.reflect.Method.invokeNative(Native Method)
09-08 07:21:31.133: E/AndroidRuntime(778):  at java.lang.reflect.Method.invoke(Method.java:511)
09-08 07:21:31.133: E/AndroidRuntime(778):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-08 07:21:31.133: E/AndroidRuntime(778):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-08 07:21:31.133: E/AndroidRuntime(778):  at dalvik.system.NativeStart.main(Native Method)
09-08 07:21:31.133: E/AndroidRuntime(778): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
09-08 07:21:31.133: E/AndroidRuntime(778):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:747)
09-08 07:21:31.133: E/AndroidRuntime(778):  at android.widget.TabHost.setCurrentTab(TabHost.java:413)
09-08 07:21:31.133: E/AndroidRuntime(778):  at android.widget.TabHost.addTab(TabHost.java:240)
09-08 07:21:31.133: E/AndroidRuntime(778):  at com.example.myandroidapp.MainActivity.onCreate(MainActivity.java:31)
09-08 07:21:31.133: E/AndroidRuntime(778):  at android.app.Activity.performCreate(Activity.java:5104)
09-08 07:21:31.133: E/AndroidRuntime(778):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-08 07:21:31.133: E/AndroidRuntime(778):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-08 07:21:31.133: E/AndroidRuntime(778):  ... 11 more
09-08 07:22:17.282: I/Process(778): Sending signal. PID: 778 SIG: 9

1 个答案:

答案 0 :(得分:0)

它的外观类似StackOverflow : llegalstateexception-at-tabhost, 您正在尝试扩展活动,尝试扩展TabActivity类

检查接受的答案。