我的应用程序在ParkActivity中使用onClickListener时崩溃,没有任何错误信息。

huangapple go评论71阅读模式
英文:

My app is getting crash without any errors if I use onClickListener in ParkActivity

问题

以下是你提供的代码的翻译部分:

我是Android开发的初学者,有一个疑问,如何从第二个Activity跳转到第三个Activity。

从MainActivity:

TextView parkText = (TextView)findViewById(R.id.park);
parkText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent i = new Intent(view.getContext(), ParkActivity.class);
        startActivity(i);
    }
});

从ParkActivity:

public class ParkActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.place_list);
        
        final ArrayList<Places> parkPlaceList = new ArrayList<Places>();
        parkPlaceList.add(new Places("TowerPark", R.mipmap.chennai_hdpi));
        // 其他地方省略...
    }
}

从Places:

package com.example.chennaitourapp;

public class Places {
    private String mtextview;
    private int mimageResourceId;

    public Places(String textView, int imageResourceId) {
        mtextview = textView;
        mimageResourceId = imageResourceId;
    }

    // getter方法省略...
}

从PlaceAdapter:

public class PlaceAdapter extends ArrayAdapter<Places> {
    public PlaceAdapter(@NonNull Context context, ArrayList<Places> parkPlacelist) {
        super(context, 0, parkPlacelist);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // ...
        return listItemView;
    }
}

现在我需要从ParkActivity跳转到ParkActivity2,我创建了另一个名为ParkActivity2的Activity。

如果我从ParkActivity点击或触摸TextView,我希望能够转到ParkActivity2。我知道我需要使用OnClickListener,在MainActivity中已经完成了这一步,但我需要知道在哪里编写该代码。

实际上,我将该代码写在了ParkActivity中,但是我的应用程序崩溃了,没有任何错误信息。

英文:

I'm a beginner in Android Development and I have one doubt, How to pass from 2nd Activity into 3rd Activity

From MainActivity



    TextView parkText = (TextView)findViewById(R.id.park);
            parkText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent i = new Intent(view.getContext(),ParkActivity.class);
                    startActivity(i);
                }
            });

From ParkActivity

    public class ParkActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           setContentView(R.layout.place_list);
    
           final ArrayList&lt;Places&gt; parkPlaceList = new ArrayList&lt;Places&gt;();
            parkPlaceList.add(new Places(&quot;TowerPark&quot;,R.mipmap.chennai_hdpi));
            parkPlaceList.add(new Places(&quot;TowerPark&quot;,R.mipmap.chennai_hdpi));
            parkPlaceList.add(new Places(&quot;TowerPark&quot;,R.mipmap.chennai_hdpi));
            parkPlaceList.add(new Places(&quot;TowerPark&quot;,R.mipmap.chennai_hdpi));
            parkPlaceList.add(new Places(&quot;TowerPark&quot;,R.mipmap.chennai_hdpi));
            parkPlaceList.add(new Places(&quot;TowerPark&quot;,R.mipmap.chennai_hdpi));
            parkPlaceList.add(new Places(&quot;TowerPark&quot;,R.mipmap.chennai_hdpi));
    
            PlaceAdapter itemsAdapter = new PlaceAdapter(this,parkPlaceList);
            ListView placeList = (ListView)findViewById(R.id.list);
            placeList.setAdapter(itemsAdapter);
    
    
    
        }
    }

From Places

package com.example.chennaitourapp;
public class Places {


    private String mtextview;
     private int mimageResourceId;

     public Places(String textView, int imageResourceId){
         mtextview = textView;
         mimageResourceId = imageResourceId;
     }
    public String getMtextview() {
        return mtextview;
    }

    public int getMimageResourceId() {
        return mimageResourceId;
    }
}

From PlaceAdapter

public class PlaceAdapter extends ArrayAdapter&lt;Places&gt; {
    public PlaceAdapter(@NonNull Context context, ArrayList&lt;Places&gt;parkPlacelist) {
        super(context, 0,parkPlacelist);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listItemView = convertView;
        if (listItemView == null){
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.imagepname,parent,false);
        }
      Places placeListPosition = getItem(position);

        TextView placesName = (TextView)listItemView.findViewById(R.id.text);
        placesName.setText(placeListPosition.getMtextview());

        ImageView placesImage = (ImageView)listItemView.findViewById(R.id.image);
        placesImage.setImageResource(placeListPosition.getMimageResourceId());





        return listItemView;

Now I have to pass from ParkActivity To ParkActivity2 and I created another Activity called ParkActivity2

If I touch or click textview from ParkActivity I need to go to ParkActivity and I know I have to use onClickListener which I already done in MainActivity but I need to know where I have to write that code

Actually I wrote that code in ParkActivity but my app is getting crashed without any errors.

答案1

得分: 1

ParkActivity中,在设置适配器之后,为你的listView(在你的情况下是placeList)设置onItemClickListener

placeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // 跳转到ParkActivity2
                startActivity(new Intent(context, ParkActivity2.class));
            }
        });

如果你是手动添加的,请不要忘记在清单文件中添加你的活动。

英文:

In ParkActivity set onItemClickListener in your listView (which is placeList in your case) after setting the adapter.

placeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
                //go to ParkActivity2
                startActivity(new Intent(context,ParkActivity2.class));
            }
        });

Don't forget to add your Activity in Manifest, if you added it manually.

huangapple
  • 本文由 发表于 2020年10月6日 18:37:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64224078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定