如何用coordinatorview显示snackbar?

时间:2015-10-04 01:41:06

标签: java android

所以我试图从协调器视图创建一个快餐栏,但是当我尝试显示它时它会崩溃。看一下logcat,它说错误是因为视图为空。但我发现它是由id发现的,所以我不确定为什么会这样。非常感谢,尼克。

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/snackbarPosition"
    xmlns:android="http://schemas.android.com/apk/res/android">
</android.support.design.widget.CoordinatorLayout>

以下是代码:

public class ViewCodeActivity extends AppCompatActivity {

    ManagerDatabaseAdapter managerHelper;
    public static final String EXTRA_MESSAGE = "com.nick.mowen.receiptmanager.LOCATION";
    String[] names;
    View coordinatorView;
    Snackbar snackbar;
    TextView Main, Address;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewcode);
        managerHelper = new ManagerDatabaseAdapter(this);
        Intent intent = getIntent();
        names = intent.getStringArrayExtra(EXTRA_MESSAGE);
        Main = (TextView)findViewById(R.id.mainText);
        Address = (TextView)findViewById(R.id.addressText);
        image = (ImageView)findViewById(R.id.pictureViewCode);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                String sd = Environment.getExternalStorageDirectory().toString();
                intent.setDataAndType(Uri.parse("file://" + sd + names[1] + "/" + names[0] + ".png"), "image/*");
                startActivity(intent);
            }
        });
        Main.setText(names[0]);
        Address.setText(names[2]);
        getImage(names[1], names[0]);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_codeview, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.delete:
                finishDeleting(names[0]);
                return true;
            case R.id.edit:
                updateName();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void finishDeleting(String name) {
        long id = managerHelper.deleteRow(name);

        if (id<0) {
            Message.message(this, "Delete Failed");
        }
        else {
            coordinatorView = findViewById(R.id.snackbarPosition);
            Snackbar.make(coordinatorView, R.string.deleted, Snackbar.LENGTH_SHORT)
                    .setAction(R.string.Undo, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            managerHelper.insertData(names[0], "/Receipt_Codes", "Not Set");
                        }
                    }).setActionTextColor(getColor(R.color.accent_app)).show();
        }

        finish();
    }

这是logcat:

10-03 20:33:17.941 7823-7823/com.nick.mowen.receiptmanager E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.view.View.getResources()' on a null object reference
10-03 20:33:17.941 7823-7823/com.nick.mowen.receiptmanager E/AndroidRuntime:     at android.support.design.widget.Snackbar.make(Snackbar.java:231)
10-03 20:33:17.941 7823-7823/com.nick.mowen.receiptmanager E/AndroidRuntime:     at com.nick.mowen.receiptmanager.ViewCodeActivity.finishDeleting(ViewCodeActivity.java:93)

0 个答案:

没有答案