我的app 2微调器,基本上是下拉列表,允许用户选择值。 Spinner1在下拉列表中有4个选项,Spinner2在下拉列表中有5个选项。
如果用户从spinner1中选择option1而从spinner2选择option1,则应该在视图的其余部分的片段中的webview中显示项目符号列表[在此应用的HTML webapp版本中,我使用li标签]。图像将让事情更清楚。 现在,每当我尝试从两个微调器中选择值并单击“提交”时,应用程序就会崩溃。
我附上以下代码
这适用于启动活动 -
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MyAndroidAppActivity extends Activity {
private Spinner spinner1, spinner2;
private Button btnSubmit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
//add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("Equipment");
list.add("Indications");
list.add("Illustration");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
//get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
/*@Override
public void onClick(View v) {
Toast.makeText(MyAndroidAppActivity.this,
"OnClickListener : " +
"\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem()) +
"\nSpinner 2 : " + String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}*/
@Override
public void onClick(View v) {
WebViewerFragment wvf = (WebViewerFragment) getFragmentManager().findFragmentById(R.id.webviewfrag);
if (wvf != null && wvf.getView() != null) {
wvf.updateWebView(spinner1.getSelectedItemPosition(), spinner2.getSelectedItemPosition());
}
}
});
}
}
这是webview的方法
import android.R.string;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.Toast;
public class WebViewerFragment extends Fragment {
private WebView mWebView;
// a two dimensional array representing the data to put in the WebView
String mData[][]= new String[4][5];
{
for (int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
mData[i][j]="<html><body><h1>Temp1</h1></body></html>";
}
}
}
/* Toast.makeText(this, mData[i][j],Toast.LENGTH_SHORT);
*/
/* int[][] a1 = {
{ 1, 2, 3, },
{ 4, 5, 6, },
};
{ {},{}};
*/
/* String SurgEqui="<html><body><h2>Equipment</h2><ul><li><p>IV catheter :Be certain that IV is in place and flushing easily</p></li></body><html>";
*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mainView = (View) inflater.inflate(R.layout.frag_layout, container, false);
mWebView = (WebView) mainView.findViewById(R.id.webView1);
return mainView;
}
public void updateWebView(int firstSelection, int secondSelection) {
if (firstSelection == AdapterView.INVALID_POSITION || secondSelection == AdapterView.INVALID_POSITION) {
return;
} else {
if (mWebView != null) {
mWebView.loadData(mData[firstSelection][secondSelection].replace("+", "%20"), "text/html", "utf-8");
// mWebView.loadData(mData[firstSelection][secondSelection], "text/html", null);
//wv.loadData(URLEncoder.encode(myString).replace("+", "%20"), "text/html, "utf-8");
}
}
}
}
这是布局
<?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:orientation="vertical"
>
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="101dp"
android:text="@string/submit" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:entries="@array/country_arrays" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/spinner1" />
<fragment
android:id="@+id/webviewfrag"
android:name="android.webkit.WebViewFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnSubmit"
android:layout_marginLeft="246dp"
android:layout_marginTop="244dp"
/>
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnSubmit" />
</RelativeLayout>
答案 0 :(得分:0)
您应该包含带有错误的logcat。但是我注意到了两个问题:
1.去除:
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
来自addListenerOnButton()方法。
2.在addListenerOnButton()之前调用addListenerOnSpinnerItemSelection():
addListenerOnSpinnerItemSelection();
addListenerOnButton();