开发者

Using two or more activty on Android

code below, don't know whats the problem

package and.views;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class androidView extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public boolean onCreateOptionsMenu(Menu menu)
    {
        super.onCreateOptionsMenu(menu);
        menu.add(0,0, 0, "AutoComplete"); 
        menu.add(0,1, 1, "Button"); 
        menu.add(0,2, 2, "CheckBox");
        menu.add(0,3, 3, "EditText"); 
        menu.add(0,4, 4, "RadioGroup");
        menu.add(0,5, 5, "Spinner"); 

        return true;
    }
    /** Override onOptionsItemSelected to execute code 开发者_运维技巧for each menu item */
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch(item.getItemId())
        {
        case 0:
            showAutoComplete();
            return true;
        case 1:
            return true;
        case 2:
            return true;
        case 3:
            return true;
        case 4:
            return true;
        case 5:
            return true;

        }
        return true;
    }

public void showAutoComplete()
    { 
        Intent autocomplete = new Intent(this, AutoComplete.class);   
   try{  

        this.startActivity(autocomplete);
   }
catch(Exception e)
{
    System.out.print(" activity not found");
}
   }
}

2nd class

package and.views;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

public class AutoComplete extends Activity{
    public void onCreate(Bundle icircle) {
        super.onCreate(icircle);
        setContentView(R.layout.autocomplete);
       ArrayAdapter<String> monthArray=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Months);
       final AutoCompleteTextView textView= (AutoCompleteTextView)findViewById(R.id.testAutoComplete);
       textView.setAdapter(monthArray);
       final Button changeButton=(Button)findViewById(R.id.testAutoComplete);
       changeButton.setOnClickListener(new Button.OnClickListener(){
          public void onClick(View v)
          {
              changeOption(textView);
          }

       });
       final Button changeButton2 = (Button) findViewById(R.id.textColorButton);
       changeButton2.setOnClickListener(new Button.OnClickListener() 
       { public void onClick(View v)
       { changeOption2(textView);
       }
       });

    }   
    static final String[]Months= new String[]{ "January","February","March","April","May","June","July","August", "September","October","November","December" }; 
    public void changeOption(AutoCompleteTextView text)

    { 
        if (text.getHeight()==100){ text.setHeight(30);
    } 
    else
        { 
        text.setHeight(100); 
        } 
    } public void changeOption2(AutoCompleteTextView text)
    { 
        text.setTextColor(Color.RED); 
    }
    }

Manifest file

enter code here<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="and.views"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".androidView"
                  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=".AutoComplete" android:label="AutoComplete" android:launchMode="standard" android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
   </application>
</manifest> 


Chetan, I'm not quite sure what the exact problem you are having here, but I'm guessing you get a nullPointerException when you try to launch a new Activity? I didn't take an extensive look at your code but I noticed you don't have any of the extra Activities in your Manifest. Anytime you create a new Activity to be launched you need to add it to the Manifest. I'm pretty new to Android so I'm not too sure why all this is, but I came across that problem before as well.


Not too sure if the answer came too late for you.

Not much information about simple_list_item_1 online I can find, but if your problem is with the AutoCompleteTextView context popup not showing any of your prefilled autocomplete text, a change to simple_dropdown_item_1line should solve the problem

Note: I'm testing on API8

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜