我想通过点击照片个人资料转移到其他活动,我不知道该怎么做。当我定义它时,它总是给我一个错误。我想使用string-array
名称中的ID移至另一项活动,请参阅 list_tutor.xml 和 strings.xml 。
a = (ImageView)findViewById(R.id.list_tutor);
如果我写的代码没有错误,但应用程序崩溃并停止工作:
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”
主要活动
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
<?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>
答案 0 :(得分:0)
例外情况告诉您,在SearchRating.java
文件中,您正在访问尚未初始化的ListView
。
查看您的list_tutor.xml
文件,该ID没有ListView,这就是SearchRating
的{{1}}中抛出NullPointerException的原因。
您要为onCreate
ListView
的{{1}}
修改的
获取drawable:使用此方法(将其添加到您的活动中)
list
然后在名为list_tutor.xml
public Drawable getDrawableByStringId(String strId) {
int id = getResources().getIdentifier(strId, "drawable", getPackageName());
return getDrawable(id);
}
您设置项目的方式不必要地复杂化。所以我回答了你项目的设置。
希望你能学到更多并意识到这一点。
答案 1 :(得分:0)
首先,您无法定义a = (ImageView)findViewById(R.id.khalid);
您需要定义它a = (ImageView)findViewById(R.id.list_tutor);
然后根据您的日志,它显示您传递给适配器的值为null。由于这个原因,你的应用程序正在崩溃。尝试使用断点调试应用程序,以检查setAdapter
时传递的值 mylistview = (ListView) findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(this, rowItems); // Here might be problem.
mylistview.setAdapter(adapter);
第三件事是定义你的
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View view1) {
Intent myIntent = new Intent(SearchRating.this, TutorProfile.class);
startActivity(myIntent);
}
});
在customAdapter中,