调用一个活动多布局在2个不同的回调 [英] Calling Multiple Layouts in One Activity on 2 different Call Backs

查看:158
本文介绍了调用一个活动多布局在2个不同的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是什么做的是,我初始化我的活动开始布局。每当上布局的按钮被点击我在同一个活动初始化另一个布局。每当我点击我做一个数据库记录的一些工作有关插入。甚至忽略了数据库的一部分我在logcat中获得的Java空指针异常。请看看,引导我

 包com.example.emp_management;进口android.app.Activity;
进口android.content.ContentValues​​;
进口android.database.sqlite.SQLiteDatabase;
进口android.os.Bundle;
进口android.provider.SyncStateContract.Columns;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;
进口com.example.emp_management.DatabaseHelper;公共类Administrator_Work延伸活动{@覆盖
保护无效的onCreate(捆绑adminkakaam){
    // TODO自动生成方法存根    super.onCreate(adminkakaam);
    的setContentView(R.layout.administrator);
    Toast.makeText(这一点,以管理员身份登录!,Toast.LENGTH_LONG).show();
    最终的EditText new_user =(EditText上)findViewById(R.id.editText1);
    最终的EditText new_pass =(EditText上)findViewById(R.id.textView2);
    按钮add_emp =(按钮)findViewById(R.id.addemployee);
    最终按钮create_acc =(按钮)findViewById(R.id.creat_acc);
    add_emp.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            的setContentView(R.layout.add_employee);
            create_acc.setOnClickListener(新OnClickListener(){                @覆盖
                公共无效的onClick(视图v){
                    // TODO自动生成方法存根
                    // DatabaseHelper accessing_db =新DatabaseHelper(Administrator_Work.this);
                    。//accessing_db.insert_new_user(new_user.getText()的toString(),new_pass.getText()的toString());
                    //Toast.makeText(getApplicationContext(),新用户已经创建了!!,Toast.LENGTH_SHORT).show();                }
            });
        }
    });}}


不推荐使用

解决方案

调用的setContentView 多次。我会建议你使用不同片段的申请,或者至少有一个布局包含您需要的所有意见,并隐藏/根据您的需要告诉他们。如果你坚持要用不同的布局相同的活动,那么你可以看看<一个href=\"http://stackoverflow.com/questions/6811989/android-switching-screens-with-new-activity-or-just-changing-content-view\">here.

what am doing is that am initializing a layout on the start of my activity. Whenever a button on that layout is clicked am initializing another layout in the same activity. Whenever i click the am doing some working regarding insertion of a database record. even on ignoring the database part am getting java null pointer exception in logcat. Kindly have a look and guide me

package com.example.emp_management;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.SyncStateContract.Columns;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.emp_management.DatabaseHelper;

public class Administrator_Work extends Activity{

@Override
protected void onCreate(Bundle adminkakaam) {
    // TODO Auto-generated method stub

    super.onCreate(adminkakaam);
    setContentView(R.layout.administrator);
    Toast.makeText(this, "Logged in as Administrator!",Toast.LENGTH_LONG).show();
    final EditText new_user = (EditText) findViewById(R.id.editText1);
    final EditText new_pass = (EditText) findViewById(R.id.textView2);
    Button add_emp = (Button)findViewById(R.id.addemployee);
    final Button create_acc = (Button) findViewById(R.id.creat_acc);
    add_emp.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub 
            setContentView(R.layout.add_employee);
            create_acc.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //DatabaseHelper accessing_db = new DatabaseHelper(Administrator_Work.this);
                    //accessing_db.insert_new_user(new_user.getText().toString(), new_pass.getText().toString());
                    //Toast.makeText(getApplicationContext(), "New User Has Been Created!!", Toast.LENGTH_SHORT).show();

                }
            });
        }
    });

}

}

解决方案

Calling setContentView multiple times is not recommended. I would advise you to use different fragments for your application, or at least have the first layout contain all the views you need and hide/show them according to your needs. If you insist on using different layouts for the same activity then you could have a look here.

这篇关于调用一个活动多布局在2个不同的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆