Android Intent不起作用

时间:2015-06-07 12:18:57

标签: java android android-intent

所以我有这些代码

main.java

package com.example.kamusinggris_indonesiaidiom;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Main extends Activity {
private TextView teks;
private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    teks = (TextView) findViewById(R.id.text);
    list = (ListView) findViewById(R.id.list);

}
 protected void onNewIntent(Intent intent) {
        handleIntent(getIntent());
         }
private void handleIntent(Intent intent) {
    // TODO Auto-generated method stub
    handleIntent(intent);

     if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            Intent wordIntent = new Intent(this, Definisi.class);
            wordIntent.setData(intent.getData());
            startActivity(wordIntent);
        } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            showResults(query);
}


}
    private void showResults(String query) {
    // TODO Auto-generated method stub
    Cursor cursor = managedQuery(Provider.CONTENT_URI, null, null,
            new String[] {query}, null);

    if (cursor == null) {
        // There are no results
        teks.setText(getString(R.string.no_results, new Object[] {query}));
    } else {
        // Display the number of results
        int count = cursor.getCount();
        String countString = getResources().getQuantityString(R.plurals.search_results,
                                count, new Object[] {count, query});
        teks.setText(countString);

        // Specify the columns we want to display in the result
        String[] from = new String[] { Database.KATA,
                                       Database.DEFINISI };

        // Specify the corresponding layout elements where we want the columns to go
        int[] to = new int[] { R.id.kata,
                               R.id.definisi };

        // Create a simple cursor adapter for the definitions and apply them to the ListView
        SimpleCursorAdapter words = new SimpleCursorAdapter(this,
                                      R.layout.hasil_pencarian, cursor, from, to);
        list.setAdapter(words);

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View tmp,
                    int posisi, long id) {
                // TODO Auto-generated method stub
                Intent definisi = new Intent(getApplicationContext(), Definisi.class);
                Uri data = Uri.withAppendedPath(Provider.CONTENT_URI,
                                                String.valueOf(id));
                definisi.setData(data);
                startActivity(definisi);
            }

});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
                     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())    );
        searchView.setIconifiedByDefault(false);
    }

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search:
            onSearchRequested();
            return true;
        default:
            return false;
    }
}
}

definisi.java

package com.example.kamusinggris_indonesiaidiom;

import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;
import android.widget.TextView;

public class Definisi extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_definisi);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
}
    Uri uri = getIntent().getData();
    Cursor kursor = managedQuery(uri, null, null, null, null);

    if (kursor == null) {
        finish();
    } else {
        kursor.moveToFirst();

        TextView kata = (TextView) findViewById(R.id.kata);
        TextView definisi= (TextView) findViewById(R.id.definisi);

        int wIndex = kursor.getColumnIndexOrThrow(Database.KATA);
        int dIndex = kursor.getColumnIndexOrThrow(Database.DEFINISI);

        kata.setText(kursor.getString(wIndex));
        definisi.setText(kursor.getString(dIndex));
}       }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
    }

    return true;
}   
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search:
            onSearchRequested();
            return true;
        case android.R.id.home:
            Intent a = new Intent(this, Main.class);
            a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(a);
            return true;
        default:
            return false;
    }
}
}

它是一个字典,所以当点击列表视图上的搜索建议时,它应该打开definisi.java并显示定义。但是我在这里得到的是当我点击搜索建议时它显示了main.java(它只是回到上一个活动)。意图部分出了什么问题?请帮帮我

1 个答案:

答案 0 :(得分:1)

intent

中使用已通过的onNewIntent变量
 protected void onNewIntent(Intent intent) {
        handleIntent(intent);
 }

您还应该从handleIntent方法

中删除对handleIntent的来电