Android:操作栏内的AppCompatActivity和textview

时间:2016-03-05 18:00:54

标签: java android android-toolbar appcompatactivity

我想在AppCompatActivity的操作栏中添加一个autocompletetextview组件。我的代码是:

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Thread.setDefaultUncaughtExceptionHandler(new GlobalException());

        Toolbar tb = (Toolbar) findViewById(R.id.toolbar_search);
        setSupportActionBar(tb);
}

布局/ action_bar_search.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar_search"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:layout_scrollFlags="scroll|enterAlways"
    app:layout_collapseMode="pin">

    <AutoCompleteTextView
        android:id="@+id/theSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/search_the"
        android:imeOptions="actionSearch"
        android:inputType="textAutoComplete|textAutoCorrect"
        android:textColor="#FFFFFF" >

        <requestFocus />
    </AutoCompleteTextView>

</android.support.v7.widget.Toolbar>

当我的应用程序打开时,我看到一个空白的白色屏幕,几秒钟后,它变成一个空白的黑屏,永远保持这种状态,直到我手动停止。有什么想法吗?

显然,Toolbar tb = (Toolbar) findViewById(R.id.toolbar_search); tb为空后

2 个答案:

答案 0 :(得分:2)

Toolbar您的R.layout.activity_main XML未包含在findById中,因此Toolbar无效。这会导致<include>保持为空并导致应用程序崩溃。您可以使用<include layout="@layout/action_bar_search"/>标记添加布局资源。在您的情况下:import io import sys import random import time import math lowbound = 33 highbound = 126 key=str def offset_factor(key): total = 0 for index in range(8): numm = ord(key[index]) total += numm total = total//8 total = total - 32 return total def generateoffset(key): total = 0 for i in range(0,8): offsetFactor = offsetFactor +ord(key[i]) offsetFactor = offsetFactor//8-32 return offsetFactor def generatekey(): key=" " for line in range(8): randomnumber = random.randint(lowbound,highbound) randomchar = chr(randomnumber) key += randomchar return key def decrypt(): file = open(input("Please enter the name of the text file you want " "to decrypt \n(include the extension): "), mode="r").read() encryption_key = input("Please enter the encryption key that was used to encrypt this text file: ") offset_factor, decrypted_message = 0, "" for char in encryption_key: offset_factor += int(ord(char)) offset_factor = math.floor(offset_factor / 8) - 32 for char in file: if char == " ": decrypted_message += " " else: decrypted_character = (ord(char)) - offset_factor if decrypted_character < 33: decrypted_character += 94 decrypted_message += str(chr(decrypted_character)) print(decrypted_message) def runProgram(): def choice(): userChoice = input( "1. Encrypt Message\n" "2. Decrypt Message\n" "3. Exit The Program\n" "Please enter the number of what you want to do:\n>>") if userChoice == "1": return userChoice elif userChoice == "2": return decrypt() elif userChoice == "3": print("Exiting Program") sys.exit() else: print("Invalid Response. Please try again.") choice() def getMessage(): #Beginning of where the encryption starts print("Enter the file name which you want to Encrypt\n") userMessage = input("") try: filetoencrypt = open(userMessage,"r") print (filetoencrypt) except: print("You have given a file which does not exist. please try again.\n") time.sleep(1) choice() else: print("File has been Successfully found.") toencrypt = [] for line in filetoencrypt: toencrypt.append(line.strip()) # key = generatekey() print ("Key has been generated:" + key) offset = offset_factor(key) encrypted = [] for line in toencrypt: encryptedline = "" for char in line: key1, number, getMessage = generatekey(), 0, "" offset_factor1 = math.floor(number / 8) - 32 if char == '': encryptedline +='' else: encrypt_letter = ord(char) + (offset_factor1) if encrypt_letter > 126: encrypt_letter -= 94 getMessage += str(chr(encrypt_letter)) for line in range(1): print("") NewEncrypt = input("Would you like to make it more secure, enter Yes or No: ") time.sleep(1) if NewEncrypt.upper() == "YES": getMessage = getMessage.replace(" ", "") getMessage = ' '.join([getMessage[i:i+5] for i in range(0, len(getMessage), 5)]) elif NewEncrypt.upper == "NO": getMessage = getMessage else: print("You entered an invalid option.") exit() open(input("Text encrypted successfully. \nWhat would you like to save the new " "file as (include the extension): "), mode="w").write(getMessage) print ("File saved.") sys.exit() userChoice = choice() message = getMessage() fl = decrypt() runProgram()

答案 1 :(得分:0)

以下是AutocompleteTextView的示例代码:

AutoCompleteTextView text;
String[] languages={"Android ","java","IOS","SQL","JDBC","Web services"};

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  text=(AutoCompleteTextView)findViewById(R.id.theSearch);
  ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);
  text.setAdapter(adapter);
  text.setThreshold(1);    
  }

我希望这会对你有帮助....