我创建了一个带有自定义适配器的Spinner,他看起来像这样。
但是,在我的Activity上,当我点击Save按钮时,我尝试getSelectedItem()
或getSelectedItemPosition()
,抛出错误。
执行getSelectedItem()
twrow NullPointerException
错误,执行getSelectedItemPosition()
时错误为Resources$NotFoundException: String resource ID #0x7
按照我的Activity,Spinner Row和Spinner Adapter的代码
FishFormActivity.java
package br.com.vitorvillar.aquariummanager.screens;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import br.com.vitorvillar.aquariummanager.R;
import br.com.vitorvillar.aquariummanager.adapters.FishesSpinnerAdapter;
import br.com.vitorvillar.aquariummanager.libraries.FishTypeMap;
public class FishFormActivity extends Activity {
private Spinner mSpinner;
private EditText mAmount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fish_form);
/** Get the The fields */
this.mSpinner = (Spinner) this.findViewById(R.id.spinnerFishSpecies);
this.mAmount = (EditText) this.findViewById(R.id.txtAmount);
/** Get the FishMap */
FishTypeMap fishType = new FishTypeMap(this);
fishType.loadFreshWaterMap();
/** Create and set the adapter */
FishesSpinnerAdapter adapter = new FishesSpinnerAdapter(this, fishType);
this.mSpinner.setAdapter(adapter);
}
public void saveFish(View view) {
Toast.makeText(this, this.mSpinner.getSelectedItem(), Toast.LENGTH_LONG).show();
}
}
FishesSpinnerAdapter.java
package br.com.vitorvillar.aquariummanager.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.makeramen.roundedimageview.RoundedImageView;
import java.util.List;
import br.com.vitorvillar.aquariummanager.R;
import br.com.vitorvillar.aquariummanager.libraries.FishTypeMap;
public class FishesSpinnerAdapter extends BaseAdapter {
private List<String> mNames;
private List<String> mScientificNames;
private List<Integer> mImages;
private LayoutInflater mLayoutInflater;
public FishesSpinnerAdapter(Context context, FishTypeMap fishType) {
/** Get the values from the Fish type */
this.mNames = fishType.getNames();
this.mScientificNames = fishType.getScientificNames();
this.mImages = fishType.getImages();
/** Get the inflater layout */
this.mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getCount() {
return this.mImages.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = this.mLayoutInflater.inflate(R.layout.fishes_spinner_item, parent, false);
holder = new ViewHolder();
holder.mFishName = (TextView) convertView.findViewById(R.id.txtFishName);
holder.mFishScientificName = (TextView) convertView.findViewById(R.id.txtFishScientificName);
holder.mFishImage = (RoundedImageView) convertView.findViewById(R.id.fishIcon);
/** Set the holder as tag */
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mFishName.setText(this.mNames.get(position));
holder.mFishScientificName.setText(this.mScientificNames.get(position));
holder.mFishImage.setImageResource(this.mImages.get(position));
return convertView;
}
public static class ViewHolder {
public RoundedImageView mFishImage;
public TextView mFishName;
public TextView mFishScientificName;
}
}
fishes_spinner_item.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp">
<com.makeramen.roundedimageview.RoundedImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fishIcon"
android:layout_width="60dp"
android:layout_height="70dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_attention"
app:riv_border_color="#666"
app:riv_border_width="1dp"
app:riv_corner_radius="10dp"
tools:ignore="ContentDescription" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingStart="10dp"
android:paddingTop="12dp">
<TextView
android:id="@+id/txtFishName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Molinésia"
android:textStyle="bold" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="UselessParent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtFishScientificName"
android:textColor="@android:color/darker_gray" />
</TableRow>
</TableLayout>
</LinearLayout>
以下是getSelectedItem()
执行时的错误:
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: FATAL EXCEPTION: main
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: Process: br.com.vitorvillar.aquariummanager, PID: 1614
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method of the activity
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.view.View$1.onClick(View.java:4007)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.view.View.performClick(View.java:4756)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19761)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5253)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.view.View$1.onClick(View.java:4002)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.view.View.performClick(View.java:4756)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19761)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5253)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at br.com.vitorvillar.aquariummanager.screens.FishFormActivity.saveFish(FishFormActivity.java:53)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.view.View$1.onClick(View.java:4002)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.view.View.performClick(View.java:4756)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19761)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5253)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
我创建的这个适配器没有扩展ArrayAdapter
,这可能是问题吗?但是,在执行getSelectedItemPosition()
注意:当我在getSelectedItemPosition()
方法中执行onCreate()
时,返回位置0.
有人可以帮助我吗?
答案 0 :(得分:4)
执行getSelectedItemPosition()时,错误为Resources $ NotFoundException:String resource ID#0x7
这是因为gitkit.widget.handler.onProviderSignInIdpClick_ = function(app, component, idp) {
//null values are removed later in requestGitkitEndpoint
//not sure if extra paramaters are needed in the Request
var request = {
providerId: idp.getProviderId(),
continueUri: app.getConfig().getIdpCallbackUrl(),
oauthScope: app.getConfig().getIdpConfigAdditionalScopes(),
access_type: app.getConfig().getAccessType(),
approval_prompt: app.getConfig().getApprovalPrompt()
};
//the request is then parsed into the executor within component.executeRequest
component.executeRequest(
//executor
goog.bind(app.getApi().createAuthUri, app.getApi()),
//request
request,
//cb
function(resp) {
if (!resp || gitkit.api.hasError(resp)) {
(gitkit.log.error("createAuthUri: " + goog.json.serialize(resp)), component.showInfoBar(gitkit.widget.handler.common.getErrorMessage(gitkit.api.getErrorCode(resp))))
} else {
if(resp.providerId === 'google.com'){
var append = null;
if (goog.isDefAndNotNull(app.getConfig().getAccessType())) {
var paramValue = goog.string.urlEncode(app.getConfig().getAccessType());
append = "&access_type=" + paramValue;
}
if (goog.isDefAndNotNull(app.getConfig().getApprovalPrompt())) {
var paramValue = goog.string.urlEncode(app.getConfig().getApprovalPrompt());
if(append) append = append.concat("&approval_prompt=" + paramValue);
else append = "&approval_prompt=" + paramValue
}
if(append){
resp.authUri = resp.authUri.concat(append);
}
}
resp.sessionId && gitkit.storage.setSessionId(resp.sessionId, app.getAppId()),
gitkit.storage.setRememberAccount(!1, app.getAppId()),
gitkit.util.goTo(goog.asserts.assert(resp.authUri));
}
});
};
的返回类型是int - 所以getSelectedItemPosition()
方法认为它是字符串资源ID。要正确显示位置,请使用makeText()
作为String.valueOf(getSelectedItemPosition())
方法的第二个参数。
执行getSelectedItem()twrow时出现NullPointerException错误
从堆栈跟踪看起来,当您没有选择任何项目时,您正试图在makeText()
上调用toString()
,因此我的建议是在调用{{1}之前检查getSelectedItem()
是否getSelectedItem() != null
}}
另一个观察是您在适配器内返回makeText()
null
。将其更改为getItem()
当我在onCreate()方法中执行getSelectedItemPosition()时,返回位置0
由于您尚未选择项目 - return mNames.get(position);
将返回默认值0。
答案 1 :(得分:0)
ResourceNotFound of 7因为你试图Toast the number 7。
试试这个。
Toast.makeText(this, "" +this.mSpinner.getSelectedItem(), Toast.LENGTH_LONG).show();
原因是你正在调用接受R.string
资源id的重载方法,它是整数,但值7不存在,因此你的错误。