错误按钮MainActivity Android

时间:2015-02-17 20:39:20

标签: java android

我希望你能帮我解决这个错误。

我想将一个活动与一个按钮相关联,但我在logcat中有这个错误(已更新)。

02-17 18:04:27.033: D/AndroidRuntime(1850): Shutting down VM
02-17 18:04:27.034: E/AndroidRuntime(1850): FATAL EXCEPTION: main
02-17 18:04:27.034: E/AndroidRuntime(1850): Process: com.cm.georganizer, PID: 1850
02-17 18:04:27.034: E/AndroidRuntime(1850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cm.georganizer/com.cm.georganizer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
02-17 18:04:27.034: E/AndroidRuntime(1850):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
02-17 18:04:27.034: E/AndroidRuntime(1850):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-17 18:04:27.034: E/AndroidRuntime(1850):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-17 18:04:27.034: E/AndroidRuntime(1850):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-17 18:04:27.034: E/AndroidRuntime(1850):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-17 18:04:27.034: E/AndroidRuntime(1850):     at android.os.Looper.loop(Looper.java:135)
02-17 18:04:27.034: E/AndroidRuntime(1850):     at android.app.ActivityThread.main(ActivityThread.java:5221)
02-17 18:04:27.034: E/AndroidRuntime(1850):     at java.lang.reflect.Method.invoke(Native Method)
02-17 18:04:27.034: E/AndroidRuntime(1850):     at java.lang.reflect.Method.invoke(Method.java:372)
02-17 18:04:27.034: E/AndroidRuntime(1850):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-17 18:04:27.034: E/AndroidRuntime(1850):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-17 18:04:27.034: E/AndroidRuntime(1850): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
02-17 18:04:27.034: E/AndroidRuntime(1850):     at com.cm.georganizer.MainActivity.onCreate(MainActivity.java:47)
02-17 18:04:27.034: E/AndroidRuntime(1850):     at android.app.Activity.performCreate(Activity.java:5933)
02-17 18:04:27.034: E/AndroidRuntime(1850):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-17 18:04:27.034: E/AndroidRuntime(1850):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-17 18:04:27.034: E/AndroidRuntime(1850):     ... 10 more
02-17 18:09:27.363: I/Process(1850): Sending signal. PID: 1850 SIG: 9

这是我的MainActivity(已更新)

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.SparseIntArray;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

import android.database.Cursor;
import android.support.v7.app.ActionBarActivity;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

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

import br.liveo.interfaces.NavigationLiveoListener;
import br.liveo.navigationliveo.NavigationLiveo;


public class MainActivity extends NavigationLiveo implements NavigationLiveoListener {


    Button btnAgregarMiembro;
    ListView lista;
    SQLControlador dbconeccion;
    TextView tv_miemID, tv_miemNombre;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        dbconeccion = new SQLControlador(this);
        dbconeccion.abrirBaseDeDatos();
        btnAgregarMiembro = (Button) findViewById(R.id.btnAgregarMiembro);
        lista = (ListView) findViewById(R.id.listViewMiembros);

        //acción del boton agregar miembro
        btnAgregarMiembro.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent iagregar = new Intent(MainActivity.this, AgregarMiembro.class);
                startActivity(iagregar);
            }
        });

        // Tomar los datos desde la base de datos para poner en el curso y después en el adapter
        Cursor cursor = dbconeccion.leerDatos();

        String[] from = new String[] {
                DBhelper.MIEMBRO_ID,
                DBhelper.MIEMBRO_NOMBRE
        };
        int[] to = new int[] {
                R.id.miembro_id,
                R.id.miembro_nombre
        };

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                MainActivity.this, R.layout.formato_fila, cursor, from, to);

        adapter.notifyDataSetChanged();
        lista.setAdapter(adapter);

        // acción cuando hacemos click en item para poder modificarlo o eliminarlo
        lista.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {

                tv_miemID = (TextView) view.findViewById(R.id.miembro_id);
                tv_miemNombre = (TextView) view.findViewById(R.id.miembro_nombre);

                String aux_miembroId = tv_miemID.getText().toString();
                String aux_miembroNombre = tv_miemNombre.getText().toString();

                Intent modify_intent = new Intent(getApplicationContext(), ModificarMiembro.class);
                modify_intent.putExtra("miembroId", aux_miembroId);
                modify_intent.putExtra("miembroNombre", aux_miembroNombre);
                startActivity(modify_intent);
            }
        });
    }  //termina el onCreate



    public List<String> mListNameItem;

    @Override
    public void onUserInformation() {
        //User information here
        //this.mUserName.setText("User");
        //this.mUserEmail.setText("rudsonlive@gmail.com");
        //this.mUserPhoto.setImageResource(R.drawable.ic_rudsonlive);
        this.mUserBackground.setImageResource(R.drawable.ic_app_background);
    }

    @Override
    public void onInt(Bundle savedInstanceState) {
        //Creation of the list items is here

        // set listener {required}
        this.setNavigationListener(this);

        //First item of the position selected from the list
        this.setDefaultStartPositionNavigation(0);

        // name of the list items
        mListNameItem = new ArrayList<>();
        mListNameItem.add(0, getString(R.string.index));
        mListNameItem.add(1, getString(R.string.add_event));

        // icons list items
        List<Integer> mListIconItem = new ArrayList<>();
        mListIconItem.add(0, R.drawable.ic_inbox_black_24dp);
        mListIconItem.add(1, R.drawable.ic_star_black_24dp); //Item no icon set 0

        //{optional} - Among the names there is some subheader, you must indicate it here
        List<Integer> mListHeaderItem = new ArrayList<>();
        mListHeaderItem.add(4);

        //{optional} - Among the names there is any item counter, you must indicate it (position) and the value here
        SparseIntArray mSparseCounterItem = new SparseIntArray(); //indicate all items that have a counter
        mSparseCounterItem.put(0, 0);
        mSparseCounterItem.put(1, 0);

        //If not please use the FooterDrawer use the setFooterVisible(boolean visible) method with value false
        //this.setFooterInformationDrawer(R.string.settings, R.drawable.ic_settings_black_24dp);

        this.setNavigationAdapter(mListNameItem, mListIconItem, mListHeaderItem, mSparseCounterItem);
    }

    @Override
    public void onItemClickNavigation(int position, int layoutContainerId) {

        FragmentManager mFragmentManager = getSupportFragmentManager();

        Fragment mFragment = new FragmentMain().newInstance(mListNameItem.get(position));

        switch (position) {
            case 0:
                mFragment = new FragmentMain().newInstance(mListNameItem.get(position));
                break;

            case 1:
                mFragment = new FragmentEvent().newInstance(mListNameItem.get(position));
                break;
        }

        if (mFragment != null){
            mFragmentManager.beginTransaction().replace(layoutContainerId, mFragment).commit();
        }
    }

    @Override
    public void onPrepareOptionsMenuNavigation(Menu menu, int position, boolean visible) {

        //hide the menu when the navigation is opens
        switch (position) {
            case 0:
                menu.findItem(R.id.menu_add).setVisible(!visible);
                menu.findItem(R.id.menu_search).setVisible(!visible);
                break;

            case 1:
                menu.findItem(R.id.menu_add).setVisible(!visible);
                menu.findItem(R.id.menu_search).setVisible(!visible);
                break;
        }
    }

    @Override
    public void onClickUserPhotoNavigation(View v) {
        //user photo onClick
        Toast.makeText(this, R.string.open_user_profile, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClickFooterItemNavigation(View v) {
        //footer onClick
        startActivity(new Intent(this, SettingsActivity.class));
    }
}

fragment_main.xml

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

    <ImageView
        android:id="@+id/imgLiveo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/clear"
        android:src="@drawable/logo" />

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imgLiveo"
        android:gravity="center"
        android:layout_marginTop="16dp"
        android:text="@string/clear"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/btnAgregarMiembro"
        android:layout_width="fill_parent"
        android:layout_below="@+id/txtTitle"
        android:layout_height="wrap_content"
        android:text="Agregar miembro" />

    <ListView
        android:id="@+id/listViewMiembros"
        android:layout_width="match_parent"
        android:layout_below="@+id/btnAgregarMiembro"
        android:layout_height="wrap_content"
        android:dividerHeight="2dp" >
    </ListView>

</RelativeLayout>

我已多次尝试修复它但不起作用

事先非常感谢你的帮助

0 个答案:

没有答案