当searchActivity的setContentView时,应用崩溃

时间:2018-12-28 05:49:43

标签: android android-layout

我在ActionBar上创建了一个搜索框。如果我使用SearchViewMainActivity上搜索字符串,它将激活处理搜索查询的SearchActivity。但是,当我调用setContentView()来设置SearchActivity的布局时,应用程序崩溃了。

public class SearchActivity extends ListActivity {

private TextView textview;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_search);
    handleIntent(getIntent());
    //textview=findViewById(R.id.search_result);
}

public void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}


private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        doSearch(query);
    }
}

public void doSearch(String query) {
    //textview.setText("You have searched for "+query);
}
}

这是我的MainActivity

   public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
    }

    @Override
    public boolean onCreateOptionsMenu( Menu menu )
    {
        getMenuInflater().inflate( R.menu.search_menu, menu );

        // Add SearchWidget.
        SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
        SearchView searchView = (SearchView) menu.findItem( R.id.search_box ).getActionView();

        searchView.setSearchableInfo( searchManager.getSearchableInfo( getComponentName() ) );

        return super.onCreateOptionsMenu( menu );
    }

    }

这是我的search_layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/search_result"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />


</RelativeLayout>

这是AndroidManifest.xml文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.anjaleeps.loancomparison">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="android.app.default_searchable"
        android:value=".SearchActivity" />
    <activity
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:name=".SearchActivity" >
        <intent-filter >
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <intent-filter >
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

</application>
</manifest>

1 个答案:

答案 0 :(得分:1)

尝试将AppCompatActivity扩展到SearchActivity。如果您的意图不为空,那么它应该起作用。

public class SearchActivity extends AppCompatActivity 

有关更多信息,您可以参考https://developer.android.com/reference/android/app/Activity

相关问题