Android编译但崩溃

时间:2015-06-13 18:00:53

标签: java android android-layout listview android-intent

我正在尝试开发我的第一个应用程序。它编译并安装但在我打开时崩溃。

这是代码

public class MainActivity extends ActionBarActivity implements OnItemClickListener {
private  ListView listview;
private DrawerLayout drawerlayout;
private ActionBarDrawerToggle drawerListener;
private LinearLayout menu;
private myAdapter myadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    listview=(ListView)findViewById(R.id.ListView);




    myadapter=new myAdapter(this);
    listview.setOnItemClickListener(this);
    listview.setAdapter(myadapter);
    drawerlayout=(DrawerLayout)findViewById(R.id.drawerLayout);

    drawerListener= new ActionBarDrawerToggle(this,drawerlayout,R.drawable.ic_ic_drawer,R.string.app_name, R.string.app_name){
            @Override
            public void onDrawerClosed(View drawerView) {

                Toast.makeText(MainActivity.this, "ola", Toast.LENGTH_LONG).show();
                menu=(LinearLayout)findViewById(R.id.menu_lateral);
                 menu.setVisibility(View.VISIBLE);
                super.onDrawerClosed(drawerView);

        }



            @Override
            public void onDrawerOpened(View drawerView) {
                menu=(LinearLayout)findViewById(R.id.menu_lateral);
                 menu.setVisibility(View.GONE);
                super.onDrawerOpened(drawerView);
            }
    };




    drawerlayout.setDrawerListener(drawerListener);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}


@Override
public void onPostCreate(Bundle savedInstanceState,
        PersistableBundle persistentState) {
    drawerListener.syncState();
    super.onPostCreate(savedInstanceState, persistentState);
}

@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) {
if(drawerListener.onOptionsItemSelected(item)){
    return true;
}
    return super.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    drawerListener.onConfigurationChanged(newConfig);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {

    class myAdapter extends BaseAdapter{
private Context context;
View row=null;
String [] menu;
int [] images ={R.drawable.ic_contact, R.drawable.menu, R.drawable.ic_reservas, R.drawable.ic_map, R.drawable.galeria, R.drawable.info, R.drawable.facebook };

public myAdapter(Context context) {
menu=context.getResources().getStringArray(R.array.slide);
this.context=context;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return menu.length;
}
@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return menu[position];
}
@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView==null){
        View row=null;
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=inflater.inflate(R.layout.custom_row, parent, false);
    }
    else
    {
        row=convertView;
    }

    TextView titulo=(TextView) row.findViewById(R.id.textView1);
    ImageView icone=(ImageView) row.findViewById(R.id.imageView1);

    titulo.setText(menu[position]);
    icone.setImageResource(images[position]);
    return row;
}

这是日志猫

    06-13 18:55:12.842: D/OpenGLRenderer(4763): Render dirty regions requested: true
06-13 18:55:12.862: D/Atlas(4763): Validating map...
06-13 18:55:12.942: I/Adreno-EGL(4763): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.AF.1.1.04.04.03.164.081_msm8960_LA.AF.1.1__release_AU ()
06-13 18:55:12.942: I/Adreno-EGL(4763): OpenGL ES Shader Compiler Version: E031.25.01.03
06-13 18:55:12.942: I/Adreno-EGL(4763): Build Date: 11/02/14 Sun
06-13 18:55:12.942: I/Adreno-EGL(4763): Local Branch: mybranch5454281
06-13 18:55:12.942: I/Adreno-EGL(4763): Remote Branch: quic/l_LNX.LA.3.6
06-13 18:55:12.942: I/Adreno-EGL(4763): Local Patches: NONE
06-13 18:55:12.942: I/Adreno-EGL(4763): Reconstruct Branch: AU_LINUX_ANDROID_LA.AF.1.1.04.04.03.164.081 + db92d1e + cb8de02 +  NOTHING
06-13 18:55:12.942: I/OpenGLRenderer(4763): Initialized EGL, version 1.4
06-13 18:55:12.972: D/OpenGLRenderer(4763): Enabling debug mode 0
06-13 18:55:13.072: D/AndroidRuntime(4763): Shutting down VM
06-13 18:55:13.102: E/AndroidRuntime(4763): FATAL EXCEPTION: main
06-13 18:55:13.102: E/AndroidRuntime(4763): Process: com.example.alambique, PID: 4763
06-13 18:55:13.102: E/AndroidRuntime(4763): java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
06-13 18:55:13.102: E/AndroidRuntime(4763):     at com.example.alambique.myAdapter.getView(MainActivity.java:155)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.AbsListView.obtainView(AbsListView.java:2346)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.ListView.makeAndAddView(ListView.java:1864)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.ListView.fillDown(ListView.java:698)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.ListView.fillFromTop(ListView.java:759)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.ListView.layoutChildren(ListView.java:1673)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.AbsListView.onLayout(AbsListView.java:2150)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.View.layout(View.java:15596)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.ViewGroup.layout(ViewGroup.java:4966)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:931)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.View.layout(View.java:15596)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.ViewGroup.layout(ViewGroup.java:4966)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.View.layout(View.java:15596)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.ViewGroup.layout(ViewGroup.java:4966)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.View.layout(View.java:15596)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.ViewGroup.layout(ViewGroup.java:4966)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.View.layout(View.java:15596)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.ViewGroup.layout(ViewGroup.java:4966)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.View.layout(View.java:15596)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.ViewGroup.layout(ViewGroup.java:4966)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.View.layout(View.java:15596)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.ViewGroup.layout(ViewGroup.java:4966)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.View.layout(View.java:15596)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.ViewGroup.layout(ViewGroup.java:4966)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5786)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.Choreographer.doCallbacks(Choreographer.java:580)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.Choreographer.doFrame(Choreographer.java:550)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.os.Handler.handleCallback(Handler.java:739)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.os.Looper.loop(Looper.java:135)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at android.app.ActivityThread.main(ActivityThread.java:5256)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at java.lang.reflect.Method.invoke(Native Method)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at java.lang.reflect.Method.invoke(Method.java:372)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
06-13 18:55:13.102: E/AndroidRuntime(4763):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
06-13 18:55:13.162: I/Process(4763): Sending signal. PID: 4763 SIG: 9

2 个答案:

答案 0 :(得分:0)

请用以下代码替换getView我希望它能正常工作

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder = null;
    if (convertView == null) {
        mViewHolder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         row=inflater.inflate(R.layout.custom_row, parent, false);
        mViewHolder.titulo= (TextView) row.findViewById(R.id.textView1);
        mViewHolder.icone= (ImageView) row.findViewById(R.id.imageView1);
        convertView.setTag(mViewHolder);
    } else {
        mViewHolder = (ViewHolder) convertView.getTag();
    }
titulo.setText(menu[position]);
icone.setImageResource(images[position]);
return convertView ;
}

在您的adpater中使用ViewHolder Class

static class ViewHolder {
private TextView titulo;
private ImageView icone;
}

答案 1 :(得分:0)

View row=null;

if{}块中定义为局部变量两次,在适配器中定义为类字段。行变量在这里:

TextView titulo=(TextView) row.findViewById(R.id.textView1);
ImageView icone=(ImageView) row.findViewById(R.id.imageView1);

被声明为适配器字段,分配给null(可能是为了避免编译器抱怨),然后再也没有触及过。解决方案 - 只需以适当的方式声明变量。