0

我正在尝试从 Listview 项目开始我的活动。它对第一项工作正常,但当我单击时出现错误SecondActivity。这是我的ListView活动代码

public class menu extends ListActivity{
    String [] menuItems = {"MainActivity","SecondActivity","Item2","Item3"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(menu.this,android.R.layout.simple_expandable_list_item_1, menuItems));

    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        try {

            Log.d("Tst","Value");
            Class menuItem = Class.forName("com.example.newboston." + menuItems[position] );


            Intent itemItent = new Intent(menu.this, menuItem);
            startActivity(itemItent);

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block 
            e.printStackTrace();
        }       
    }
}

这是我的代码SecondActivity

public class SecondActivity extends Activity{

    EditText etPass = (EditText) findViewById(R.id.etPassword);
    Button btPass =(Button) findViewById(R.id.btPassword);
    ToggleButton tgPass = (ToggleButton) findViewById(R.id.tgPassword);
    TextView tvPass = (TextView) findViewById(R.id.displayTV);
    Random animate = new Random();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
//      Log tag;
//      Log.d("testing", "In toggle java");
        setContentView(R.layout.togglebtn);
        passTextCheck();    
        textPosition();
    }

    public void passTextCheck(){
    tgPass.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(tgPass.isChecked()){
                etPass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }else{
                etPass.setInputType(InputType.TYPE_CLASS_TEXT);
            }   
        }
        });
    }
    public void textPosition(){
        btPass.setOnClickListener(new View.OnClickListener() {
//          Toast testToast = new Toast(toggle.this);
            CharSequence text;
            @Override
            public void onClick(View v) {
                String check  = tgPass.getText().toString();
                if(check.contentEquals("left")){
                    tvPass.setGravity(Gravity.LEFT);
                    tvPass.setTextColor(Color.BLUE);
                    text = "Left + Blue";
                    Toast.makeText(SecondActivity.this, text ,Toast.LENGTH_SHORT).show();

                }else if(check.contentEquals("right")){
                    tvPass.setGravity(Gravity.RIGHT);
                    tvPass.setTextColor(Color.GREEN);
                    text = "Right + Green";
                    Toast.makeText(SecondActivity.this, text ,Toast.LENGTH_SHORT).show();
                }else if(check.contentEquals("center")){
                    tvPass.setGravity(Gravity.CENTER);
                    tvPass.setTextColor(Color.MAGENTA);
                    text = "Center + Magenta";
                    Toast.makeText(SecondActivity.this, text ,Toast.LENGTH_SHORT).show();
                }else if(check.contentEquals("animate")){
                    tvPass.setText("Hello");
                    tvPass.setGravity(Gravity.CENTER);
                    tvPass.setTextColor(Color.rgb(animate.nextInt(50), animate.nextInt(50),animate.nextInt(50))  );
                }
                // TODO Auto-generated method stub

            }
        });

    }
}

这是我的AndroidManifest.xml代码

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.newboston"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.newboston.Startup"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.newboston.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.newboston.MAINACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.newboston.menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.newboston.menu" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.newboston.SecondActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.newboston.SecondActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
4

2 回答 2

0

一个明显的错误。您正在外部初始化您的视图onCreate。您可以findViewById将当前视图层次结构设置为活动。您需要先将内容设置为活动,然后初始化您的视图。如果没有,你会得到NullPointerException.

    EditText etPass;
    Button btPass; 
    ToggleButton tgPass;
    TextView tvPass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.togglebtn);
    etPass = (EditText) findViewById(R.id.etPassword);
    btPass =(Button) findViewById(R.id.btPassword);
    tgPass = (ToggleButton) findViewById(R.id.tgPassword);
    tvPass = (TextView) findViewById(R.id.displayTV);
    .... // rest of the code
    }
于 2013-08-01T05:19:44.613 回答
0
public class SecondActivity extends Activity{  

    @Override
     protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //  Log tag;
    // Log.d("testing", "In toggle java");
    setContentView(R.layout.togglebtn);


**Changes here** 
**EditText etPass = (EditText) findViewById(R.id.etPassword);
Button btPass =(Button) findViewById(R.id.btPassword);
ToggleButton tgPass = (ToggleButton) findViewById(R.id.tgPassword);
TextView tvPass = (TextView) findViewById(R.id.displayTV);
Random animate = new Random();**

    passTextCheck();    
    textPosition();
}

public void passTextCheck(){
tgPass.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(tgPass.isChecked()){
            etPass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }else{
            etPass.setInputType(InputType.TYPE_CLASS_TEXT);
        }   
    }
    });
}
public void textPosition(){
    btPass.setOnClickListener(new View.OnClickListener() {
//          Toast testToast = new Toast(toggle.this);
            CharSequence text;
            @Override
            public void onClick(View v) {
                String check  = tgPass.getText().toString();
                if(check.contentEquals("left")){
                    tvPass.setGravity(Gravity.LEFT);
                    tvPass.setTextColor(Color.BLUE);
                    text = "Left + Blue";
                    Toast.makeText(SecondActivity.this, text ,Toast.LENGTH_SHORT).show();

            }else if(check.contentEquals("right")){
                tvPass.setGravity(Gravity.RIGHT);
                tvPass.setTextColor(Color.GREEN);
                text = "Right + Green";
                Toast.makeText(SecondActivity.this, text ,Toast.LENGTH_SHORT).show();
            }else if(check.contentEquals("center")){
                tvPass.setGravity(Gravity.CENTER);
                tvPass.setTextColor(Color.MAGENTA);
                text = "Center + Magenta";
                Toast.makeText(SecondActivity.this, text ,Toast.LENGTH_SHORT).show();
            }else if(check.contentEquals("animate")){
                tvPass.setText("Hello");
                tvPass.setGravity(Gravity.CENTER);
                tvPass.setTextColor(Color.rgb(animate.nextInt(50), animate.nextInt(50),animate.nextInt(50))  );
            }
            // TODO Auto-generated method stub

        }
    });

}

}

于 2013-08-01T05:17:28.047 回答