通过单击ImageView移至另一个活动 [英] Moving to another activity by clicking ImageView
问题描述
我想通过单击照片个人资料转到其他活动,我不知道该怎么做.当我定义它时,总是会给我一个错误.我想使用string-array
名称中的ID转到其他活动,请参见 list_tutor.xml 和 strings.xml .
I want to move to other activity by clicking photo profile, I don't know how to do it. It always give me an error when I defined it. I want to move to another activity using id in string-array
names see list_tutor.xml and strings.xml.
a = (ImageView)findViewById(R.id.list_tutor);
如果我编写的代码没有错误,但是应用程序崩溃并停止工作:
If I write that code has no error, but app crashed and stop working:
11-30 05:57:30.787 7171-7171/com.example.abdullahalhajri.smarttutor D/AndroidRuntime: Shutting down VM
11-30 05:57:30.790 7171-7171/com.example.abdullahalhajri.smarttutor E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abdullahalhajri.smarttutor, PID: 7171
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abdullahalhajri.smarttutor/com.example.abdullahalhajri.smarttutor.activities.SearchRating}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.abdullahalhajri.smarttutor.activities.SearchRating.onCreate(SearchRating.java:63)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Activity.performCreate(Activity.java:6991)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
如果我这样写
a = (ImageView)findViewById(R.id.khalid)
(khalid是profile_pics项目上的项目,请参见strings.xml),它在khalid
上给我红色错误:
(khalid is item on profile_pics items see strings.xml), it gives me red color error on khalid
:
无法解析符号"khalid"
cannot resolve symbol "khalid"
主要活动
public class SearchRating extends Activity implements OnItemClickListener {
ImageView a;
String[] member_names;
TypedArray profile_pics;
String[] statues;
String[] contactType;
List<RowItem> rowItems;
ListView mylistview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_tutor);
Intent intent0 = getIntent();
a = (ImageView)findViewById(R.id.profile_pic);;
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View view1) {
Intent myIntent = new Intent(SearchRating.this, TutorProfile.class);
startActivity(myIntent);
}
});
rowItems = new ArrayList<RowItem>();
member_names = getResources().getStringArray(R.array.Member_names);
profile_pics = getResources().obtainTypedArray(R.array.profile_pics);
statues = getResources().getStringArray(R.array.statues);
contactType = getResources().getStringArray(R.array.contactType);
for (int i = 0; i < member_names.length; i++) {
RowItem item = new RowItem(member_names[i],
profile_pics.getResourceId(i, -1), statues[i],
contactType[i]);
rowItems.add(item);
}
mylistview = (ListView) findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(this, rowItems);
mylistview.setAdapter(adapter);
mylistview.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String member_name = rowItems.get(position).getMember_name();
Toast.makeText(getApplicationContext(), "" + member_name,
Toast.LENGTH_SHORT).show();
}
}
CustomAdapter
package com.example.abdullahalhajri.smarttutor.activities;
/**
* Created by Khalid on 11/27/17.
*/
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.abdullahalhajri.smarttutor.R;
import java.util.List;
public class CustomAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;
CustomAdapter(Context context, List<RowItem> rowItems) {
this.context = context;
this.rowItems = rowItems;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
/* private view holder class */
private class ViewHolder {
ImageView profile_pic;
TextView member_name;
TextView status;
TextView contactType;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_tutor, null);
holder = new ViewHolder();
holder.member_name = (TextView) convertView
.findViewById(R.id.member_name);
holder.profile_pic = (ImageView) convertView
.findViewById(R.id.profile_pic);
holder.status = (TextView) convertView.findViewById(R.id.status);
holder.contactType = (TextView) convertView
.findViewById(R.id.contact_type);
RowItem row_pos = rowItems.get(position);
holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
holder.member_name.setText(row_pos.getMember_name());
holder.status.setText(row_pos.getStatus());
holder.contactType.setText(row_pos.getContactType());
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
TutorProfile
package com.example.abdullahalhajri.smarttutor.activities;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import com.example.abdullahalhajri.smarttutor.R;
public class TutorProfile extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorprofile);
Intent intent0 = getIntent();
}
}
list_tutor.xml
list_tutor.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="2dp" >
<ImageView
android:id="@+id/profile_pic"
android:layout_width="70dp"
android:layout_height="70dp"
android:contentDescription="desc"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="@+id/member_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/profile_pic"
android:paddingBottom="10dp"
android:text="txt"
android:textSize="20sp" />
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/member_name"
android:layout_below="@+id/member_name"
android:text="txt"
android:textSize="16sp" />
<TextView
android:id="@+id/contact_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/member_name"
android:layout_alignBottom="@+id/member_name"
android:layout_alignParentRight="true"
android:text="txt"
android:textSize="16sp" />
</RelativeLayout>
strings.xml
<!-- ListviewTuruor -->
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<!-- Names -->
<string-array name="Member_names">
<item>Robert</item>
<item>Shanni</item>
<item>Rachael</item>
<item>Maddy</item>
<item>Kate</item>
<item>Emma</item>
<item>Isabella</item>
<item>Khalid</item>
<item>Sophia</item>
</string-array>
<!-- Pictures -->
<array name="profile_pics">
<item>@drawable/robert</item>
<item>@drawable/shanni</item>
<item>@drawable/rachael</item>
<item>@drawable/maddy_pic</item>
<item>@drawable/kate</item>
<item>@drawable/nikhil_pic</item>
<item>@drawable/isabella</item>
<item>@drawable/khalid</item>
<item>@drawable/sophia</item>
</array>
<!-- Status -->
<string-array name="statues">
<item>Math</item>
<item>IT</item>
<item>Biology</item>
<item>English</item>
<item>Art</item>
<item>Natural Science</item>
<item>Art</item>
<item>Networking</item>
<item>Chemistry</item>
</string-array>
<!-- contact type -->
<string-array name="contactType">
<item>4.7/5</item>
<item>4.6/5</item>
<item>4.4/5</item>
<item>4/5</item>
<item>3.9/5</item>
<item>3.5/5</item>
<item>3.5/5</item>
<item>2.7/5</item>
<item>2/5</item>
</string-array>
布局 activity_search_rating
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
推荐答案
该异常告诉您在SearchRating.java
文件中,您正在访问尚未初始化的ListView
.
The exception is telling you that in your SearchRating.java
file, you're accessing a ListView
that you haven't initialized yet.
查看您的list_tutor.xml
文件,该ID没有ListView,这就是为什么在SearchRating
的onCreate
中抛出NullPointerException的原因.
Looking at your list_tutor.xml
file, there's no ListView by that ID and that is why a NullPointerException is being the thrown in SearchRating
's onCreate
.
您要将ID为list
的ListView
添加到list_tutor.xml
You want to add a ListView
with the ID list
to your list_tutor.xml
编辑
要获取可绘制对象:请使用此方法(将其添加到您的活动中)
For getting the drawables: use this method (add it in your activity)
public Drawable getDrawableByStringId(String strId) {
int id = getResources().getIdentifier(strId, "drawable", getPackageName());
return getDrawable(id);
}
然后在名称为onItemClick(AdapterView<?> parent, View view, int position, long id)
String id = customAdapter.getItem(position).split("/")[1];
Drawable aDrawable = getDrawableByStringId(id);
设置项目的方式不必要地复杂.因此,我对您的项目的设置进行了回答.
The way you have set up your project is unnecessarily complicated. So I answered in respect to your project's setup.
希望您能学到更多并意识到这一点.
Hopefully you'll learn more and realize that.
这篇关于通过单击ImageView移至另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!