com.example.appname意外停止

时间:2015-03-10 07:57:57

标签: android eclipse development-environment

这是我在android开发方面的第一份工作,所以如果问题很轻微或很简单,我会提前道歉。我得到的错误是

  

com.exmaple.appname意外停止了

我的应用程序包含3个java类和3个xml类:

爪哇

  • DataProvider.java
  • MainActivity.java
  • MoviesAdapter.java

XML

  • activity_main.xml中
  • child_layout.xml
  • parent_layout.xml

我的代码如下:

MainActivity.class:

package com.example.expandablelist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;

public class MainActivity extends ActionBarActivity implements OnClickListener{
    private static final Intent Intent = null;
    HashMap<String, List<String>> Movies_category;
    List<String> Movies_list;
    ExpandableListView Exp_list;
    MoviesAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Exp_list = (ExpandableListView) findViewById(R.id.exp_list);
    Movies_category = DataProvider.getInfo();
    Movies_list = new ArrayList<String>(Movies_category.keySet());
    adapter = new MoviesAdapter(this, Movies_category, Movies_list);
    Exp_list.setAdapter(adapter);
}

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub

}

}

DataProvide.java

 package com.example.expandablelist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DataProvider{

public static HashMap<String, List<String>> getInfo() 
{
    HashMap<String, List<String>> MoviesDetails = new HashMap<String, List<String>>();
            List<String> Action_Movies = new ArrayList<String>();
            Action_Movies.add("Action Movie 1");
            Action_Movies.add("Action Movie 2");
            Action_Movies.add("Action Movie 3");
            Action_Movies.add("Action Movie 4");

            List<String> Romantic_Movies = new ArrayList<String>();
            Action_Movies.add("Romantic Movie 1");
            Action_Movies.add("Romantic Movie 2");
            Action_Movies.add("Romantic Movie 3");
            Action_Movies.add("Romantic Movie 4");

            List<String> Horror_Movies = new ArrayList<String>();
            Action_Movies.add("Horror Movie 1");
            Action_Movies.add("Horror Movie 2");
            Action_Movies.add("Horror Movie 3");
            Action_Movies.add("Horro Movie 4");

            List<String> Comedy_Movies = new ArrayList<String>();
            Action_Movies.add("Comedy Movie 1");
            Action_Movies.add("Comedy Movie 2");
            Action_Movies.add("Comedy Movie 3");
            Action_Movies.add("Comedy Movie 4");

            MoviesDetails.put("Action Movies", Action_Movies);
            MoviesDetails.put("Romantic Movies", Romantic_Movies);
            MoviesDetails.put("Horror Movies", Horror_Movies);
            MoviesDetails.put("Comedy Movies", Comedy_Movies);

            return MoviesDetails;
            }

}

MoviesAdpater.java

package com.example.expandablelist;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MoviesAdapter extends BaseExpandableListAdapter{
    private Context ctx;
    private HashMap<String, List<String>> Movies_category;
    private List<String> Movies_List;

    public MoviesAdapter(Context ctx, HashMap<String, List<String>> Movies_category, List<String> Movies_List) 
    {
        this.ctx=ctx;
        this.Movies_category=Movies_category;
        this.Movies_List=Movies_List;
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return Movies_List.size();
    }

    @Override
    public int getChildrenCount(int arg0) {
        // TODO Auto-generated method stub
        return Movies_category.get(Movies_List.get(arg0)).size();
    }

    @Override
    public Object getGroup(int arg0) {
        // TODO Auto-generated method stub
        return Movies_List.get(arg0);
    }

    @Override
    public Object getChild(int parent, int child) {
        // 
        return Movies_category.get(Movies_List.get(parent)).get(child);
    }
//*********** Last thing I did - on video (part 2) until 7.15. **************
    @Override
    public long getGroupId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public long getChildId(int parent, int child) {
        // TODO Auto-generated method stub
        return child;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public View getGroupView(int parent, boolean isExpanded,
            View convertView, ViewGroup parentView) {
        // TODO Auto-generated method stub
        String group_title = (String) getGroup(parent);
        if(convertView == null)
        {
            LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.parent_layout, parentView,false);
        }
        TextView parent_textview = (TextView) convertView.findViewById(R.id.parent_txt);
        parent_textview.setTypeface(null, Typeface.BOLD);
        parent_textview.setText(group_title);
        return convertView;
    }

    @Override
    public View getChildView(int parent, int child, boolean lastChild, View convertView, ViewGroup parentview) 
    {
        String child_title = (String) getChild(parent, child);
        if (convertView == null)
        {
            LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.child_layout, parentview,false);
        }

        TextView child_textview = (TextView) convertView.findViewById(R.id.child_txt);
        child_textview.setText(child_title);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return false;
    }

}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<ExpandableListView
android:id="@+id/exp_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#A4C739"
android:dividerHeight="0.5dp"
android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft" 
android:layout_weight="1">

</ExpandableListView>

<Button
    android:id="@+id/btnSimple"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Next" />

child_layout.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="wrap_content"
        android:orientation="vertical" >

    <TextView
        android:id="@+id/child_txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"/>


</LinearLayout>

parent_layout.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" >

    <TextView
        android:id="@+id/parent_txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
        android:textColor="#A4C739" 
        android:paddingTop="10dp"
        android:paddingBottom="10dp"/>

</LinearLayout>

我感谢所有帮助。

提前致谢。

此致

Ĵ

1 个答案:

答案 0 :(得分:0)

此代码中的setContentView()在哪里,没有布局。你将如何获得ListView

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   //  add setContentView() here
   Exp_list = (ExpandableListView) findViewById(R.id.exp_list);
   Movies_category = DataProvider.getInfo();
   Movies_list = new ArrayList<String>(Movies_category.keySet());
   adapter = new MoviesAdapter(this, Movies_category, Movies_list);
   Exp_list.setAdapter(adapter);
}