0

I am newbie to android. I am developing an android app for an University. For this I am creating a news page. I am trying to parse the xml file onclick of a listitem. But getting error. The main error occurs in the switch statement. Here are my files. Please help me in getting this.

MainActivity.java

package com.example.listviewdemo;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

String[] presidents ={
        "ABC1",
        "ABC2",
        "ABC3"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, presidents));
}

@Override
public void onListItemClick(
        ListView parent, View v, int position, long id)

        {
    switch (item.getItemId()) {

    case R.id.news1:
    Intent a = new Intent(getApplicationContext(), News1.class);
    startActivity(a);
    return true;    

    case R.id.news2:
        Intent b= new Intent(getApplicationContext(), News2.class);
        startActivity(a);
        return true;
    default:

    return super.onOptionsItemSelected(item);
    }
    }

}

activity_news1.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.listviewdemo.News1" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/img1" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_marginBottom="90dp"
    android:text="bla bla bla" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_alignTop="@+id/textView1"
    android:layout_marginTop="14dp"
    android:text="bla bla bla"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="14dp"
    android:text="bla bla bla" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignLeft="@+id/imageView1"
    android:text="bla bla bla" />

</RelativeLayout>

activity_news2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.listviewdemo.News1" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/img1" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_marginBottom="90dp"
    android:text="bla bla bla" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_alignTop="@+id/textView1"
    android:layout_marginTop="14dp"
    android:text="bla bla bla"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="14dp"
    android:text="bla bla bla" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignLeft="@+id/imageView1"
    android:text="bla bla bla" />

</RelativeLayout>

Here is my Logcat:

Description Resource Path Location Type item cannot be resolved MainActivity.java /ListViewDemo/src/com/example/listviewdemo line 34 Java Problem

Description Resource Path Location Type item cannot be resolved to a variable MainActivity.java /ListViewDemo/src/com/example/listviewdemo line 47 Java Problem

Description Resource Path Location Type news1 cannot be resolved or is not a field MainActivity.java /ListViewDemo/src/com/example/listviewdemo line 36 Java Problem

Description Resource Path Location Type news2 cannot be resolved or is not a field MainActivity.java /ListViewDemo/src/com/example/listviewdemo line 41 Java Problem

Description Resource Path Location Type Void methods cannot return a value MainActivity.java /ListViewDemo/src/com/example/listviewdemo line 39 Java Problem

Description Resource Path Location Type Void methods cannot return a value MainActivity.java /ListViewDemo/src/com/example/listviewdemo line 44 Java Problem

Kaushik
  • 6,150
  • 5
  • 39
  • 54
Web
  • 121
  • 3
  • 12
  • in case R.id.news2 startActivity(a) - refers unreachable variable, – user3455363 Aug 04 '14 at 10:29
  • @user3455363, Is the switch statement correct there? – Web Aug 04 '14 at 10:36
  • yes user3455363 is right. You are getting error because you are having intent a in case R.id.news2 inside startActivity() instead of b which is not reachable. – ORIGINAL Aug 04 '14 at 10:38
  • case R.id.news2: Intent b= new Intent(getApplicationContext(), News2.class); startActivity(a); variable a does not exist in that scope, Also if it does not work for both cases in your switch try to change getActivityContext to this - (refer your current isntance of activity) – user3455363 Aug 04 '14 at 10:39
  • Ok I changed that. But that is not my error. I have posted my Logcat, please check and guide me through. – Web Aug 04 '14 at 10:39
  • Do you have your R class accessible? Im asking if can you tell if its been generated ? if its not Check this http://stackoverflow.com/questions/7824730/r-cannot-be-resolved-to-a-variable and this http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error – user3455363 Aug 04 '14 at 10:55
  • your `onItemClick` is wrong.No need to create `news_1` and `news_2`.Single activity can do this stuffs. – Kaushik Aug 04 '14 at 11:51
  • @Kaushik, I have many paragraphs of text and here in my example case News1 and News2 are two diffferent news about the college. So have creatd two different activities for each. – Web Aug 04 '14 at 12:12
  • @Web : you should update those `TextViews` and `ImageViews` instead of creating new `Activity` according to the position has been clicked – Kaushik Aug 04 '14 at 12:57
  • @Kaushil, my requirement is: onclick of a news item, it should display the details of that particular news containing image and some paragraphs of text. So i created new activity. I tried to call a particular activity onclick of a news item. Bt this is not happening. Hope now i am clear with my issue. Please guide me – Web Aug 06 '14 at 06:29

0 Answers0