使用mainactivity查找意图查找意图时出错

时间:2014-08-30 02:01:59

标签: android eclipse listview android-intent

我是新手,面临意图问题。 目标:生成一个列表,然后单击List中的项目,打开一个新的布局,其中一些参数在intent中传递。

问题:

  1. 如果我尝试使用下面的代码,我会收到找不到意图的错误。
  2. 如果我尝试使用特定的intent组件(散列出来的行),它会给运行时异常提示&#34;您的内容必须有一个listview,其id属性为&#39; android.R.id.list&#39;。< / LI>

    请帮忙。

    package com.example.testapp08;
    
    import android.os.Bundle;
    import android.view.View;
    import android.app.ListActivity;
    import android.content.Intent;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    
    public class MainActivity extends ListActivity {
        String[]countries;
        final String THE_ACTION2 = "com.example.testapp08.TEMPT2";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            countries=getResources().getStringArray(R.array.countries);
            this.setListAdapter(new ArrayAdapter<String>(
                    this, 
                    R.layout.list_layout, 
                    R.id.countryName, 
                    countries));
    
            ListView listview = getListView();
    
            listview.setOnItemClickListener(new OnItemClickListener()
            {
    
                @Override
                public void onItemClick(AdapterView<?> parent, View clickView, int position,
                        long id) {
                    // TODO Auto-generated method stub
                    String country = countries[position];
                    //Toast.makeText(MainActivity.this, String.format("%s was chosen", country), Toast.LENGTH_SHORT).show();
                    //Intent intent = new Intent(MainActivity.this, Tempt1.class );
                    Intent intent = new Intent();
                    intent.setAction(THE_ACTION2);
                    intent.putExtra("MY TEXT", country);
                    startActivity(intent);
                }
            });
    
        }
    }
    

    package com.example.testapp08;
    
    
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class Tempt1 extends MainActivity{
            TextView textView1;
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.temp_layout);
            textView1 = (TextView) findViewById(R.id.textView1);
            Bundle bundle = getIntent().getExtras();
    
            String var_from_prev_intent = bundle.getString("MY TEXT");
            textView1.setText(var_from_prev_intent);
    
    }
    }
    

1 个答案:

答案 0 :(得分:0)

我给了你一个非常简单的listview示例。

import android.os.Bundle;
import android.view.View;
import android.app.ListActivity;
import android.content.Intent;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends ListActivity {
    String[]countries;
    final String THE_ACTION2 = "com.example.testapp08.TEMPT2";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        countries=getResources().getStringArray(R.array.country);
        this.setListAdapter(new ArrayAdapter<String>(
                this, 
                android.R.layout.simple_list_item_1, 

                countries));

        ListView listview = getListView();

        listview.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View clickView, int position,
                    long id) {
                // TODO Auto-generated method stub
                String country = countries[position];
                //Toast.makeText(MainActivity.this, String.format("%s was chosen", country), Toast.LENGTH_SHORT).show();
                //Intent intent = new Intent(MainActivity.this, Tempt1.class );
                Intent intent = new Intent(MainActivity.this,MainActivity2.class);
                //intent.setAction(THE_ACTION2);
                intent.putExtra("MY TEXT", country);
                startActivity(intent);
            }
        });

    }
}

2)MainActivity2.java

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity2 extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity2);
        Intent i = getIntent();
        Toast.makeText(this,i.getStringExtra("MY TEXT"), Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_activity2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

现在,当您点击任何列表项时,您将看到以toast msg格式选择的内容。

希望这会对你有所帮助。