活动之间的导航不起作用



我正在我的安卓应用程序中使用列表视图。 列表视图工作正常,但是当我尝试在项目单击时导航到另一个活动时,我的应用程序崩溃并在模拟器上显示您的应用程序意外停止的消息。

这是我onItemClick代码:

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    Intent i=new Intent(MainActivity.this,CityActivity.class);
    i.putExtra("me","hello");
    startActivity(i);
}

我的第二个活动的代码:

public class CityActivity extends Activity {
    TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cityname);
        text= (TextView)findViewById(R.id.textView1);
        Intent intent = getIntent();
        String message=intent.getStringExtra("me");
        text.setText(message);
    }
    @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, menu);
        return true;
    }
}

第二个活动的 XML 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

我的第一个活动的完整代码:

public class MainActivity extends Activity implements OnItemClickListener {
    ListView listview;
    List<Country_Name> items;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview=(ListView)findViewById(R.id.list);
        JSONObject main_obj=getjson();
        items=new ArrayList<Country_Name>();
        try {
            JSONArray array=main_obj.getJSONArray("Country");
            for(int i=0;i<array.length();i++) {
                JSONObject obj=array.getJSONObject(i);
                String name=obj.getString("Name");
                String image_url=obj.getString("Image");
                Country_Name country=new Country_Name(name,image_url);
                JSONArray array_city=obj.getJSONArray("City");
                for(int j=0;j<array_city.length();j++)
                {
                JSONObject obj2=array_city.getJSONObject(i);
                country.items.add(new City_Name(obj2.getString("CName"),obj2.getString("CImage")));
                }
                this.items.add(country);
            }
        }
        catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        CustomeBaseAdapter adapter=new CustomeBaseAdapter(this,this.items);
        this.listview.setAdapter(adapter);
        listview.setOnItemClickListener(this);
    }

    @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, menu);
        return true;
    }
    public JSONObject getjson() {
        String json="";
        StringBuilder builder=new StringBuilder();
        JSONObject obj=null;
        try {
            BufferedReader reader=new BufferedReader(new InputStreamReader(getAssets().open("Country.txt")));
            String line="";
            while((line=reader.readLine())!=null)
            {
            builder.append(line);
            }
            json=builder.toString();
            obj=new JSONObject(json);
        }
        catch(Exception ex) {
            ex.toString();
        }
        return obj;
    }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        Intent i=new Intent(MainActivity.this,CityActivity.class);
        i.putExtra("me","hello");
        startActivity(i);
        /*Toast toast = Toast.makeText(getApplicationContext(),"hello",
        Toast.LENGTH_SHORT);
        toast.show();*/
    }
}

假设你的"CustomBaseAdapter"类和jSONObject没问题,我应该建议你记录和调试以检查它在哪里失败。似乎新活动还可以,因此通话可能会失败。您可以尝试使用其他方式拨打电话,至少在我喜欢更多的情况下:

  public class MainActivity extends Activity {
  *instead of:*
  public class MainActivity extends Activity implements OnItemClickListener {
  *and use "setOnItemClickListener" method instead:*
  listview.setOnItemClickListener(new OnItemClickListener(){
     @Override
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
     }
  });

希望它能有所帮助!祝你好运

相关内容

  • 没有找到相关文章

最新更新